feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { createMarkdownRenderer } from "vitepress";
|
||||
import type { Plugin } from "vite";
|
||||
|
||||
// Build both highlights and the local search index from the actual documentation.
|
||||
export function docsDataPlugin(root: string): Plugin {
|
||||
const id = "\0virtual:docs-data";
|
||||
let renderer: ReturnType<typeof createMarkdownRenderer>;
|
||||
return {
|
||||
name: "heroui-docs-data",
|
||||
resolveId(source) {
|
||||
if (source === "virtual:docs-data") return id;
|
||||
if (/^virtual:docs-highlight\/[a-z0-9-]+$/.test(source))
|
||||
return "\0" + source;
|
||||
},
|
||||
async load(source) {
|
||||
if (source !== id && !source.startsWith("\0virtual:docs-highlight/"))
|
||||
return;
|
||||
renderer ??= createMarkdownRenderer(root, {
|
||||
theme: { light: "github-light", dark: "github-dark" },
|
||||
headers: { level: [2, 3] },
|
||||
});
|
||||
const md = await renderer;
|
||||
if (source.startsWith("\0virtual:docs-highlight/")) {
|
||||
const name = source.slice("\0virtual:docs-highlight/".length);
|
||||
const file = path.join(root, "demos", name + ".vue");
|
||||
this.addWatchFile(file);
|
||||
return (
|
||||
"export default " +
|
||||
JSON.stringify(
|
||||
md.render("```vue\n" + (await readFile(file, "utf8")) + "\n```"),
|
||||
)
|
||||
);
|
||||
}
|
||||
const highlights: string[] = [];
|
||||
for (const name of await readdir(path.join(root, "demos"))) {
|
||||
if (!name.endsWith(".vue")) continue;
|
||||
const file = path.join(root, "demos", name);
|
||||
this.addWatchFile(file);
|
||||
const key = name.slice(0, -4);
|
||||
highlights.push(
|
||||
JSON.stringify(key) +
|
||||
":()=>import(" +
|
||||
JSON.stringify("virtual:docs-highlight/" + key) +
|
||||
")",
|
||||
);
|
||||
}
|
||||
const pages = [
|
||||
"introduction.md",
|
||||
"guide.md",
|
||||
"themes.md",
|
||||
...(await readdir(path.join(root, "components")))
|
||||
.filter((f) => f.endsWith(".md"))
|
||||
.map((f) => "components/" + f),
|
||||
];
|
||||
const search = [];
|
||||
const plain = (value: string) =>
|
||||
value
|
||||
.replace(/^[| :\-]+$/gm, " ")
|
||||
.replace(/<[^>]*>/g, " ")
|
||||
.replace(/[`*#|]/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
for (const name of pages) {
|
||||
const file = path.join(root, name);
|
||||
this.addWatchFile(file);
|
||||
const raw = await readFile(file, "utf8");
|
||||
const title = raw.match(/^title:\s*(.+)$/m)?.[1] ?? name;
|
||||
const description = raw.match(/^description:\s*(.+)$/m)?.[1] ?? "";
|
||||
const url = "/" + name.slice(0, -3);
|
||||
search.push({ title, section: "概览", url, text: description });
|
||||
const content = raw.replace(/^---[\s\S]*?---/, "");
|
||||
// The rendered IDs come from the same Markdown renderer as the real pages.
|
||||
const env: {
|
||||
headers?: { title: string; slug: string; children?: unknown[] }[];
|
||||
} = {};
|
||||
const rendered = md.render(content, env);
|
||||
const headings = [
|
||||
...rendered.matchAll(
|
||||
/<h([23]) id="([^"]+)"[^>]*>([\s\S]*?)<\/h[23]>/g,
|
||||
),
|
||||
];
|
||||
const chunks = content.split(/^#{2,3}\s+.+$/m).slice(1);
|
||||
headings.forEach((h, i) =>
|
||||
search.push({
|
||||
title,
|
||||
section: plain(h[3].replace(/<a[\s\S]*?<\/a>/g, "")),
|
||||
url: url + "#" + h[2],
|
||||
text: plain(chunks[i] ?? "").slice(0, 2000),
|
||||
}),
|
||||
);
|
||||
}
|
||||
return (
|
||||
"export const highlights=" +
|
||||
"{" +
|
||||
highlights.join(",") +
|
||||
"}" +
|
||||
";export const searchIndex=" +
|
||||
JSON.stringify(search) +
|
||||
";"
|
||||
);
|
||||
},
|
||||
handleHotUpdate(ctx) {
|
||||
if (/\.(md|vue)$/.test(ctx.file)) {
|
||||
const mod = ctx.server.moduleGraph.getModuleById(id);
|
||||
if (mod) {
|
||||
ctx.server.moduleGraph.invalidateModule(mod);
|
||||
for (const [key, highlight] of ctx.server.moduleGraph.idToModuleMap) {
|
||||
if (key.startsWith("\0virtual:docs-highlight/"))
|
||||
ctx.server.moduleGraph.invalidateModule(highlight);
|
||||
}
|
||||
ctx.server.ws.send({ type: "full-reload" });
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user