mirror of
https://github.com/litruv/Docs-Viewer.git
synced 2026-09-10 18:19: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:
73
src/index.js
Normal file
73
src/index.js
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* 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,
|
||||
};
|
||||
Reference in New Issue
Block a user