mirror of
https://github.com/litruv/Docs-Viewer.git
synced 2026-09-11 02:29:45 +10:00
Port Docs-Viewer to slatehtml as the docs-viewer package.
Replace the classic DOM shell with slate widgets, markdown→slate rendering, and a CI index build that uses build-docs.cjs.
This commit is contained in:
60
src/markdown/preprocess.js
Normal file
60
src/markdown/preprocess.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Frontmatter, wiki links, and wiki image embeds (Docs-Viewer compatible).
|
||||
*/
|
||||
|
||||
export function extractMetadata(content) {
|
||||
const lines = String(content || "").trim().split("\n");
|
||||
let metadata = {};
|
||||
let contentStart = 0;
|
||||
|
||||
if (lines[0]?.trim() === "---") {
|
||||
const endMetadata = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
|
||||
if (endMetadata !== -1) {
|
||||
const entries = lines
|
||||
.slice(1, endMetadata)
|
||||
.map((line) => line.match(/^([\w-]+):\s*(.*)$/))
|
||||
.filter(Boolean)
|
||||
.map(([, key, value]) => {
|
||||
if (key === "defaultOpen") {
|
||||
return [key, value.trim().toLowerCase() === "true"];
|
||||
}
|
||||
if (key === "sort") return [key, parseInt(value.trim(), 10)];
|
||||
return [key, value.trim()];
|
||||
});
|
||||
metadata = Object.fromEntries(entries);
|
||||
contentStart = endMetadata + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
metadata,
|
||||
content: lines.slice(contentStart).join("\n").trim(),
|
||||
};
|
||||
}
|
||||
|
||||
export function ensureTitle(content, title) {
|
||||
const stripped = String(content || "")
|
||||
.replace(/^#\s+.*$/m, "")
|
||||
.trim();
|
||||
return `# ${title}\n\n${stripped}`;
|
||||
}
|
||||
|
||||
export function processWikiLinks(content, indexService, documents) {
|
||||
return String(content || "").replace(/\[\[(.*?)\]\]/g, (match, linkText) => {
|
||||
const [targetTitle, displayText] = linkText.split("|").map((s) => s.trim());
|
||||
if (/\.(png|jpg|jpeg|gif|webp|mp4|webm)$/i.test(targetTitle)) return match;
|
||||
const doc = indexService.findDocumentByTitle(documents, targetTitle);
|
||||
if (!doc) return match;
|
||||
return `[${displayText || doc.title}](?${doc.slug})`;
|
||||
});
|
||||
}
|
||||
|
||||
export function processImages(content, _basePath) {
|
||||
return String(content || "").replace(/!\[\[(.*?)\]\]/g, (match, filename) => {
|
||||
const mediaPath = `./docs/images/${filename}`;
|
||||
if (filename.toLowerCase().endsWith(".mp4") || filename.toLowerCase().endsWith(".webm")) {
|
||||
return `\n\n<video src="${mediaPath}" controls playsinline></video>\n\n`;
|
||||
}
|
||||
return `\n\n\n\n`;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user