refactor(doc-data): Use static imports for doc entries

This commit is contained in:
hyper 2026-06-30 18:54:34 +08:00
parent d72d4e7bde
commit a05b0f4291
2 changed files with 59 additions and 47 deletions

View File

@ -27,52 +27,59 @@ function getBody(raw: string): string {
return match ? match[1] : raw;
}
/** Glob import all .md files in src/doc-entries/ */
const modules = import.meta.webpackContext("./", {
recursive: false,
regExp: /\.md$/,
});
/** Lazy-loaded doc entries, parsed on first access. */
let _entries: DocEntry[] | null = null;
export function loadDocEntries(): DocEntry[] {
if (_entries) return _entries;
const entries: DocEntry[] = [];
for (const key of modules.keys()) {
const mod = modules(key);
const raw =
typeof mod === "string"
? mod
: ((mod as { default?: string }).default ?? "");
if (!raw) continue;
function parseEntry(raw: string): DocEntry | null {
const fm = parseFrontmatter(raw);
if (!fm) continue;
const body = getBody(raw);
entries.push({
if (!fm) return null;
return {
tag: fm.tag as string,
icon: fm.icon as string,
title: fm.title as string,
description: fm.description as string,
syntax: fm.syntax as string,
props: (fm.props as DocEntry["props"]) ?? [],
body,
});
}
// Sort alphabetically by tag
entries.sort((a, b) => a.tag.localeCompare(b.tag));
_entries = entries;
return entries;
body: getBody(raw),
};
}
// Static imports each .md file is asset/source so imports are strings.
// Add a new import here when creating a new doc entry.
import mdDiceRaw from "../doc-entries/md-dice.md";
import mdTableRaw from "../doc-entries/md-table.md";
import mdLinkRaw from "../doc-entries/md-link.md";
import mdPinsRaw from "../doc-entries/md-pins.md";
import mdFontRaw from "../doc-entries/md-font.md";
import mdBgRaw from "../doc-entries/md-bg.md";
import mdBorderRaw from "../doc-entries/md-border.md";
import mdEmbedRaw from "../doc-entries/md-embed.md";
import mdDeckRaw from "../doc-entries/md-deck.md";
import mdYarnRaw from "../doc-entries/md-yarn-spinner.md";
import mdTokenRaw from "../doc-entries/md-token.md";
import mdTokenViewerRaw from "../doc-entries/md-token-viewer.md";
import mdCommanderRaw from "../doc-entries/md-commander.md";
const rawDocuments: string[] = [
mdDiceRaw,
mdTableRaw,
mdLinkRaw,
mdPinsRaw,
mdFontRaw,
mdBgRaw,
mdBorderRaw,
mdEmbedRaw,
mdDeckRaw,
mdYarnRaw,
mdTokenRaw,
mdTokenViewerRaw,
mdCommanderRaw,
];
let _entries: DocEntry[] | null = null;
function loadDocEntries(): DocEntry[] {
if (_entries) return _entries;
_entries = rawDocuments.map(parseEntry).filter(Boolean) as DocEntry[];
_entries.sort((a, b) => a.tag.localeCompare(b.tag));
return _entries;
}
/**
* Direct static array for the DocDialog sidebar uses webpackContext
* so all .md files in doc-entries/ are bundled automatically.
*/
export const docEntries = loadDocEntries();

7
src/global.d.ts vendored
View File

@ -11,6 +11,11 @@ interface ImportMeta {
options: {
recursive?: boolean;
regExp?: RegExp;
}
},
): WebpackContext;
}
declare module "*.md" {
const content: string;
export default content;
}