diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-12 12:04:03 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-12 12:04:03 +0800 |
| commit | fff01530bbbfed7e7b76566d15ade9836eeaf14f (patch) | |
| tree | fa8eb48217b982fa94eacb251c256dc5b2cc281f /doc/docs | |
| parent | 01a84cb83973b1dbbcc0cbbc6835ea3f3ec755e3 (diff) | |
| download | yuescript-fff01530bbbfed7e7b76566d15ade9836eeaf14f.tar.gz yuescript-fff01530bbbfed7e7b76566d15ade9836eeaf14f.tar.bz2 yuescript-fff01530bbbfed7e7b76566d15ade9836eeaf14f.zip | |
Again. [skip CI]
Diffstat (limited to 'doc/docs')
| -rw-r--r-- | doc/docs/.vitepress/theme/index.ts | 151 | ||||
| -rw-r--r-- | doc/docs/de/doc/getting-started/usage.md | 2 | ||||
| -rw-r--r-- | doc/docs/doc/getting-started/usage.md | 2 | ||||
| -rw-r--r-- | doc/docs/id-id/doc/getting-started/usage.md | 2 | ||||
| -rw-r--r-- | doc/docs/pt-br/doc/getting-started/usage.md | 2 | ||||
| -rw-r--r-- | doc/docs/zh/doc/getting-started/usage.md | 2 |
6 files changed, 154 insertions, 7 deletions
diff --git a/doc/docs/.vitepress/theme/index.ts b/doc/docs/.vitepress/theme/index.ts index 7e9882e..4dd7408 100644 --- a/doc/docs/.vitepress/theme/index.ts +++ b/doc/docs/.vitepress/theme/index.ts | |||
| @@ -1,7 +1,11 @@ | |||
| 1 | import DefaultTheme from "vitepress/theme"; | 1 | import DefaultTheme from "vitepress/theme"; |
| 2 | import type { Theme } from "vitepress"; | 2 | import type { Theme } from "vitepress"; |
| 3 | import { h } from "vue"; | 3 | import { useRoute } from "vitepress"; |
| 4 | import { h, nextTick, watch } from "vue"; | ||
| 4 | import "./custom.css"; | 5 | import "./custom.css"; |
| 6 | import darkPlus from "@shikijs/themes/dark-plus"; | ||
| 7 | import lightPlus from "@shikijs/themes/light-plus"; | ||
| 8 | import yuescriptGrammar from "../grammars/yuescript.tmLanguage.json"; | ||
| 5 | 9 | ||
| 6 | // @ts-ignore | 10 | // @ts-ignore |
| 7 | import CompilerModal from "./components/CompilerModal.vue"; | 11 | import CompilerModal from "./components/CompilerModal.vue"; |
| @@ -12,6 +16,134 @@ import YueCompiler from "./components/YueCompiler.vue"; | |||
| 12 | // @ts-ignore | 16 | // @ts-ignore |
| 13 | import YueDisplay from "./components/YueDisplay.vue"; | 17 | import YueDisplay from "./components/YueDisplay.vue"; |
| 14 | 18 | ||
| 19 | type ShikiHighlighter = Awaited< | ||
| 20 | ReturnType<(typeof import("shiki/core"))["createHighlighterCore"]> | ||
| 21 | >; | ||
| 22 | |||
| 23 | let shikiHighlighterPromise: Promise<ShikiHighlighter> | null = null; | ||
| 24 | let shikiRepairQueued = false; | ||
| 25 | let shikiRepairRetry1 = 0; | ||
| 26 | let shikiRepairRetry2 = 0; | ||
| 27 | |||
| 28 | type SupportedLanguage = "yuescript" | "lua" | "shellscript"; | ||
| 29 | |||
| 30 | function toSupportedLanguage(className: string): SupportedLanguage | null { | ||
| 31 | const match = className.match(/(?:^|\s)language-([a-z0-9_-]+)/i); | ||
| 32 | if (!match) return null; | ||
| 33 | const lang = match[1].toLowerCase(); | ||
| 34 | if (lang === "yuescript" || lang === "yue") return "yuescript"; | ||
| 35 | if (lang === "lua") return "lua"; | ||
| 36 | if ( | ||
| 37 | lang === "shellscript" || | ||
| 38 | lang === "shell" || | ||
| 39 | lang === "bash" || | ||
| 40 | lang === "sh" || | ||
| 41 | lang === "zsh" | ||
| 42 | ) { | ||
| 43 | return "shellscript"; | ||
| 44 | } | ||
| 45 | return null; | ||
| 46 | } | ||
| 47 | |||
| 48 | function getShikiHighlighter() { | ||
| 49 | if (!shikiHighlighterPromise) { | ||
| 50 | shikiHighlighterPromise = Promise.all([ | ||
| 51 | import("shiki/core"), | ||
| 52 | import("shiki/engine/javascript"), | ||
| 53 | import("@shikijs/langs/lua"), | ||
| 54 | import("@shikijs/langs/shellscript"), | ||
| 55 | ]).then( | ||
| 56 | ([ | ||
| 57 | { createHighlighterCore }, | ||
| 58 | { createJavaScriptRegexEngine }, | ||
| 59 | luaLang, | ||
| 60 | shellscriptLang, | ||
| 61 | ]) => | ||
| 62 | createHighlighterCore({ | ||
| 63 | themes: [lightPlus, darkPlus], | ||
| 64 | langs: [ | ||
| 65 | { | ||
| 66 | ...yuescriptGrammar, | ||
| 67 | name: "yuescript", | ||
| 68 | scopeName: "source.yuescript", | ||
| 69 | aliases: ["yue"], | ||
| 70 | }, | ||
| 71 | ...luaLang.default, | ||
| 72 | ...shellscriptLang.default, | ||
| 73 | ], | ||
| 74 | engine: createJavaScriptRegexEngine(), | ||
| 75 | }), | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | return shikiHighlighterPromise; | ||
| 79 | } | ||
| 80 | |||
| 81 | function collectPlainCodeTargets() { | ||
| 82 | if (typeof document === "undefined") return []; | ||
| 83 | const nodes = Array.from( | ||
| 84 | document.querySelectorAll<HTMLElement>( | ||
| 85 | "pre > code[class*='language-'], code[class*='language-']", | ||
| 86 | ), | ||
| 87 | ); | ||
| 88 | return nodes.filter((code) => { | ||
| 89 | if (code.closest("pre.shiki")) return false; | ||
| 90 | const pre = code.parentElement; | ||
| 91 | if (pre && pre.tagName === "PRE" && pre.classList.contains("shiki")) { | ||
| 92 | return false; | ||
| 93 | } | ||
| 94 | return toSupportedLanguage(code.className) !== null; | ||
| 95 | }); | ||
| 96 | } | ||
| 97 | |||
| 98 | async function repairPlainCodeBlocks() { | ||
| 99 | const targets = collectPlainCodeTargets(); | ||
| 100 | if (!targets.length) return; | ||
| 101 | |||
| 102 | const highlighter = await getShikiHighlighter(); | ||
| 103 | |||
| 104 | for (const code of targets) { | ||
| 105 | const lang = toSupportedLanguage(code.className); | ||
| 106 | if (!lang) continue; | ||
| 107 | const source = (code.textContent || "").replace(/\r\n?/g, "\n"); | ||
| 108 | if (!source.trim()) continue; | ||
| 109 | const html = highlighter.codeToHtml(source, { | ||
| 110 | lang, | ||
| 111 | themes: { | ||
| 112 | light: "light-plus", | ||
| 113 | dark: "dark-plus", | ||
| 114 | }, | ||
| 115 | }); | ||
| 116 | const tpl = document.createElement("template"); | ||
| 117 | tpl.innerHTML = html.trim(); | ||
| 118 | const replacement = tpl.content.firstElementChild as HTMLElement | null; | ||
| 119 | if (!replacement) continue; | ||
| 120 | replacement.classList.add("vp-code"); | ||
| 121 | const pre = code.parentElement?.tagName === "PRE" ? code.parentElement : null; | ||
| 122 | (pre || code).replaceWith(replacement); | ||
| 123 | } | ||
| 124 | |||
| 125 | applyShikiInlineColorFallback(); | ||
| 126 | } | ||
| 127 | |||
| 128 | function scheduleShikiRepair() { | ||
| 129 | if (typeof window === "undefined") return; | ||
| 130 | if (shikiRepairQueued) return; | ||
| 131 | shikiRepairQueued = true; | ||
| 132 | window.requestAnimationFrame(() => { | ||
| 133 | shikiRepairQueued = false; | ||
| 134 | void repairPlainCodeBlocks(); | ||
| 135 | }); | ||
| 136 | } | ||
| 137 | |||
| 138 | function scheduleShikiRepairWithRetries() { | ||
| 139 | if (typeof window === "undefined") return; | ||
| 140 | scheduleShikiRepair(); | ||
| 141 | window.clearTimeout(shikiRepairRetry1); | ||
| 142 | window.clearTimeout(shikiRepairRetry2); | ||
| 143 | shikiRepairRetry1 = window.setTimeout(scheduleShikiRepair, 120); | ||
| 144 | shikiRepairRetry2 = window.setTimeout(scheduleShikiRepair, 500); | ||
| 145 | } | ||
| 146 | |||
| 15 | function applyShikiInlineColorFallback() { | 147 | function applyShikiInlineColorFallback() { |
| 16 | if (typeof document === "undefined") return; | 148 | if (typeof document === "undefined") return; |
| 17 | const isDark = document.documentElement.classList.contains("dark"); | 149 | const isDark = document.documentElement.classList.contains("dark"); |
| @@ -31,13 +163,28 @@ const theme: Theme = { | |||
| 31 | h(DefaultTheme.Layout, null, { | 163 | h(DefaultTheme.Layout, null, { |
| 32 | "layout-bottom": () => [h(HomeFooter), h(CompilerModal)], | 164 | "layout-bottom": () => [h(HomeFooter), h(CompilerModal)], |
| 33 | }), | 165 | }), |
| 166 | setup() { | ||
| 167 | if (typeof window === "undefined") return; | ||
| 168 | const route = useRoute(); | ||
| 169 | watch( | ||
| 170 | () => route.path, | ||
| 171 | async () => { | ||
| 172 | await nextTick(); | ||
| 173 | scheduleShikiRepairWithRetries(); | ||
| 174 | }, | ||
| 175 | { immediate: true }, | ||
| 176 | ); | ||
| 177 | }, | ||
| 34 | enhanceApp({ app }) { | 178 | enhanceApp({ app }) { |
| 35 | app.component("CompilerModal", CompilerModal); | 179 | app.component("CompilerModal", CompilerModal); |
| 36 | app.component("YueCompiler", YueCompiler); | 180 | app.component("YueCompiler", YueCompiler); |
| 37 | app.component("YueDisplay", YueDisplay); | 181 | app.component("YueDisplay", YueDisplay); |
| 38 | 182 | ||
| 39 | if (typeof window !== "undefined") { | 183 | if (typeof window !== "undefined") { |
| 40 | const run = () => requestAnimationFrame(applyShikiInlineColorFallback); | 184 | const run = () => { |
| 185 | requestAnimationFrame(applyShikiInlineColorFallback); | ||
| 186 | scheduleShikiRepairWithRetries(); | ||
| 187 | }; | ||
| 41 | window.addEventListener("DOMContentLoaded", run, { once: true }); | 188 | window.addEventListener("DOMContentLoaded", run, { once: true }); |
| 42 | window.addEventListener("load", run, { once: true }); | 189 | window.addEventListener("load", run, { once: true }); |
| 43 | 190 | ||
diff --git a/doc/docs/de/doc/getting-started/usage.md b/doc/docs/de/doc/getting-started/usage.md index 2c10406..2a1864a 100644 --- a/doc/docs/de/doc/getting-started/usage.md +++ b/doc/docs/de/doc/getting-started/usage.md | |||
| @@ -8,7 +8,7 @@ YueScript-Modul in Lua verwenden: | |||
| 8 | 8 | ||
| 9 | "your_yuescript_entry.yue" in Lua require'n. | 9 | "your_yuescript_entry.yue" in Lua require'n. |
| 10 | 10 | ||
| 11 | ```Lua | 11 | ```lua |
| 12 | require("yue")("your_yuescript_entry") | 12 | require("yue")("your_yuescript_entry") |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
diff --git a/doc/docs/doc/getting-started/usage.md b/doc/docs/doc/getting-started/usage.md index 8818dd0..c02e8c0 100644 --- a/doc/docs/doc/getting-started/usage.md +++ b/doc/docs/doc/getting-started/usage.md | |||
| @@ -8,7 +8,7 @@ Use YueScript module in Lua: | |||
| 8 | 8 | ||
| 9 | Require "your_yuescript_entry.yue" in Lua. | 9 | Require "your_yuescript_entry.yue" in Lua. |
| 10 | 10 | ||
| 11 | ```Lua | 11 | ```lua |
| 12 | require("yue")("your_yuescript_entry") | 12 | require("yue")("your_yuescript_entry") |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
diff --git a/doc/docs/id-id/doc/getting-started/usage.md b/doc/docs/id-id/doc/getting-started/usage.md index a9523f7..a092163 100644 --- a/doc/docs/id-id/doc/getting-started/usage.md +++ b/doc/docs/id-id/doc/getting-started/usage.md | |||
| @@ -8,7 +8,7 @@ Gunakan modul YueScript di Lua: | |||
| 8 | 8 | ||
| 9 | Require "your_yuescript_entry.yue" di Lua. | 9 | Require "your_yuescript_entry.yue" di Lua. |
| 10 | 10 | ||
| 11 | ```Lua | 11 | ```lua |
| 12 | require("yue")("your_yuescript_entry") | 12 | require("yue")("your_yuescript_entry") |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
diff --git a/doc/docs/pt-br/doc/getting-started/usage.md b/doc/docs/pt-br/doc/getting-started/usage.md index 055c6c2..c3b5dfe 100644 --- a/doc/docs/pt-br/doc/getting-started/usage.md +++ b/doc/docs/pt-br/doc/getting-started/usage.md | |||
| @@ -8,7 +8,7 @@ Use o módulo YueScript em Lua: | |||
| 8 | 8 | ||
| 9 | Use require em "your_yuescript_entry.yue" no Lua. | 9 | Use require em "your_yuescript_entry.yue" no Lua. |
| 10 | 10 | ||
| 11 | ```Lua | 11 | ```lua |
| 12 | require("yue")("your_yuescript_entry") | 12 | require("yue")("your_yuescript_entry") |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
diff --git a/doc/docs/zh/doc/getting-started/usage.md b/doc/docs/zh/doc/getting-started/usage.md index c9bad2e..b605e7c 100644 --- a/doc/docs/zh/doc/getting-started/usage.md +++ b/doc/docs/zh/doc/getting-started/usage.md | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | 8 | ||
| 9 |   在 Lua 中引入 "你的脚本入口文件.yue"。 | 9 |   在 Lua 中引入 "你的脚本入口文件.yue"。 |
| 10 | 10 | ||
| 11 | ```Lua | 11 | ```lua |
| 12 | require("yue")("你的脚本入口文件") | 12 | require("yue")("你的脚本入口文件") |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
