@nikala-ui/folio 0.15.1 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/ui/code-block-commands.d.ts +4 -0
- package/dist/components/ui/code-block-commands.js +58 -0
- package/dist/components/ui/code-block.d.ts +2 -1
- package/dist/components/ui/code-block.jsx +4 -127
- package/dist/components/ui/code-highlighter.d.ts +2 -0
- package/dist/components/ui/code-highlighter.js +76 -0
- package/dist/config.js +3 -0
- package/dist/mdx/builtin-languages.d.ts +34 -0
- package/dist/mdx/builtin-languages.js +54 -0
- package/dist/mdx/custom-languages.d.ts +10 -0
- package/dist/mdx/custom-languages.js +52 -0
- package/dist/mdx/highlighter.d.ts +2 -1
- package/dist/mdx/highlighter.js +19 -52
- package/dist/server/plugin/constants.d.ts +2 -0
- package/dist/server/plugin/constants.js +2 -0
- package/dist/server/plugin/index.js +2 -1
- package/dist/server/plugin/virtual-modules.js +8 -2
- package/dist/templates/env.d.ts +5 -0
- package/dist/types.d.ts +9 -0
- package/dist/vendor/docs-src/components/ui/code-block-commands.ts +65 -0
- package/dist/vendor/docs-src/components/ui/code-block.tsx +5 -132
- package/dist/vendor/docs-src/components/ui/code-highlighter.ts +76 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type CodeBlockPackageManager = "bunx" | "npx" | "pnpm" | "yarn";
|
|
2
|
+
export declare function highlightCliCommand(code: string): string;
|
|
3
|
+
export declare function transformCommandForPm(cmd: string, pm: CodeBlockPackageManager): string;
|
|
4
|
+
export declare function escapeHtml(str: string): string;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Pure helpers that format and highlight CLI commands for CodeBlock's package manager runner.
|
|
2
|
+
export function highlightCliCommand(code) {
|
|
3
|
+
const trimmed = code.trim();
|
|
4
|
+
const parts = trimmed.split(/\s+/);
|
|
5
|
+
if (parts.length === 0)
|
|
6
|
+
return code;
|
|
7
|
+
const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
|
|
8
|
+
if (!isRunner)
|
|
9
|
+
return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
|
|
10
|
+
return parts
|
|
11
|
+
.map((part, index) => {
|
|
12
|
+
if (index === 0) {
|
|
13
|
+
return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
|
|
14
|
+
}
|
|
15
|
+
if (part.startsWith("-")) {
|
|
16
|
+
return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
|
|
17
|
+
}
|
|
18
|
+
if (part.startsWith("@")) {
|
|
19
|
+
return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
|
|
20
|
+
}
|
|
21
|
+
if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
|
|
22
|
+
return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
|
|
23
|
+
}
|
|
24
|
+
return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
|
|
25
|
+
})
|
|
26
|
+
.join(" ");
|
|
27
|
+
}
|
|
28
|
+
export function transformCommandForPm(cmd, pm) {
|
|
29
|
+
const trimmed = cmd.trim();
|
|
30
|
+
const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
|
|
31
|
+
let base = trimmed;
|
|
32
|
+
for (const r of runners) {
|
|
33
|
+
if (trimmed.startsWith(r)) {
|
|
34
|
+
base = trimmed.slice(r.length).trim();
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
switch (pm) {
|
|
39
|
+
case "bunx":
|
|
40
|
+
return `bunx ${base}`;
|
|
41
|
+
case "npx":
|
|
42
|
+
return `npx ${base}`;
|
|
43
|
+
case "pnpm":
|
|
44
|
+
return `pnpm dlx ${base}`;
|
|
45
|
+
case "yarn":
|
|
46
|
+
return `yarn dlx ${base}`;
|
|
47
|
+
default:
|
|
48
|
+
return `${pm} ${base}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function escapeHtml(str) {
|
|
52
|
+
return str
|
|
53
|
+
.replace(/&/g, "&")
|
|
54
|
+
.replace(/</g, "<")
|
|
55
|
+
.replace(/>/g, ">")
|
|
56
|
+
.replace(/"/g, """)
|
|
57
|
+
.replace(/'/g, "'");
|
|
58
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type JSX, type ParentComponent } from "solid-js";
|
|
2
|
-
|
|
2
|
+
import { type CodeBlockPackageManager } from "./code-block-commands";
|
|
3
|
+
export type { CodeBlockPackageManager };
|
|
3
4
|
export interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
4
5
|
/** The raw source code text to display and copy */
|
|
5
6
|
code?: string;
|
|
@@ -5,115 +5,10 @@ import { Badge } from "@/components/ui/badge";
|
|
|
5
5
|
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
|
6
6
|
import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
|
|
7
7
|
import { createClipboard } from "@/hooks/create-clipboard";
|
|
8
|
+
import { highlightOnClient } from "./code-highlighter";
|
|
9
|
+
import { escapeHtml, highlightCliCommand, transformCommandForPm } from "./code-block-commands";
|
|
8
10
|
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
9
11
|
import { cn } from "@/lib/cn";
|
|
10
|
-
let shikiHighlighterPromise = null;
|
|
11
|
-
async function getShikiHighlighter() {
|
|
12
|
-
if (typeof window === "undefined")
|
|
13
|
-
return null;
|
|
14
|
-
if (!shikiHighlighterPromise) {
|
|
15
|
-
try {
|
|
16
|
-
const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
|
|
17
|
-
import("shiki/core"),
|
|
18
|
-
import("shiki/engine/javascript"),
|
|
19
|
-
]);
|
|
20
|
-
const createHighlighter = createBundledHighlighter({
|
|
21
|
-
langs: {
|
|
22
|
-
typescript: () => import("@shikijs/langs/typescript"),
|
|
23
|
-
javascript: () => import("@shikijs/langs/javascript"),
|
|
24
|
-
tsx: () => import("@shikijs/langs/tsx"),
|
|
25
|
-
jsx: () => import("@shikijs/langs/jsx"),
|
|
26
|
-
bash: () => import("@shikijs/langs/shellscript"),
|
|
27
|
-
json: () => import("@shikijs/langs/json"),
|
|
28
|
-
css: () => import("@shikijs/langs/css"),
|
|
29
|
-
html: () => import("@shikijs/langs/html"),
|
|
30
|
-
rust: () => import("@shikijs/langs/rust"),
|
|
31
|
-
},
|
|
32
|
-
themes: {
|
|
33
|
-
"github-dark": () => import("@shikijs/themes/github-dark"),
|
|
34
|
-
"github-light": () => import("@shikijs/themes/github-light"),
|
|
35
|
-
},
|
|
36
|
-
engine: () => createJavaScriptRegexEngine(),
|
|
37
|
-
});
|
|
38
|
-
shikiHighlighterPromise = createHighlighter({
|
|
39
|
-
themes: ["github-dark", "github-light"],
|
|
40
|
-
langs: [
|
|
41
|
-
"typescript",
|
|
42
|
-
"javascript",
|
|
43
|
-
"tsx",
|
|
44
|
-
"jsx",
|
|
45
|
-
"bash",
|
|
46
|
-
"json",
|
|
47
|
-
"css",
|
|
48
|
-
"html",
|
|
49
|
-
"rust",
|
|
50
|
-
],
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
catch (e) {
|
|
54
|
-
console.warn("Failed to load Shiki highlighter", e);
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return shikiHighlighterPromise;
|
|
59
|
-
}
|
|
60
|
-
function highlightCliCommand(code) {
|
|
61
|
-
const trimmed = code.trim();
|
|
62
|
-
const parts = trimmed.split(/\s+/);
|
|
63
|
-
if (parts.length === 0)
|
|
64
|
-
return code;
|
|
65
|
-
const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
|
|
66
|
-
if (!isRunner)
|
|
67
|
-
return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
|
|
68
|
-
return parts
|
|
69
|
-
.map((part, index) => {
|
|
70
|
-
if (index === 0) {
|
|
71
|
-
return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
|
|
72
|
-
}
|
|
73
|
-
if (part.startsWith("-")) {
|
|
74
|
-
return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
|
|
75
|
-
}
|
|
76
|
-
if (part.startsWith("@")) {
|
|
77
|
-
return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
|
|
78
|
-
}
|
|
79
|
-
if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
|
|
80
|
-
return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
|
|
81
|
-
}
|
|
82
|
-
return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
|
|
83
|
-
})
|
|
84
|
-
.join(" ");
|
|
85
|
-
}
|
|
86
|
-
function transformCommandForPm(cmd, pm) {
|
|
87
|
-
const trimmed = cmd.trim();
|
|
88
|
-
const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
|
|
89
|
-
let base = trimmed;
|
|
90
|
-
for (const r of runners) {
|
|
91
|
-
if (trimmed.startsWith(r)) {
|
|
92
|
-
base = trimmed.slice(r.length).trim();
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
switch (pm) {
|
|
97
|
-
case "bunx":
|
|
98
|
-
return `bunx ${base}`;
|
|
99
|
-
case "npx":
|
|
100
|
-
return `npx ${base}`;
|
|
101
|
-
case "pnpm":
|
|
102
|
-
return `pnpm dlx ${base}`;
|
|
103
|
-
case "yarn":
|
|
104
|
-
return `yarn dlx ${base}`;
|
|
105
|
-
default:
|
|
106
|
-
return `${pm} ${base}`;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
function escapeHtml(str) {
|
|
110
|
-
return str
|
|
111
|
-
.replace(/&/g, "&")
|
|
112
|
-
.replace(/</g, "<")
|
|
113
|
-
.replace(/>/g, ">")
|
|
114
|
-
.replace(/"/g, """)
|
|
115
|
-
.replace(/'/g, "'");
|
|
116
|
-
}
|
|
117
12
|
export const CodeBlock = (props) => {
|
|
118
13
|
const siteLocale = useSiteLocale();
|
|
119
14
|
const [local, rest] = splitProps(props, [
|
|
@@ -158,26 +53,8 @@ export const CodeBlock = (props) => {
|
|
|
158
53
|
setHighlightedHtml("");
|
|
159
54
|
return;
|
|
160
55
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (highlighter) {
|
|
164
|
-
try {
|
|
165
|
-
const html = highlighter.codeToHtml(codeStr, {
|
|
166
|
-
lang: lang === "js" ? "javascript" : lang === "ts" ? "typescript" : lang,
|
|
167
|
-
themes: {
|
|
168
|
-
light: "github-light",
|
|
169
|
-
dark: "github-dark",
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
setHighlightedHtml(html);
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
catch (err) {
|
|
176
|
-
console.warn(`Shiki failed for language: ${lang}`, err);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
// Fallback
|
|
180
|
-
setHighlightedHtml(`<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
|
|
56
|
+
highlightOnClient(codeStr, lang).then((html) => {
|
|
57
|
+
setHighlightedHtml(html ?? `<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
|
|
181
58
|
});
|
|
182
59
|
});
|
|
183
60
|
const handleCopy = async () => {
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Browser-side Shiki highlighting for CodeBlock usages without server-rendered children.
|
|
2
|
+
let highlighterPromise = null;
|
|
3
|
+
async function getHighlighter() {
|
|
4
|
+
if (typeof window === "undefined")
|
|
5
|
+
return null;
|
|
6
|
+
if (!highlighterPromise) {
|
|
7
|
+
try {
|
|
8
|
+
const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
|
|
9
|
+
import("shiki/core"),
|
|
10
|
+
import("shiki/engine/javascript"),
|
|
11
|
+
]);
|
|
12
|
+
const createHighlighter = createBundledHighlighter({
|
|
13
|
+
langs: {
|
|
14
|
+
typescript: () => import("@shikijs/langs/typescript"),
|
|
15
|
+
javascript: () => import("@shikijs/langs/javascript"),
|
|
16
|
+
tsx: () => import("@shikijs/langs/tsx"),
|
|
17
|
+
jsx: () => import("@shikijs/langs/jsx"),
|
|
18
|
+
bash: () => import("@shikijs/langs/shellscript"),
|
|
19
|
+
json: () => import("@shikijs/langs/json"),
|
|
20
|
+
css: () => import("@shikijs/langs/css"),
|
|
21
|
+
html: () => import("@shikijs/langs/html"),
|
|
22
|
+
rust: () => import("@shikijs/langs/rust"),
|
|
23
|
+
},
|
|
24
|
+
themes: {
|
|
25
|
+
"github-dark": () => import("@shikijs/themes/github-dark"),
|
|
26
|
+
"github-light": () => import("@shikijs/themes/github-light"),
|
|
27
|
+
},
|
|
28
|
+
engine: () => createJavaScriptRegexEngine(),
|
|
29
|
+
});
|
|
30
|
+
highlighterPromise = createHighlighter({
|
|
31
|
+
themes: ["github-dark", "github-light"],
|
|
32
|
+
langs: ["typescript", "javascript", "tsx", "jsx", "bash", "json", "css", "html", "rust"],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.warn("Failed to load Shiki highlighter", e);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return highlighterPromise;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolve a language from the site's `shiki.customLangs`. The grammar module is
|
|
44
|
+
* a separate lazy chunk, fetched only for languages the highlighter lacks.
|
|
45
|
+
*/
|
|
46
|
+
async function loadCustomLanguage(highlighter, lang) {
|
|
47
|
+
try {
|
|
48
|
+
const { default: languages } = await import("virtual:folio-highlight-languages");
|
|
49
|
+
const match = languages.find((entry) => [entry.name, ...(entry.aliases ?? [])].some((id) => id.toLowerCase() === lang));
|
|
50
|
+
if (!match)
|
|
51
|
+
return null;
|
|
52
|
+
await highlighter.loadLanguage(match);
|
|
53
|
+
return match.name;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.warn(`Failed to load custom Shiki language: ${lang}`, err);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Highlight code in the browser, or return null when the caller should fall back to plain text. */
|
|
61
|
+
export async function highlightOnClient(code, rawLang) {
|
|
62
|
+
const highlighter = await getHighlighter();
|
|
63
|
+
if (!highlighter)
|
|
64
|
+
return null;
|
|
65
|
+
const requested = rawLang === "js" ? "javascript" : rawLang === "ts" ? "typescript" : rawLang;
|
|
66
|
+
const lang = highlighter.getLoadedLanguages().includes(requested)
|
|
67
|
+
? requested
|
|
68
|
+
: (await loadCustomLanguage(highlighter, requested.toLowerCase())) ?? requested;
|
|
69
|
+
try {
|
|
70
|
+
return highlighter.codeToHtml(code, { lang, themes: { light: "github-light", dark: "github-dark" } });
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
console.warn(`Shiki failed for language: ${rawLang}`, err);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/config.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { pathToFileURL } from "node:url";
|
|
4
4
|
import fs from "fs-extra";
|
|
5
|
+
import { validateCustomLanguages } from "./mdx/custom-languages.js";
|
|
5
6
|
import { validateFolioPlugins } from "./plugin.js";
|
|
6
7
|
import { loadSiteTranslations } from "./plugins/i18n/site-locale.js";
|
|
7
8
|
export const DEFAULT_DOCS_CONFIG = {
|
|
@@ -51,6 +52,7 @@ export const DEFAULT_DOCS_CONFIG = {
|
|
|
51
52
|
};
|
|
52
53
|
export function defineDocsConfig(config) {
|
|
53
54
|
validateFolioPlugins(config.plugins);
|
|
55
|
+
validateCustomLanguages(config.shiki?.customLangs);
|
|
54
56
|
if (config.uiLocale !== undefined) {
|
|
55
57
|
throw new Error("[folio] Configure site translations through createI18nPlugin() in DocsConfig.plugins");
|
|
56
58
|
}
|
|
@@ -140,6 +142,7 @@ export async function resolveDocsConfig(cwd = process.cwd()) {
|
|
|
140
142
|
...pluginConfig.uiLocale,
|
|
141
143
|
},
|
|
142
144
|
};
|
|
145
|
+
validateCustomLanguages(mergedConfig.shiki?.customLangs);
|
|
143
146
|
mergedConfig.uiLocale = await loadSiteTranslations(cwd, mergedConfig.uiLocale);
|
|
144
147
|
return mergedConfig;
|
|
145
148
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const languageLoaders: {
|
|
2
|
+
readonly typescript: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
3
|
+
readonly javascript: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
4
|
+
readonly tsx: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
5
|
+
readonly jsx: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
6
|
+
readonly bash: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
7
|
+
readonly json: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
8
|
+
readonly css: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
9
|
+
readonly html: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
10
|
+
readonly markdown: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
11
|
+
readonly mdx: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
12
|
+
readonly rust: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
13
|
+
readonly python: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
14
|
+
readonly yaml: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
15
|
+
readonly toml: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
16
|
+
readonly sql: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
17
|
+
readonly diff: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
18
|
+
readonly c: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
19
|
+
readonly cpp: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
20
|
+
readonly go: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
21
|
+
readonly mermaid: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
22
|
+
readonly dockerfile: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
23
|
+
readonly zig: () => Promise<typeof import("@shikijs/langs/*")>;
|
|
24
|
+
};
|
|
25
|
+
/** Fence-language shorthands that resolve to a built-in language id. */
|
|
26
|
+
export declare const LANGUAGE_ALIASES: Readonly<Record<string, string>>;
|
|
27
|
+
/** Identifiers that always render without highlighting. */
|
|
28
|
+
export declare const PLAIN_TEXT_IDS: readonly ["text", "txt", "plain"];
|
|
29
|
+
/**
|
|
30
|
+
* Every identifier Folio already owns: loadable languages, shorthand aliases
|
|
31
|
+
* and plain-text names. Custom languages must not reuse any of them.
|
|
32
|
+
*/
|
|
33
|
+
export declare const RESERVED_LANGUAGE_IDS: ReadonlySet<string>;
|
|
34
|
+
export declare function normalizeLangAlias(lang: string): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Built-in Shiki language catalog shared by the highlighter and config validation.
|
|
2
|
+
export const languageLoaders = {
|
|
3
|
+
typescript: () => import("@shikijs/langs/typescript"),
|
|
4
|
+
javascript: () => import("@shikijs/langs/javascript"),
|
|
5
|
+
tsx: () => import("@shikijs/langs/tsx"),
|
|
6
|
+
jsx: () => import("@shikijs/langs/jsx"),
|
|
7
|
+
bash: () => import("@shikijs/langs/shellscript"),
|
|
8
|
+
json: () => import("@shikijs/langs/json"),
|
|
9
|
+
css: () => import("@shikijs/langs/css"),
|
|
10
|
+
html: () => import("@shikijs/langs/html"),
|
|
11
|
+
markdown: () => import("@shikijs/langs/markdown"),
|
|
12
|
+
mdx: () => import("@shikijs/langs/mdx"),
|
|
13
|
+
rust: () => import("@shikijs/langs/rust"),
|
|
14
|
+
python: () => import("@shikijs/langs/python"),
|
|
15
|
+
yaml: () => import("@shikijs/langs/yaml"),
|
|
16
|
+
toml: () => import("@shikijs/langs/toml"),
|
|
17
|
+
sql: () => import("@shikijs/langs/sql"),
|
|
18
|
+
diff: () => import("@shikijs/langs/diff"),
|
|
19
|
+
c: () => import("@shikijs/langs/c"),
|
|
20
|
+
cpp: () => import("@shikijs/langs/cpp"),
|
|
21
|
+
go: () => import("@shikijs/langs/go"),
|
|
22
|
+
mermaid: () => import("@shikijs/langs/mermaid"),
|
|
23
|
+
dockerfile: () => import("@shikijs/langs/docker"),
|
|
24
|
+
zig: () => import("@shikijs/langs/zig"),
|
|
25
|
+
};
|
|
26
|
+
/** Fence-language shorthands that resolve to a built-in language id. */
|
|
27
|
+
export const LANGUAGE_ALIASES = {
|
|
28
|
+
shell: "bash",
|
|
29
|
+
sh: "bash",
|
|
30
|
+
zsh: "bash",
|
|
31
|
+
js: "javascript",
|
|
32
|
+
ts: "typescript",
|
|
33
|
+
py: "python",
|
|
34
|
+
rs: "rust",
|
|
35
|
+
yml: "yaml",
|
|
36
|
+
md: "markdown",
|
|
37
|
+
rb: "ruby",
|
|
38
|
+
cs: "csharp",
|
|
39
|
+
};
|
|
40
|
+
/** Identifiers that always render without highlighting. */
|
|
41
|
+
export const PLAIN_TEXT_IDS = ["text", "txt", "plain"];
|
|
42
|
+
/**
|
|
43
|
+
* Every identifier Folio already owns: loadable languages, shorthand aliases
|
|
44
|
+
* and plain-text names. Custom languages must not reuse any of them.
|
|
45
|
+
*/
|
|
46
|
+
export const RESERVED_LANGUAGE_IDS = new Set([
|
|
47
|
+
...Object.keys(languageLoaders),
|
|
48
|
+
...Object.keys(LANGUAGE_ALIASES),
|
|
49
|
+
...PLAIN_TEXT_IDS,
|
|
50
|
+
]);
|
|
51
|
+
export function normalizeLangAlias(lang) {
|
|
52
|
+
const id = lang.toLowerCase().trim();
|
|
53
|
+
return Object.hasOwn(LANGUAGE_ALIASES, id) ? LANGUAGE_ALIASES[id] : id;
|
|
54
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LanguageRegistration } from "shiki";
|
|
2
|
+
/** Lower-cased language name or alias mapped to its registration. */
|
|
3
|
+
export type CustomLanguageIndex = ReadonlyMap<string, LanguageRegistration>;
|
|
4
|
+
/**
|
|
5
|
+
* Validate `shiki.customLangs` and return the index used for lookups.
|
|
6
|
+
* Throws a `[folio]` error describing the first invalid registration.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createCustomLanguageIndex(languages: unknown): CustomLanguageIndex;
|
|
9
|
+
/** Throws a `[folio]` configuration error when `shiki.customLangs` is invalid. */
|
|
10
|
+
export declare function validateCustomLanguages(languages: unknown): void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { RESERVED_LANGUAGE_IDS } from "./builtin-languages.js";
|
|
2
|
+
function fail(index, message) {
|
|
3
|
+
throw new Error(`[folio] shiki.customLangs[${index}] ${message}`);
|
|
4
|
+
}
|
|
5
|
+
function readIdentifier(value, index, label) {
|
|
6
|
+
if (typeof value !== "string" || !value.trim())
|
|
7
|
+
fail(index, `${label} must be a non-empty string`);
|
|
8
|
+
const id = value.trim().toLowerCase();
|
|
9
|
+
if (/\s/.test(id))
|
|
10
|
+
fail(index, `${label} "${value}" must not contain whitespace`);
|
|
11
|
+
return id;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Validate `shiki.customLangs` and return the index used for lookups.
|
|
15
|
+
* Throws a `[folio]` error describing the first invalid registration.
|
|
16
|
+
*/
|
|
17
|
+
export function createCustomLanguageIndex(languages) {
|
|
18
|
+
const index = new Map();
|
|
19
|
+
if (languages === undefined)
|
|
20
|
+
return index;
|
|
21
|
+
if (!Array.isArray(languages))
|
|
22
|
+
throw new Error("[folio] shiki.customLangs must be an array of Shiki language registrations");
|
|
23
|
+
const scopes = new Set();
|
|
24
|
+
languages.forEach((entry, position) => {
|
|
25
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
26
|
+
fail(position, "must be a language registration object");
|
|
27
|
+
const language = entry;
|
|
28
|
+
const name = readIdentifier(language.name, position, "name");
|
|
29
|
+
if (typeof language.scopeName !== "string" || !language.scopeName.trim())
|
|
30
|
+
fail(position, "scopeName must be a non-empty string");
|
|
31
|
+
if (!Array.isArray(language.patterns))
|
|
32
|
+
fail(position, `("${language.name}") must define a patterns array`);
|
|
33
|
+
if (language.aliases !== undefined && !Array.isArray(language.aliases))
|
|
34
|
+
fail(position, `("${language.name}") aliases must be an array of strings`);
|
|
35
|
+
if (scopes.has(language.scopeName))
|
|
36
|
+
fail(position, `("${language.name}") scopeName "${language.scopeName}" is registered more than once`);
|
|
37
|
+
scopes.add(language.scopeName);
|
|
38
|
+
const ids = new Set([name, ...(language.aliases ?? []).map((alias) => readIdentifier(alias, position, "alias"))]);
|
|
39
|
+
for (const id of ids) {
|
|
40
|
+
if (RESERVED_LANGUAGE_IDS.has(id))
|
|
41
|
+
fail(position, `("${language.name}") uses "${id}", which is a built-in language or alias`);
|
|
42
|
+
if (index.has(id))
|
|
43
|
+
fail(position, `("${language.name}") uses "${id}", which is already registered by "${index.get(id).name}"`);
|
|
44
|
+
index.set(id, language);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return index;
|
|
48
|
+
}
|
|
49
|
+
/** Throws a `[folio]` configuration error when `shiki.customLangs` is invalid. */
|
|
50
|
+
export function validateCustomLanguages(languages) {
|
|
51
|
+
createCustomLanguageIndex(languages);
|
|
52
|
+
}
|
|
@@ -4,7 +4,8 @@ export declare function configureDocsHighlighter(config: ShikiConfig): void;
|
|
|
4
4
|
export declare function getDocsHighlighter(config?: ShikiConfig): Promise<Highlighter>;
|
|
5
5
|
/**
|
|
6
6
|
* Ensures a language is loaded in Shiki on-demand.
|
|
7
|
-
*
|
|
7
|
+
* Custom languages from `shiki.customLangs` are matched first by name or alias;
|
|
8
|
+
* otherwise a bundled language is loaded dynamically.
|
|
8
9
|
* Returns the resolved language identifier or "text" as fallback.
|
|
9
10
|
*/
|
|
10
11
|
export declare function ensureLanguage(highlighter: Highlighter, rawLang: string): Promise<string>;
|
package/dist/mdx/highlighter.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { createBundledHighlighter } from "shiki/core";
|
|
2
2
|
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
3
|
+
import { languageLoaders, normalizeLangAlias } from "./builtin-languages.js";
|
|
4
|
+
import { createCustomLanguageIndex } from "./custom-languages.js";
|
|
3
5
|
let highlighterPromise = null;
|
|
4
6
|
let activeShikiConfig = null;
|
|
5
7
|
let activeShikiConfigKey = null;
|
|
6
8
|
let activeAllowedLanguages = null;
|
|
9
|
+
let activeCustomLanguages = new Map();
|
|
7
10
|
const DEFAULT_BASE_LANGS = [
|
|
8
11
|
"typescript",
|
|
9
12
|
"javascript",
|
|
@@ -22,30 +25,6 @@ const DEFAULT_BASE_LANGS = [
|
|
|
22
25
|
"sql",
|
|
23
26
|
"diff",
|
|
24
27
|
];
|
|
25
|
-
const languageLoaders = {
|
|
26
|
-
typescript: () => import("@shikijs/langs/typescript"),
|
|
27
|
-
javascript: () => import("@shikijs/langs/javascript"),
|
|
28
|
-
tsx: () => import("@shikijs/langs/tsx"),
|
|
29
|
-
jsx: () => import("@shikijs/langs/jsx"),
|
|
30
|
-
bash: () => import("@shikijs/langs/shellscript"),
|
|
31
|
-
json: () => import("@shikijs/langs/json"),
|
|
32
|
-
css: () => import("@shikijs/langs/css"),
|
|
33
|
-
html: () => import("@shikijs/langs/html"),
|
|
34
|
-
markdown: () => import("@shikijs/langs/markdown"),
|
|
35
|
-
mdx: () => import("@shikijs/langs/mdx"),
|
|
36
|
-
rust: () => import("@shikijs/langs/rust"),
|
|
37
|
-
python: () => import("@shikijs/langs/python"),
|
|
38
|
-
yaml: () => import("@shikijs/langs/yaml"),
|
|
39
|
-
toml: () => import("@shikijs/langs/toml"),
|
|
40
|
-
sql: () => import("@shikijs/langs/sql"),
|
|
41
|
-
diff: () => import("@shikijs/langs/diff"),
|
|
42
|
-
c: () => import("@shikijs/langs/c"),
|
|
43
|
-
cpp: () => import("@shikijs/langs/cpp"),
|
|
44
|
-
go: () => import("@shikijs/langs/go"),
|
|
45
|
-
mermaid: () => import("@shikijs/langs/mermaid"),
|
|
46
|
-
dockerfile: () => import("@shikijs/langs/docker"),
|
|
47
|
-
zig: () => import("@shikijs/langs/zig"),
|
|
48
|
-
};
|
|
49
28
|
const DEFAULT_LIGHT_THEME = "github-light";
|
|
50
29
|
const DEFAULT_DARK_THEME = "github-dark";
|
|
51
30
|
const themeLoaders = {
|
|
@@ -58,6 +37,7 @@ const createDocsHighlighter = createBundledHighlighter({
|
|
|
58
37
|
engine: () => createJavaScriptRegexEngine(),
|
|
59
38
|
});
|
|
60
39
|
export function configureDocsHighlighter(config) {
|
|
40
|
+
activeCustomLanguages = createCustomLanguageIndex(config.customLangs);
|
|
61
41
|
activeShikiConfig = config;
|
|
62
42
|
activeShikiConfigKey = JSON.stringify(config);
|
|
63
43
|
activeAllowedLanguages = config.langs
|
|
@@ -85,11 +65,25 @@ export async function getDocsHighlighter(config) {
|
|
|
85
65
|
}
|
|
86
66
|
/**
|
|
87
67
|
* Ensures a language is loaded in Shiki on-demand.
|
|
88
|
-
*
|
|
68
|
+
* Custom languages from `shiki.customLangs` are matched first by name or alias;
|
|
69
|
+
* otherwise a bundled language is loaded dynamically.
|
|
89
70
|
* Returns the resolved language identifier or "text" as fallback.
|
|
90
71
|
*/
|
|
91
72
|
export async function ensureLanguage(highlighter, rawLang) {
|
|
92
73
|
const normalized = normalizeLangAlias(rawLang);
|
|
74
|
+
const custom = activeCustomLanguages.get(normalized);
|
|
75
|
+
if (custom) {
|
|
76
|
+
if (highlighter.getLoadedLanguages().includes(custom.name))
|
|
77
|
+
return custom.name;
|
|
78
|
+
try {
|
|
79
|
+
await highlighter.loadLanguage(custom);
|
|
80
|
+
return custom.name;
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.warn(`[folio] Failed to load custom Shiki language "${custom.name}":`, err);
|
|
84
|
+
return "text";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
93
87
|
if (!normalized || normalized === "text" || normalized === "txt" || normalized === "plain") {
|
|
94
88
|
return "text";
|
|
95
89
|
}
|
|
@@ -141,33 +135,6 @@ export async function ensureTheme(highlighter, themeName) {
|
|
|
141
135
|
}
|
|
142
136
|
return DEFAULT_DARK_THEME;
|
|
143
137
|
}
|
|
144
|
-
function normalizeLangAlias(lang) {
|
|
145
|
-
const l = lang.toLowerCase().trim();
|
|
146
|
-
switch (l) {
|
|
147
|
-
case "shell":
|
|
148
|
-
case "sh":
|
|
149
|
-
case "zsh":
|
|
150
|
-
return "bash";
|
|
151
|
-
case "js":
|
|
152
|
-
return "javascript";
|
|
153
|
-
case "ts":
|
|
154
|
-
return "typescript";
|
|
155
|
-
case "py":
|
|
156
|
-
return "python";
|
|
157
|
-
case "rs":
|
|
158
|
-
return "rust";
|
|
159
|
-
case "yml":
|
|
160
|
-
return "yaml";
|
|
161
|
-
case "md":
|
|
162
|
-
return "markdown";
|
|
163
|
-
case "rb":
|
|
164
|
-
return "ruby";
|
|
165
|
-
case "cs":
|
|
166
|
-
return "csharp";
|
|
167
|
-
default:
|
|
168
|
-
return l;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
138
|
export async function highlightCode(code, options = {}) {
|
|
172
139
|
const highlighter = await getDocsHighlighter();
|
|
173
140
|
const rawLang = options.lang || "text";
|
|
@@ -12,6 +12,8 @@ export declare const VIRTUAL_ICONS_ID = "virtual:folio-icons";
|
|
|
12
12
|
export declare const RESOLVED_ICONS_ID: string;
|
|
13
13
|
export declare const VIRTUAL_THEME_ID = "virtual:folio-theme";
|
|
14
14
|
export declare const RESOLVED_THEME_ID: string;
|
|
15
|
+
export declare const VIRTUAL_HIGHLIGHT_LANGUAGES_ID = "virtual:folio-highlight-languages";
|
|
16
|
+
export declare const RESOLVED_HIGHLIGHT_LANGUAGES_ID: string;
|
|
15
17
|
export declare const VIRTUAL_SHIKI_ID = "virtual:folio-shiki-stub";
|
|
16
18
|
export declare const RESOLVED_SHIKI_ID: string;
|
|
17
19
|
export declare const CONFIG_FILENAMES: Set<string>;
|
|
@@ -12,6 +12,8 @@ export const VIRTUAL_ICONS_ID = "virtual:folio-icons";
|
|
|
12
12
|
export const RESOLVED_ICONS_ID = "\0" + VIRTUAL_ICONS_ID;
|
|
13
13
|
export const VIRTUAL_THEME_ID = "virtual:folio-theme";
|
|
14
14
|
export const RESOLVED_THEME_ID = "\0" + VIRTUAL_THEME_ID;
|
|
15
|
+
export const VIRTUAL_HIGHLIGHT_LANGUAGES_ID = "virtual:folio-highlight-languages";
|
|
16
|
+
export const RESOLVED_HIGHLIGHT_LANGUAGES_ID = "\0" + VIRTUAL_HIGHLIGHT_LANGUAGES_ID;
|
|
15
17
|
export const VIRTUAL_SHIKI_ID = "virtual:folio-shiki-stub";
|
|
16
18
|
export const RESOLVED_SHIKI_ID = "\0" + VIRTUAL_SHIKI_ID;
|
|
17
19
|
export const CONFIG_FILENAMES = new Set([
|
|
@@ -4,7 +4,7 @@ import { compileMdx } from "../../mdx/compiler.js";
|
|
|
4
4
|
import { loadConfig } from "../../config.js";
|
|
5
5
|
import { resolveDefaultThemeMode } from "../../theme-mode.js";
|
|
6
6
|
import { createFolioBuildSession } from "./lifecycle.js";
|
|
7
|
-
import { CONFIG_FILENAMES, RESOLVED_CONFIG_ID, RESOLVED_ICONS_ID, RESOLVED_ROUTES_ID, RESOLVED_SHIKI_ID, RESOLVED_SOURCES_ID, RESOLVED_THEME_ID, RESOLVED_TREE_ID, VIRTUAL_CONFIG_ID, VIRTUAL_COMPONENTS_ID, VIRTUAL_ICONS_ID, VIRTUAL_ROUTES_ID, VIRTUAL_SOURCES_ID, VIRTUAL_THEME_ID, VIRTUAL_TREE_ID, } from "./constants.js";
|
|
7
|
+
import { CONFIG_FILENAMES, RESOLVED_CONFIG_ID, RESOLVED_HIGHLIGHT_LANGUAGES_ID, RESOLVED_ICONS_ID, RESOLVED_ROUTES_ID, RESOLVED_SHIKI_ID, RESOLVED_SOURCES_ID, RESOLVED_THEME_ID, RESOLVED_TREE_ID, VIRTUAL_CONFIG_ID, VIRTUAL_COMPONENTS_ID, VIRTUAL_HIGHLIGHT_LANGUAGES_ID, VIRTUAL_ICONS_ID, VIRTUAL_ROUTES_ID, VIRTUAL_SOURCES_ID, VIRTUAL_THEME_ID, VIRTUAL_TREE_ID, } from "./constants.js";
|
|
8
8
|
import { escapeHtmlAttribute, getServerModuleDir, getTailwindSourceDirectives } from "./styles.js";
|
|
9
9
|
import { createVirtualModuleLoader } from "./virtual-modules.js";
|
|
10
10
|
import { getComponentSourceDir } from "./components.js";
|
|
@@ -64,6 +64,7 @@ export function nikalaDocsPlugin(options = {}) {
|
|
|
64
64
|
[VIRTUAL_ROUTES_ID]: RESOLVED_ROUTES_ID, [VIRTUAL_SOURCES_ID]: RESOLVED_SOURCES_ID,
|
|
65
65
|
[VIRTUAL_COMPONENTS_ID]: "\0" + VIRTUAL_COMPONENTS_ID, [VIRTUAL_ICONS_ID]: RESOLVED_ICONS_ID,
|
|
66
66
|
[RESOLVED_ICONS_ID]: RESOLVED_ICONS_ID, [VIRTUAL_THEME_ID]: RESOLVED_THEME_ID,
|
|
67
|
+
[VIRTUAL_HIGHLIGHT_LANGUAGES_ID]: RESOLVED_HIGHLIGHT_LANGUAGES_ID,
|
|
67
68
|
};
|
|
68
69
|
return virtualIds[id] || null;
|
|
69
70
|
},
|
|
@@ -5,7 +5,7 @@ import { scanContentDirectories } from "../../core/content-scanner.js";
|
|
|
5
5
|
import { buildConfiguredSidebarTree, buildSidebarTree } from "../../core/route-tree.js";
|
|
6
6
|
import { serializeModuleValue } from "../module-serialization.js";
|
|
7
7
|
import { collectComponentExports, collectStaticComponentFiles, getComponentSourceDir, toLucideComponentName } from "./components.js";
|
|
8
|
-
import { RESOLVED_COMPONENTS_ID, RESOLVED_CONFIG_ID, RESOLVED_ICONS_ID, RESOLVED_ROUTES_ID, RESOLVED_SHIKI_ID, RESOLVED_SOURCES_ID, RESOLVED_THEME_ID, RESOLVED_TREE_ID, } from "./constants.js";
|
|
8
|
+
import { RESOLVED_COMPONENTS_ID, RESOLVED_CONFIG_ID, RESOLVED_HIGHLIGHT_LANGUAGES_ID, RESOLVED_ICONS_ID, RESOLVED_ROUTES_ID, RESOLVED_SHIKI_ID, RESOLVED_SOURCES_ID, RESOLVED_THEME_ID, RESOLVED_TREE_ID, } from "./constants.js";
|
|
9
9
|
function getSearchAdapterRuntime(provider) {
|
|
10
10
|
if (!provider || typeof provider !== "object" || !provider.runtime)
|
|
11
11
|
return undefined;
|
|
@@ -36,8 +36,14 @@ export const bundledLanguages = {};
|
|
|
36
36
|
export const bundledThemes = {};
|
|
37
37
|
export default { createHighlighter, bundledLanguages, bundledThemes };
|
|
38
38
|
`;
|
|
39
|
+
if (id === RESOLVED_HIGHLIGHT_LANGUAGES_ID) {
|
|
40
|
+
return `export default ${serializeModuleValue(resolvedConfig.shiki?.customLangs ?? [])};`;
|
|
41
|
+
}
|
|
39
42
|
if (id === RESOLVED_CONFIG_ID) {
|
|
40
|
-
|
|
43
|
+
// Grammars load lazily through virtual:folio-highlight-languages, so keep
|
|
44
|
+
// them out of the eagerly loaded client config.
|
|
45
|
+
const { customLangs: _customLangs, ...clientShiki } = resolvedConfig.shiki ?? {};
|
|
46
|
+
const serializedConfig = serializeModuleValue(resolvedConfig.shiki ? { ...resolvedConfig, shiki: clientShiki } : resolvedConfig);
|
|
41
47
|
const runtime = getSearchAdapterRuntime(resolvedConfig.search?.provider);
|
|
42
48
|
if (!runtime)
|
|
43
49
|
return `export default ${serializedConfig};`;
|
package/dist/templates/env.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ declare module "virtual:folio-config" {
|
|
|
7
7
|
export default config;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
declare module "virtual:folio-highlight-languages" {
|
|
11
|
+
const languages: Array<{ name: string; aliases?: string[]; [key: string]: unknown }>;
|
|
12
|
+
export default languages;
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
declare module "virtual:folio-tree" {
|
|
11
16
|
export const pages: any[];
|
|
12
17
|
export const tree: any[];
|
package/dist/types.d.ts
CHANGED
|
@@ -113,7 +113,16 @@ export interface ShikiConfig {
|
|
|
113
113
|
light?: string;
|
|
114
114
|
dark?: string;
|
|
115
115
|
};
|
|
116
|
+
/** Allowlist of built-in language ids. Custom languages are not affected. */
|
|
116
117
|
langs?: string[];
|
|
118
|
+
/**
|
|
119
|
+
* Additional languages as Shiki language registrations (TextMate grammars
|
|
120
|
+
* with `name`, `scopeName`, `patterns` and optional `aliases`). Code fences
|
|
121
|
+
* whose language matches a `name` or alias use the grammar. Names and
|
|
122
|
+
* aliases must not collide with built-in languages or each other. Grammars
|
|
123
|
+
* must be plain JSON data and are loaded lazily.
|
|
124
|
+
*/
|
|
125
|
+
customLangs?: import("shiki").LanguageRegistration[];
|
|
117
126
|
}
|
|
118
127
|
export interface DocsSeoConfig {
|
|
119
128
|
/** Social preview image path or URL. */
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Pure helpers that format and highlight CLI commands for CodeBlock's package manager runner.
|
|
2
|
+
|
|
3
|
+
export type CodeBlockPackageManager = "bunx" | "npx" | "pnpm" | "yarn";
|
|
4
|
+
|
|
5
|
+
export function highlightCliCommand(code: string): string {
|
|
6
|
+
const trimmed = code.trim();
|
|
7
|
+
const parts = trimmed.split(/\s+/);
|
|
8
|
+
if (parts.length === 0) return code;
|
|
9
|
+
|
|
10
|
+
const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
|
|
11
|
+
if (!isRunner) return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
|
|
12
|
+
|
|
13
|
+
return parts
|
|
14
|
+
.map((part, index) => {
|
|
15
|
+
if (index === 0) {
|
|
16
|
+
return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
|
|
17
|
+
}
|
|
18
|
+
if (part.startsWith("-")) {
|
|
19
|
+
return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
|
|
20
|
+
}
|
|
21
|
+
if (part.startsWith("@")) {
|
|
22
|
+
return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
|
|
23
|
+
}
|
|
24
|
+
if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
|
|
25
|
+
return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
|
|
26
|
+
}
|
|
27
|
+
return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
|
|
28
|
+
})
|
|
29
|
+
.join(" ");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function transformCommandForPm(cmd: string, pm: CodeBlockPackageManager): string {
|
|
33
|
+
const trimmed = cmd.trim();
|
|
34
|
+
const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
|
|
35
|
+
|
|
36
|
+
let base = trimmed;
|
|
37
|
+
for (const r of runners) {
|
|
38
|
+
if (trimmed.startsWith(r)) {
|
|
39
|
+
base = trimmed.slice(r.length).trim();
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
switch (pm) {
|
|
45
|
+
case "bunx":
|
|
46
|
+
return `bunx ${base}`;
|
|
47
|
+
case "npx":
|
|
48
|
+
return `npx ${base}`;
|
|
49
|
+
case "pnpm":
|
|
50
|
+
return `pnpm dlx ${base}`;
|
|
51
|
+
case "yarn":
|
|
52
|
+
return `yarn dlx ${base}`;
|
|
53
|
+
default:
|
|
54
|
+
return `${pm} ${base}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function escapeHtml(str: string): string {
|
|
59
|
+
return str
|
|
60
|
+
.replace(/&/g, "&")
|
|
61
|
+
.replace(/</g, "<")
|
|
62
|
+
.replace(/>/g, ">")
|
|
63
|
+
.replace(/"/g, """)
|
|
64
|
+
.replace(/'/g, "'");
|
|
65
|
+
}
|
|
@@ -14,10 +14,12 @@ import { Badge } from "@/components/ui/badge";
|
|
|
14
14
|
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
|
15
15
|
import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
|
|
16
16
|
import { createClipboard } from "@/hooks/create-clipboard";
|
|
17
|
+
import { highlightOnClient } from "./code-highlighter";
|
|
18
|
+
import { escapeHtml, highlightCliCommand, transformCommandForPm, type CodeBlockPackageManager } from "./code-block-commands";
|
|
17
19
|
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
18
20
|
import { cn } from "@/lib/cn";
|
|
19
21
|
|
|
20
|
-
export type CodeBlockPackageManager
|
|
22
|
+
export type { CodeBlockPackageManager };
|
|
21
23
|
|
|
22
24
|
export interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
23
25
|
/** The raw source code text to display and copy */
|
|
@@ -39,118 +41,6 @@ export interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
|
39
41
|
class?: string;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
let shikiHighlighterPromise: Promise<any> | null = null;
|
|
43
|
-
|
|
44
|
-
async function getShikiHighlighter() {
|
|
45
|
-
if (typeof window === "undefined") return null;
|
|
46
|
-
if (!shikiHighlighterPromise) {
|
|
47
|
-
try {
|
|
48
|
-
const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
|
|
49
|
-
import("shiki/core"),
|
|
50
|
-
import("shiki/engine/javascript"),
|
|
51
|
-
]);
|
|
52
|
-
const createHighlighter = createBundledHighlighter({
|
|
53
|
-
langs: {
|
|
54
|
-
typescript: () => import("@shikijs/langs/typescript"),
|
|
55
|
-
javascript: () => import("@shikijs/langs/javascript"),
|
|
56
|
-
tsx: () => import("@shikijs/langs/tsx"),
|
|
57
|
-
jsx: () => import("@shikijs/langs/jsx"),
|
|
58
|
-
bash: () => import("@shikijs/langs/shellscript"),
|
|
59
|
-
json: () => import("@shikijs/langs/json"),
|
|
60
|
-
css: () => import("@shikijs/langs/css"),
|
|
61
|
-
html: () => import("@shikijs/langs/html"),
|
|
62
|
-
rust: () => import("@shikijs/langs/rust"),
|
|
63
|
-
},
|
|
64
|
-
themes: {
|
|
65
|
-
"github-dark": () => import("@shikijs/themes/github-dark"),
|
|
66
|
-
"github-light": () => import("@shikijs/themes/github-light"),
|
|
67
|
-
},
|
|
68
|
-
engine: () => createJavaScriptRegexEngine(),
|
|
69
|
-
});
|
|
70
|
-
shikiHighlighterPromise = createHighlighter({
|
|
71
|
-
themes: ["github-dark", "github-light"],
|
|
72
|
-
langs: [
|
|
73
|
-
"typescript",
|
|
74
|
-
"javascript",
|
|
75
|
-
"tsx",
|
|
76
|
-
"jsx",
|
|
77
|
-
"bash",
|
|
78
|
-
"json",
|
|
79
|
-
"css",
|
|
80
|
-
"html",
|
|
81
|
-
"rust",
|
|
82
|
-
],
|
|
83
|
-
});
|
|
84
|
-
} catch (e) {
|
|
85
|
-
console.warn("Failed to load Shiki highlighter", e);
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return shikiHighlighterPromise;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function highlightCliCommand(code: string): string {
|
|
93
|
-
const trimmed = code.trim();
|
|
94
|
-
const parts = trimmed.split(/\s+/);
|
|
95
|
-
if (parts.length === 0) return code;
|
|
96
|
-
|
|
97
|
-
const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
|
|
98
|
-
if (!isRunner) return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
|
|
99
|
-
|
|
100
|
-
return parts
|
|
101
|
-
.map((part, index) => {
|
|
102
|
-
if (index === 0) {
|
|
103
|
-
return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
|
|
104
|
-
}
|
|
105
|
-
if (part.startsWith("-")) {
|
|
106
|
-
return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
|
|
107
|
-
}
|
|
108
|
-
if (part.startsWith("@")) {
|
|
109
|
-
return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
|
|
110
|
-
}
|
|
111
|
-
if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
|
|
112
|
-
return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
|
|
113
|
-
}
|
|
114
|
-
return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
|
|
115
|
-
})
|
|
116
|
-
.join(" ");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function transformCommandForPm(cmd: string, pm: CodeBlockPackageManager): string {
|
|
120
|
-
const trimmed = cmd.trim();
|
|
121
|
-
const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
|
|
122
|
-
|
|
123
|
-
let base = trimmed;
|
|
124
|
-
for (const r of runners) {
|
|
125
|
-
if (trimmed.startsWith(r)) {
|
|
126
|
-
base = trimmed.slice(r.length).trim();
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
switch (pm) {
|
|
132
|
-
case "bunx":
|
|
133
|
-
return `bunx ${base}`;
|
|
134
|
-
case "npx":
|
|
135
|
-
return `npx ${base}`;
|
|
136
|
-
case "pnpm":
|
|
137
|
-
return `pnpm dlx ${base}`;
|
|
138
|
-
case "yarn":
|
|
139
|
-
return `yarn dlx ${base}`;
|
|
140
|
-
default:
|
|
141
|
-
return `${pm} ${base}`;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function escapeHtml(str: string): string {
|
|
146
|
-
return str
|
|
147
|
-
.replace(/&/g, "&")
|
|
148
|
-
.replace(/</g, "<")
|
|
149
|
-
.replace(/>/g, ">")
|
|
150
|
-
.replace(/"/g, """)
|
|
151
|
-
.replace(/'/g, "'");
|
|
152
|
-
}
|
|
153
|
-
|
|
154
44
|
export const CodeBlock: ParentComponent<CodeBlockProps> = (props) => {
|
|
155
45
|
const siteLocale = useSiteLocale();
|
|
156
46
|
const [local, rest] = splitProps(props, [
|
|
@@ -203,25 +93,8 @@ export const CodeBlock: ParentComponent<CodeBlockProps> = (props) => {
|
|
|
203
93
|
return;
|
|
204
94
|
}
|
|
205
95
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (highlighter) {
|
|
209
|
-
try {
|
|
210
|
-
const html = highlighter.codeToHtml(codeStr, {
|
|
211
|
-
lang: lang === "js" ? "javascript" : lang === "ts" ? "typescript" : lang,
|
|
212
|
-
themes: {
|
|
213
|
-
light: "github-light",
|
|
214
|
-
dark: "github-dark",
|
|
215
|
-
},
|
|
216
|
-
});
|
|
217
|
-
setHighlightedHtml(html);
|
|
218
|
-
return;
|
|
219
|
-
} catch (err) {
|
|
220
|
-
console.warn(`Shiki failed for language: ${lang}`, err);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
// Fallback
|
|
224
|
-
setHighlightedHtml(`<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
|
|
96
|
+
highlightOnClient(codeStr, lang).then((html) => {
|
|
97
|
+
setHighlightedHtml(html ?? `<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
|
|
225
98
|
});
|
|
226
99
|
});
|
|
227
100
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Browser-side Shiki highlighting for CodeBlock usages without server-rendered children.
|
|
2
|
+
let highlighterPromise: Promise<any> | null = null;
|
|
3
|
+
|
|
4
|
+
async function getHighlighter() {
|
|
5
|
+
if (typeof window === "undefined") return null;
|
|
6
|
+
if (!highlighterPromise) {
|
|
7
|
+
try {
|
|
8
|
+
const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
|
|
9
|
+
import("shiki/core"),
|
|
10
|
+
import("shiki/engine/javascript"),
|
|
11
|
+
]);
|
|
12
|
+
const createHighlighter = createBundledHighlighter({
|
|
13
|
+
langs: {
|
|
14
|
+
typescript: () => import("@shikijs/langs/typescript"),
|
|
15
|
+
javascript: () => import("@shikijs/langs/javascript"),
|
|
16
|
+
tsx: () => import("@shikijs/langs/tsx"),
|
|
17
|
+
jsx: () => import("@shikijs/langs/jsx"),
|
|
18
|
+
bash: () => import("@shikijs/langs/shellscript"),
|
|
19
|
+
json: () => import("@shikijs/langs/json"),
|
|
20
|
+
css: () => import("@shikijs/langs/css"),
|
|
21
|
+
html: () => import("@shikijs/langs/html"),
|
|
22
|
+
rust: () => import("@shikijs/langs/rust"),
|
|
23
|
+
},
|
|
24
|
+
themes: {
|
|
25
|
+
"github-dark": () => import("@shikijs/themes/github-dark"),
|
|
26
|
+
"github-light": () => import("@shikijs/themes/github-light"),
|
|
27
|
+
},
|
|
28
|
+
engine: () => createJavaScriptRegexEngine(),
|
|
29
|
+
});
|
|
30
|
+
highlighterPromise = createHighlighter({
|
|
31
|
+
themes: ["github-dark", "github-light"],
|
|
32
|
+
langs: ["typescript", "javascript", "tsx", "jsx", "bash", "json", "css", "html", "rust"],
|
|
33
|
+
});
|
|
34
|
+
} catch (e) {
|
|
35
|
+
console.warn("Failed to load Shiki highlighter", e);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return highlighterPromise;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve a language from the site's `shiki.customLangs`. The grammar module is
|
|
44
|
+
* a separate lazy chunk, fetched only for languages the highlighter lacks.
|
|
45
|
+
*/
|
|
46
|
+
async function loadCustomLanguage(highlighter: any, lang: string): Promise<string | null> {
|
|
47
|
+
try {
|
|
48
|
+
const { default: languages } = await import("virtual:folio-highlight-languages");
|
|
49
|
+
const match = languages.find((entry) =>
|
|
50
|
+
[entry.name, ...(entry.aliases ?? [])].some((id) => id.toLowerCase() === lang),
|
|
51
|
+
);
|
|
52
|
+
if (!match) return null;
|
|
53
|
+
await highlighter.loadLanguage(match);
|
|
54
|
+
return match.name;
|
|
55
|
+
} catch (err) {
|
|
56
|
+
console.warn(`Failed to load custom Shiki language: ${lang}`, err);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Highlight code in the browser, or return null when the caller should fall back to plain text. */
|
|
62
|
+
export async function highlightOnClient(code: string, rawLang: string): Promise<string | null> {
|
|
63
|
+
const highlighter = await getHighlighter();
|
|
64
|
+
if (!highlighter) return null;
|
|
65
|
+
|
|
66
|
+
const requested = rawLang === "js" ? "javascript" : rawLang === "ts" ? "typescript" : rawLang;
|
|
67
|
+
const lang = highlighter.getLoadedLanguages().includes(requested)
|
|
68
|
+
? requested
|
|
69
|
+
: (await loadCustomLanguage(highlighter, requested.toLowerCase())) ?? requested;
|
|
70
|
+
try {
|
|
71
|
+
return highlighter.codeToHtml(code, { lang, themes: { light: "github-light", dark: "github-dark" } });
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.warn(`Shiki failed for language: ${rawLang}`, err);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const FOLIO_VERSION = "0.
|
|
1
|
+
export declare const FOLIO_VERSION = "0.16.0";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const FOLIO_VERSION = "0.
|
|
1
|
+
export const FOLIO_VERSION = "0.16.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nikala-ui/folio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Zero-config, fast, and elegant documentation engine for SolidJS, built on Nikala UI and Tailwind CSS v4",
|
|
6
6
|
"type": "module",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"@nikala-ui/folio-algolia": "0.3.0",
|
|
103
103
|
"@types/fs-extra": "^11.0.4",
|
|
104
104
|
"@types/node": "^26.1.1",
|
|
105
|
-
"bun-types": "1.
|
|
105
|
+
"bun-types": "1.4.2",
|
|
106
106
|
"playwright": "1.63.0",
|
|
107
107
|
"solid-js": "^1.9.14",
|
|
108
108
|
"tailwindcss": "^4.0.7",
|