Files
Docs-Viewer/src/index.js
litruv 10e9f7ac2f 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.
2026-08-02 20:48:31 +10:00

74 lines
2.2 KiB
JavaScript

/**
* docs-viewer — documentation viewer on slatehtml + slatehtml-ui.
*
* import "slatehtml";
* import { mountDocs } from "docs-viewer";
* import "docs-viewer/prose.css";
*
* await mountDocs(document.querySelector("slate-docs-viewer"));
*/
import { deferCustomElementDefines } from "slatehtml/umc";
import { DocsApp, faClassToIconName } from "./app.js";
import { markdownToSpecs, collectHeadings } from "./markdown/render.js";
import { EventBus } from "./services/event-bus.js";
import { IndexService } from "./services/index-service.js";
import { SearchService } from "./services/search-service.js";
import { DocumentService } from "./services/document-service.js";
import { NavigationService } from "./services/navigation-service.js";
const modules = import.meta.glob(["./widgets/*.umc"]);
await deferCustomElementDefines(async () => {
await Promise.all(
Object.entries(modules).map(async ([path, load]) => {
try {
await load();
} catch (err) {
console.error(`[slatehtml-docs] failed to load ${path}`, err);
}
})
);
});
/**
* Mount / start the docs app on a <slate-docs-viewer> (or create one).
* @param {ParentNode|string|null} target
* @param {{ indexUrl?: string, create?: boolean }} [options]
* @returns {Promise<DocsApp>}
*/
export async function mountDocs(target = "slate-docs-viewer", options = {}) {
let host =
typeof target === "string" ? document.querySelector(target) : target;
if (!host && options.create !== false) {
host = document.createElement("slate-docs-viewer");
if (options.indexUrl) host.setAttribute("index", options.indexUrl);
document.body.appendChild(host);
}
if (!host) throw new Error("mountDocs: no slate-docs-viewer host found");
// Wait a frame so UMC Construct/stamp finishes.
await Promise.resolve();
await new Promise((r) => requestAnimationFrame(() => r()));
const app = new DocsApp(host, {
indexUrl: options.indexUrl || host.getAttribute("index") || "./index.json",
});
await app.start();
host.__docsApp = app;
return app;
}
export {
DocsApp,
faClassToIconName,
markdownToSpecs,
collectHeadings,
EventBus,
IndexService,
SearchService,
DocumentService,
NavigationService,
};