@dogsbay/format-docusaurus 0.2.0-beta.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Parse a Docusaurus sidebars module into NavItem[] (Mode 1 — explicit sidebar).
3
+ *
4
+ * A sidebars file exports one or more named sidebars:
5
+ * module.exports = { calicoSidebar: [ ...items ] }
6
+ *
7
+ * Item shapes (https://docusaurus.io/docs/sidebar/items):
8
+ * - 'doc/id' shorthand doc link
9
+ * - { type: 'doc', id, label? } doc link
10
+ * - { type: 'ref', id, label? } doc link (no sidebar membership)
11
+ * - { type: 'link', href, label } explicit/external link
12
+ * - { type: 'category', label, link?, items, ... } group
13
+ * - { type: 'autogenerated', dirName } expand from the filesystem
14
+ * - { type: 'html' } decorative — skipped
15
+ *
16
+ * Doc-id → href and doc-id → label resolution, plus autogenerated expansion,
17
+ * are injected via SidebarContext so this module stays free of filesystem I/O
18
+ * and is unit-testable.
19
+ */
20
+ import type { NavItem } from "@dogsbay/types";
21
+ export interface RawCategoryLink {
22
+ type: "doc" | "generated-index" | string;
23
+ id?: string;
24
+ title?: string;
25
+ permalink?: string;
26
+ }
27
+ export type RawSidebarItem = string | {
28
+ type: "doc";
29
+ id: string;
30
+ label?: string;
31
+ } | {
32
+ type: "ref";
33
+ id: string;
34
+ label?: string;
35
+ } | {
36
+ type: "link";
37
+ href: string;
38
+ label: string;
39
+ } | {
40
+ type: "category";
41
+ label: string;
42
+ link?: RawCategoryLink | null;
43
+ items: RawSidebarItem[];
44
+ collapsed?: boolean;
45
+ } | {
46
+ type: "autogenerated";
47
+ dirName: string;
48
+ } | {
49
+ type: "html";
50
+ value?: string;
51
+ } | Record<string, unknown>;
52
+ export type RawSidebars = Record<string, RawSidebarItem[]>;
53
+ export interface SidebarContext {
54
+ /** doc id → href (e.g. "about/index" → "/docs/about"). */
55
+ docHref: (id: string) => string;
56
+ /** doc id → display label (frontmatter sidebar_label/title, else derived). */
57
+ docLabel: (id: string) => string;
58
+ /** Expand an autogenerated category over a directory → NavItem[]. */
59
+ expandAutogenerated?: (dirName: string) => NavItem[];
60
+ }
61
+ /** Load a sidebars module via the CJS loader (single trust boundary). */
62
+ export declare function loadSidebars(sidebarPath: string): Promise<RawSidebars>;
63
+ /**
64
+ * Build NavItem[] from a loaded sidebars module. All named sidebars in the
65
+ * module are concatenated (a docs instance usually defines exactly one).
66
+ */
67
+ export declare function buildNavFromSidebars(sidebars: RawSidebars, ctx: SidebarContext): NavItem[];
@@ -0,0 +1,65 @@
1
+ import { loadCjs } from "./load-cjs.js";
2
+ /** Load a sidebars module via the CJS loader (single trust boundary). */
3
+ export async function loadSidebars(sidebarPath) {
4
+ const mod = await loadCjs(sidebarPath);
5
+ return mod ?? {};
6
+ }
7
+ function itemToNav(item, ctx) {
8
+ // Shorthand string → doc link.
9
+ if (typeof item === "string") {
10
+ return { label: ctx.docLabel(item), href: ctx.docHref(item) };
11
+ }
12
+ const type = item.type;
13
+ if (type === "doc" || type === "ref") {
14
+ const it = item;
15
+ return { label: it.label ?? ctx.docLabel(it.id), href: ctx.docHref(it.id) };
16
+ }
17
+ if (type === "link") {
18
+ const it = item;
19
+ return { label: it.label, href: it.href };
20
+ }
21
+ if (type === "autogenerated") {
22
+ const it = item;
23
+ return ctx.expandAutogenerated ? ctx.expandAutogenerated(it.dirName) : [];
24
+ }
25
+ if (type === "html") {
26
+ return null;
27
+ }
28
+ if (type === "category") {
29
+ const it = item;
30
+ const children = buildItems(it.items ?? [], ctx);
31
+ const nav = { label: it.label, children };
32
+ // A category linking to a doc becomes clickable and gets that doc as its href.
33
+ if (it.link && it.link.type === "doc" && it.link.id) {
34
+ nav.href = ctx.docHref(it.link.id);
35
+ }
36
+ return nav;
37
+ }
38
+ return null;
39
+ }
40
+ function buildItems(items, ctx) {
41
+ const out = [];
42
+ for (const item of items) {
43
+ const nav = itemToNav(item, ctx);
44
+ if (!nav)
45
+ continue;
46
+ if (Array.isArray(nav))
47
+ out.push(...nav);
48
+ else
49
+ out.push(nav);
50
+ }
51
+ return out;
52
+ }
53
+ /**
54
+ * Build NavItem[] from a loaded sidebars module. All named sidebars in the
55
+ * module are concatenated (a docs instance usually defines exactly one).
56
+ */
57
+ export function buildNavFromSidebars(sidebars, ctx) {
58
+ const out = [];
59
+ for (const key of Object.keys(sidebars)) {
60
+ const items = sidebars[key];
61
+ if (Array.isArray(items))
62
+ out.push(...buildItems(items, ctx));
63
+ }
64
+ return out;
65
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@dogsbay/format-docusaurus",
3
+ "version": "0.2.0-beta.78",
4
+ "description": "Generic Docusaurus importer for Dogsbay — converts standard Docusaurus docs (config instances, sidebars, MDX with Tabs/admonitions/CodeBlock) into TreeNode[]. Site-specific extensions live in injected adapter packages.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./dist/index.js",
8
+ "./cli": "./dist/cli.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md"
13
+ ],
14
+ "dependencies": {
15
+ "gray-matter": "^4.0.3",
16
+ "markdown-it": "^14.1.0",
17
+ "@dogsbay/markdown-it-mdx-jsx": "0.2.0-beta.78",
18
+ "@dogsbay/types": "0.2.0-beta.78"
19
+ },
20
+ "devDependencies": {
21
+ "@types/markdown-it": "^14.1.2",
22
+ "@types/node": "^22.0.0",
23
+ "typescript": "^5.8.0",
24
+ "vitest": "^3.2.0"
25
+ },
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/dogsbay/dogsbay.git",
30
+ "directory": "packages/format-docusaurus"
31
+ },
32
+ "scripts": {
33
+ "build": "tsc",
34
+ "test": "vitest run"
35
+ }
36
+ }