@dogsbay/format-html 0.2.0-beta.182 → 0.2.0-beta.184
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.
- package/dist/convert.js +4 -1
- package/dist/core/blocks.d.ts +1 -0
- package/dist/core/blocks.js +51 -3
- package/dist/core/fidelity.js +3 -0
- package/dist/core/inline.d.ts +5 -0
- package/dist/core/inline.js +4 -3
- package/dist/profiles/index.d.ts +2 -1
- package/dist/profiles/index.js +3 -2
- package/dist/profiles/mintlify.d.ts +7 -0
- package/dist/profiles/mintlify.js +261 -0
- package/dist/site/book.d.ts +6 -2
- package/dist/site/book.js +22 -0
- package/dist/site/gitbook.d.ts +8 -0
- package/dist/site/gitbook.js +16 -3
- package/dist/site/index.d.ts +5 -1
- package/dist/site/index.js +4 -2
- package/dist/site/mintlify.d.ts +75 -0
- package/dist/site/mintlify.js +529 -0
- package/dist/site/types.d.ts +8 -0
- package/package.json +5 -4
package/dist/convert.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* profile rewrite its vocabulary, walk, and compare source with output.
|
|
4
4
|
*/
|
|
5
5
|
import { load } from "cheerio";
|
|
6
|
-
import { blocksFrom, newLedger } from "./core/blocks.js";
|
|
6
|
+
import { blocksFrom, COMPONENT_ATTRS, newLedger } from "./core/blocks.js";
|
|
7
7
|
import { compare, countSource, countTree } from "./core/fidelity.js";
|
|
8
8
|
import { detectProfile, profileByName } from "./profiles/index.js";
|
|
9
9
|
/**
|
|
@@ -24,6 +24,9 @@ export function htmlToTree(input, opts = {}) {
|
|
|
24
24
|
}
|
|
25
25
|
if (opts.remove?.length)
|
|
26
26
|
root.find(opts.remove.join(",")).remove();
|
|
27
|
+
// Our own component marks are the profile's to set, never the page's
|
|
28
|
+
for (const a of COMPONENT_ATTRS)
|
|
29
|
+
root.find(`[${a}]`).addBack(`[${a}]`).removeAttr(a);
|
|
27
30
|
p.normalize($, root);
|
|
28
31
|
const assets = keepSvgs($, root, opts.assetPath);
|
|
29
32
|
const rootNode = root.get(0);
|
package/dist/core/blocks.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export declare function isBlock(n: DomNode): boolean;
|
|
|
40
40
|
export declare function newLedger(): IdLedger;
|
|
41
41
|
/** Convert a node list to blocks. */
|
|
42
42
|
export declare function blocksFrom(nodes: DomNode[], opts?: BlockOptions, ids?: IdLedger, containerId?: string): TreeNode[];
|
|
43
|
+
export declare const COMPONENT_ATTRS: string[];
|
|
43
44
|
/**
|
|
44
45
|
* A caption is inline Markdown (dogsbay-md); a <caption>'s text is plain, so the
|
|
45
46
|
* characters Markdown would read as formatting are escaped.
|
package/dist/core/blocks.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { classOf, elements, rawText, tagOf, textContent } from "../dom.js";
|
|
2
|
-
import { inlineFrom, trimEdges } from "./inline.js";
|
|
2
|
+
import { inlineFrom, safeLink, trimEdges } from "./inline.js";
|
|
3
3
|
/** Tags that are always blocks. */
|
|
4
4
|
const BLOCK_TAGS = new Set([
|
|
5
5
|
"p", "ul", "ol", "dl", "table", "pre", "div", "section", "article", "aside", "main", "header", "footer",
|
|
@@ -112,6 +112,13 @@ class Walker {
|
|
|
112
112
|
out.push(this.callout(n));
|
|
113
113
|
continue;
|
|
114
114
|
}
|
|
115
|
+
const block = blockOf(n);
|
|
116
|
+
if (block) {
|
|
117
|
+
lose();
|
|
118
|
+
this.recordId(n);
|
|
119
|
+
out.push(this.component(n, block));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
115
122
|
if (isContainer(name)) {
|
|
116
123
|
// Unwrap; the container's id goes down to its first heading
|
|
117
124
|
const id = n.attribs?.id;
|
|
@@ -189,6 +196,27 @@ class Walker {
|
|
|
189
196
|
props.title = title;
|
|
190
197
|
return { type: "callout", props, children: this.blocks(n.children ?? []) };
|
|
191
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* A component a profile marked (`data-dogsbay-block`): its type, its label/title/href from
|
|
201
|
+
* `data-dogsbay-label`/`-title`/`-href`, its content as blocks. Plain text props only.
|
|
202
|
+
*/
|
|
203
|
+
component(n, type) {
|
|
204
|
+
const props = {};
|
|
205
|
+
for (const key of COMPONENT_PROPS[type] ?? []) {
|
|
206
|
+
const v = n.attribs?.[`data-dogsbay-${key}`];
|
|
207
|
+
if (v === undefined)
|
|
208
|
+
continue;
|
|
209
|
+
const text = v.replace(/\s+/g, " ").trim();
|
|
210
|
+
if (key === "href") {
|
|
211
|
+
const href = this.opts.resolveHref ? this.opts.resolveHref(text, n) : text;
|
|
212
|
+
if (href && safeLink(href))
|
|
213
|
+
props.href = href;
|
|
214
|
+
}
|
|
215
|
+
else if (text)
|
|
216
|
+
props[key] = text;
|
|
217
|
+
}
|
|
218
|
+
return { type, props, children: this.blocks(n.children ?? []) };
|
|
219
|
+
}
|
|
192
220
|
/**
|
|
193
221
|
* A list. Every <li> is an item, empty or not. The item's leading text (or its
|
|
194
222
|
* first paragraph) is the item's own text; what follows are its blocks.
|
|
@@ -316,6 +344,22 @@ const CONTAINER_TAGS = new Set(["div", "section", "article", "main", "header", "
|
|
|
316
344
|
function isContainer(name) {
|
|
317
345
|
return CONTAINER_TAGS.has(name) || (!BLOCK_TAGS.has(name) && !INLINE_TAGS.has(name));
|
|
318
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* Components a profile can mark with `data-dogsbay-block="<type>"`, and the props each takes
|
|
349
|
+
* (from `data-dogsbay-<prop>`). htmlToTree removes these attributes from the page before the
|
|
350
|
+
* profile runs: a page cannot mark its own.
|
|
351
|
+
*/
|
|
352
|
+
const COMPONENT_PROPS = {
|
|
353
|
+
tabs: [], tab: ["label"],
|
|
354
|
+
cards: [], card: ["title", "href"],
|
|
355
|
+
accordion: [], "accordion-item": ["label"],
|
|
356
|
+
steps: [], step: [],
|
|
357
|
+
};
|
|
358
|
+
export const COMPONENT_ATTRS = ["data-dogsbay-block", "data-dogsbay-label", "data-dogsbay-title", "data-dogsbay-href"];
|
|
359
|
+
function blockOf(n) {
|
|
360
|
+
const t = n.attribs?.["data-dogsbay-block"];
|
|
361
|
+
return t && Object.hasOwn(COMPONENT_PROPS, t) ? t : undefined;
|
|
362
|
+
}
|
|
319
363
|
function isCallout(n) {
|
|
320
364
|
return n.attribs?.["data-callout"] !== undefined;
|
|
321
365
|
}
|
|
@@ -338,8 +382,12 @@ function codeBlock(pre) {
|
|
|
338
382
|
const code = elements(pre).length === 1 && tagOf(elements(pre)[0]) === "code" ? elements(pre)[0] : undefined;
|
|
339
383
|
const language = langFrom(classOf(pre)) ?? (code ? langFrom(classOf(code)) : undefined) ?? pre.attribs?.["data-lang"] ?? code?.attribs?.["data-lang"];
|
|
340
384
|
// A leading newline after <pre> is not content (HTML drops it); trailing whitespace neither
|
|
341
|
-
|
|
342
|
-
|
|
385
|
+
// trimEnd, not /\s+$/: that pattern is quadratic on a long run of spaces inside the code
|
|
386
|
+
const text = rawText(pre).replace(/^\r?\n/, "").trimEnd();
|
|
387
|
+
// A code title is written into the fence's attributes, which nothing decodes or masks: a
|
|
388
|
+
// template opener (`{{`, `{%`, `{#`) from the page is broken there, or the site build would run it
|
|
389
|
+
const title = (pre.attribs?.["data-dogsbay-title"] ?? "").replace(/\s+/g, " ").trim().replace(/\{(?=[{%#])/g, "{ ");
|
|
390
|
+
return { type: "code", html: text, props: { code: text, ...(language ? { language } : {}), ...(title ? { title } : {}) } };
|
|
343
391
|
}
|
|
344
392
|
function langFrom(cls) {
|
|
345
393
|
const m = /\b(?:language|lang)-([\w+#.-]+)/.exec(cls);
|
package/dist/core/fidelity.js
CHANGED
|
@@ -39,6 +39,9 @@ export function countSource(nodes) {
|
|
|
39
39
|
continue;
|
|
40
40
|
if (hasHref(n))
|
|
41
41
|
c.links++;
|
|
42
|
+
// A component a profile gave a link (a card) is a link, as it is in the tree
|
|
43
|
+
if (n.attribs?.["data-dogsbay-block"] && n.attribs["data-dogsbay-href"]?.trim())
|
|
44
|
+
c.links++;
|
|
42
45
|
if (/^h[1-6]$/.test(name))
|
|
43
46
|
c.headings++;
|
|
44
47
|
else if (name === "pre") {
|
package/dist/core/inline.d.ts
CHANGED
|
@@ -32,3 +32,8 @@ export declare function inlineFrom(nodes: DomNode[] | undefined, opts?: InlineOp
|
|
|
32
32
|
* does not show.
|
|
33
33
|
*/
|
|
34
34
|
export declare function trimEdges(nodes: InlineNode[]): InlineNode[];
|
|
35
|
+
/**
|
|
36
|
+
* A link or image source that runs nothing: not `javascript:`/`vbscript:` (spaces and control
|
|
37
|
+
* characters inside the scheme ignored, as browsers do), and `data:` only for an image's image.
|
|
38
|
+
*/
|
|
39
|
+
export declare function safeLink(href: string, image?: boolean): boolean;
|
package/dist/core/inline.js
CHANGED
|
@@ -20,8 +20,9 @@ function walk(nodes, ann, out, opts) {
|
|
|
20
20
|
const name = tagOf(n);
|
|
21
21
|
if (CODE_TAGS.has(name)) {
|
|
22
22
|
const text = textContent(n);
|
|
23
|
+
// Bold or italic around code (`<strong><code>`) stays on it, as the Markdown reader gives it
|
|
23
24
|
if (text)
|
|
24
|
-
out.push({ type: "code", text });
|
|
25
|
+
out.push({ type: "code", text, ...(ann.bold ? { bold: true } : {}), ...(ann.italic ? { italic: true } : {}), ...(ann.strike ? { strike: true } : {}) });
|
|
25
26
|
continue;
|
|
26
27
|
}
|
|
27
28
|
switch (name) {
|
|
@@ -148,14 +149,14 @@ export function trimEdges(nodes) {
|
|
|
148
149
|
a[0] = { ...first, text: first.text.replace(/^\s+/, "") };
|
|
149
150
|
const last = a[a.length - 1];
|
|
150
151
|
if (last?.type === "text")
|
|
151
|
-
a[a.length - 1] = { ...last, text: last.text.
|
|
152
|
+
a[a.length - 1] = { ...last, text: last.text.trimEnd() };
|
|
152
153
|
return a;
|
|
153
154
|
}
|
|
154
155
|
/**
|
|
155
156
|
* A link or image source that runs nothing: not `javascript:`/`vbscript:` (spaces and control
|
|
156
157
|
* characters inside the scheme ignored, as browsers do), and `data:` only for an image's image.
|
|
157
158
|
*/
|
|
158
|
-
function safeLink(href, image = false) {
|
|
159
|
+
export function safeLink(href, image = false) {
|
|
159
160
|
const h = href.replace(/[\u0000-\u0020]/g, "").toLowerCase();
|
|
160
161
|
if (h.startsWith("javascript:") || h.startsWith("vbscript:"))
|
|
161
162
|
return false;
|
package/dist/profiles/index.d.ts
CHANGED
|
@@ -2,10 +2,11 @@ import type { CheerioAPI } from "cheerio";
|
|
|
2
2
|
import { broadcomTechdocs } from "./broadcom-techdocs.js";
|
|
3
3
|
import { ditaOtHtml5 } from "./dita-ot-html5.js";
|
|
4
4
|
import { generic } from "./generic.js";
|
|
5
|
+
import { mintlifyProfile } from "./mintlify.js";
|
|
5
6
|
import type { Profile } from "./types.js";
|
|
6
7
|
/** Detection order: the most specific first; generic matches anything. */
|
|
7
8
|
export declare const PROFILES: Profile[];
|
|
8
9
|
export declare function profileByName(name: string): Profile | undefined;
|
|
9
10
|
export declare function detectProfile($: CheerioAPI): Profile;
|
|
10
|
-
export { broadcomTechdocs, ditaOtHtml5, generic };
|
|
11
|
+
export { broadcomTechdocs, ditaOtHtml5, generic, mintlifyProfile };
|
|
11
12
|
export type { Profile };
|
package/dist/profiles/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { broadcomTechdocs } from "./broadcom-techdocs.js";
|
|
2
2
|
import { ditaOtHtml5 } from "./dita-ot-html5.js";
|
|
3
3
|
import { generic } from "./generic.js";
|
|
4
|
+
import { mintlifyProfile } from "./mintlify.js";
|
|
4
5
|
/** Detection order: the most specific first; generic matches anything. */
|
|
5
|
-
export const PROFILES = [broadcomTechdocs, ditaOtHtml5, generic];
|
|
6
|
+
export const PROFILES = [broadcomTechdocs, ditaOtHtml5, mintlifyProfile, generic];
|
|
6
7
|
export function profileByName(name) {
|
|
7
8
|
return PROFILES.find((p) => p.name === name);
|
|
8
9
|
}
|
|
9
10
|
export function detectProfile($) {
|
|
10
11
|
return PROFILES.find((p) => p.detect($)) ?? generic;
|
|
11
12
|
}
|
|
12
|
-
export { broadcomTechdocs, ditaOtHtml5, generic };
|
|
13
|
+
export { broadcomTechdocs, ditaOtHtml5, generic, mintlifyProfile };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Profile } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The content: `#content` (in `#content-area`, beside `#header`; a custom page's is `#content.mdx-content`,
|
|
4
|
+
* not always in a `<main>`), else an API reference page's operation (`#api-playground-2-operation-page`).
|
|
5
|
+
*/
|
|
6
|
+
export declare const MINTLIFY_ROOT = "#content-area #content, #content.mdx-content, #content-area > [id^=\"api-playground-\"][id$=\"-operation-page\"]";
|
|
7
|
+
export declare const mintlifyProfile: Profile;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The content: `#content` (in `#content-area`, beside `#header`; a custom page's is `#content.mdx-content`,
|
|
3
|
+
* not always in a `<main>`), else an API reference page's operation (`#api-playground-2-operation-page`).
|
|
4
|
+
*/
|
|
5
|
+
export const MINTLIFY_ROOT = '#content-area #content, #content.mdx-content, #content-area > [id^="api-playground-"][id$="-operation-page"]';
|
|
6
|
+
const part = (name) => `[data-component-part="${name}"]`;
|
|
7
|
+
/** Mintlify's callout types → ours (as its Markdown export maps them). */
|
|
8
|
+
const CALLOUTS = { note: "note", info: "note", tip: "tip", check: "tip", warning: "warning", danger: "caution", callout: "note" };
|
|
9
|
+
/** Never content: buttons' furniture, icons, tooltips, the copy and feedback buttons. */
|
|
10
|
+
const CHROME = [
|
|
11
|
+
part("callout-icon"), part("card-icon"), part("accordion-icon"), part("accordion-caret-right"), part("expandable-icon"),
|
|
12
|
+
part("step-line"), part("step-number"), part("card-cta"), part("code-block-footer"),
|
|
13
|
+
part("prompt-actions-wrapper"), part("prompt-icon"),
|
|
14
|
+
".code-block-copy-button", "[data-base-ui-tooltip-trigger]", "[role=status]", "a.anchor-link",
|
|
15
|
+
'[data-as="iframe"]',
|
|
16
|
+
// The caret drawn in an accordion's or expandable's summary
|
|
17
|
+
"summary svg",
|
|
18
|
+
];
|
|
19
|
+
/** `hidden dark:block` or `mint-hidden dark:mint-block`: shown in dark mode only. */
|
|
20
|
+
const DARK_ONLY = /^(?=.*\s(?:mint-)?hidden\s)(?=.*\sdark:(?:mint-)?block\s)/;
|
|
21
|
+
const one = (s) => s.replace(/\s+/g, " ").trim();
|
|
22
|
+
export const mintlifyProfile = {
|
|
23
|
+
name: "mintlify",
|
|
24
|
+
detect: ($) => $(MINTLIFY_ROOT).length > 0 && $('template[data-mint="manifest"], meta[name="generator" i][content^="Mintlify" i]').length > 0,
|
|
25
|
+
root($) {
|
|
26
|
+
const r = $(MINTLIFY_ROOT).first();
|
|
27
|
+
return r.length ? r : undefined;
|
|
28
|
+
},
|
|
29
|
+
normalize($, root) {
|
|
30
|
+
const rename = (el, name) => { el.name = name; };
|
|
31
|
+
const textOf = (sel) => one(sel.first().text());
|
|
32
|
+
// Images for dark mode only (a light one stands beside each)
|
|
33
|
+
root.find("img").filter((_, e) => DARK_ONLY.test(` ${e.attribs.class ?? ""} `)).remove();
|
|
34
|
+
// Prompts: the page shows a description and a Copy button; the prompt's text is in the
|
|
35
|
+
// "Open in Cursor" link (`cursor.com/link/prompt?text=…`, 50 of 51 prompts on 2,758 pages,
|
|
36
|
+
// 2026-11-24). It is kept as written, in a code block (someone else's Markdown stays text)
|
|
37
|
+
// in a closed "Prompt" section under the description. A prompt with no such link keeps
|
|
38
|
+
// its description only.
|
|
39
|
+
// Mintlify's prompts only (their parts), outermost first, never one inside code (`.prompt` is a
|
|
40
|
+
// common class: a shell prompt `$` in a <pre>)
|
|
41
|
+
root.find(".prompt").filter((_, el) => {
|
|
42
|
+
const p = $(el);
|
|
43
|
+
return p.children(`${part("prompt-content-wrapper")}, ${part("prompt-actions-wrapper")}`).length > 0 && !p.parents(".prompt, pre, code").length;
|
|
44
|
+
}).each((_, el) => {
|
|
45
|
+
const prompt = $(el);
|
|
46
|
+
const out = $("<div>");
|
|
47
|
+
out.append(prompt.find(part("prompt-description")).first().contents());
|
|
48
|
+
const text = promptText(prompt.find(`${part("prompt-action-cursor-button")}[href]`).first().attr("href"));
|
|
49
|
+
if (text)
|
|
50
|
+
out.append($("<details>").append($("<summary>").text("Prompt"), $("<pre>").attr("data-lang", "markdown").append($("<code>").text(text))));
|
|
51
|
+
prompt.replaceWith(out);
|
|
52
|
+
});
|
|
53
|
+
// Code first: a code block's header holds buttons and its text spans the rest would touch
|
|
54
|
+
root.find(".code-group").each((_, g) => {
|
|
55
|
+
const group = $(g);
|
|
56
|
+
const labels = group.find("[role=tab]").map((_, t) => one($(t).text())).get();
|
|
57
|
+
const panels = group.find("[role=tabpanel]").toArray();
|
|
58
|
+
// A group of one is a titled code block
|
|
59
|
+
if (panels.length === 1) {
|
|
60
|
+
const pre = $(panels[0]).find("pre").first();
|
|
61
|
+
if (pre.length)
|
|
62
|
+
group.replaceWith(codeOf($, pre, pre.attr("language") ?? $(panels[0]).find("[language]").first().attr("language"), labels[0]));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const tabs = $("<div>").attr("data-dogsbay-block", "tabs");
|
|
66
|
+
panels.forEach((p, i) => {
|
|
67
|
+
const pre = $(p).find("pre").first();
|
|
68
|
+
const tab = $("<div>").attr({ "data-dogsbay-block": "tab", "data-dogsbay-label": labels[i] || `Tab ${i + 1}` });
|
|
69
|
+
if (pre.length)
|
|
70
|
+
tab.append(codeOf($, pre, pre.attr("language") ?? $(p).find("[language]").first().attr("language"), undefined));
|
|
71
|
+
tabs.append(tab);
|
|
72
|
+
});
|
|
73
|
+
group.replaceWith(tabs);
|
|
74
|
+
});
|
|
75
|
+
root.find(".code-block").each((_, b) => {
|
|
76
|
+
const block = $(b);
|
|
77
|
+
const pre = block.find("pre").first();
|
|
78
|
+
if (!pre.length)
|
|
79
|
+
return;
|
|
80
|
+
const title = textOf(block.find(part("code-block-header-filename")));
|
|
81
|
+
block.replaceWith(codeOf($, pre, block.attr("language") ?? pre.attr("language"), title || undefined));
|
|
82
|
+
});
|
|
83
|
+
// Changelog updates: the label is a heading (its self-link names its anchor)
|
|
84
|
+
root.find(part("update-label")).each((_, u) => {
|
|
85
|
+
const label = $(u);
|
|
86
|
+
const box = label.parent();
|
|
87
|
+
const id = box.children("a.anchor-link").attr("href")?.replace(/^#/, "");
|
|
88
|
+
const h = $("<h2>").text(one(label.text()));
|
|
89
|
+
if (id)
|
|
90
|
+
h.attr("id", id);
|
|
91
|
+
label.replaceWith(h);
|
|
92
|
+
});
|
|
93
|
+
root.find(CHROME.join(",")).remove();
|
|
94
|
+
// Paragraphs and the like drawn as spans
|
|
95
|
+
root.find("[data-as]").each((_, e) => {
|
|
96
|
+
const as = e.attribs["data-as"];
|
|
97
|
+
if (/^(p|h[1-6]|li|ul|ol|blockquote)$/.test(as))
|
|
98
|
+
rename(e, as);
|
|
99
|
+
});
|
|
100
|
+
root.find(".callout[data-callout-type]").each((_, c) => {
|
|
101
|
+
const el = $(c);
|
|
102
|
+
el.attr("data-callout", CALLOUTS[el.attr("data-callout-type")] ?? "note");
|
|
103
|
+
const content = el.children(part("callout-content"));
|
|
104
|
+
if (content.length)
|
|
105
|
+
el.empty().append(content.contents());
|
|
106
|
+
});
|
|
107
|
+
// Tabs: each tab's panel by its id (aria-controls), else in order
|
|
108
|
+
root.find(".tabs").filter((_, t) => $(t).children("[role=tablist]").length > 0).toArray().reverse().forEach((t) => {
|
|
109
|
+
const el = $(t);
|
|
110
|
+
const list = el.children("[role=tablist]");
|
|
111
|
+
const panels = el.find("[role=tabpanel]").filter((_, p) => $(p).closest(".tabs")[0] === t).toArray();
|
|
112
|
+
const tabs = $("<div>").attr("data-dogsbay-block", "tabs");
|
|
113
|
+
// Each panel once: by its id, else the next one no tab has taken
|
|
114
|
+
const byId = new Map(panels.filter((p) => p.attribs.id).map((p) => [p.attribs.id, p]));
|
|
115
|
+
const used = new Set();
|
|
116
|
+
let next = 0;
|
|
117
|
+
list.children("[role=tab]").each((i, li) => {
|
|
118
|
+
const id = $(li).attr("aria-controls");
|
|
119
|
+
let panel = id ? byId.get(id) : undefined;
|
|
120
|
+
if (panel && used.has(panel))
|
|
121
|
+
panel = undefined;
|
|
122
|
+
while (!panel && next < panels.length) {
|
|
123
|
+
const p = panels[next++];
|
|
124
|
+
if (!used.has(p))
|
|
125
|
+
panel = p;
|
|
126
|
+
}
|
|
127
|
+
if (panel)
|
|
128
|
+
used.add(panel);
|
|
129
|
+
const tab = $("<div>").attr({ "data-dogsbay-block": "tab", "data-dogsbay-label": one($(li).text()) || `Tab ${i + 1}` });
|
|
130
|
+
if (panel)
|
|
131
|
+
tab.append($(panel).contents());
|
|
132
|
+
tabs.append(tab);
|
|
133
|
+
});
|
|
134
|
+
el.replaceWith(tabs);
|
|
135
|
+
});
|
|
136
|
+
// Steps: the title as the step's bold first line, as the Markdown reader makes it
|
|
137
|
+
root.find("[role=list].steps").each((_, l) => {
|
|
138
|
+
const list = $(l);
|
|
139
|
+
list.attr("data-dogsbay-block", "steps");
|
|
140
|
+
list.children(".step").each((_, s) => {
|
|
141
|
+
const step = $(s);
|
|
142
|
+
const title = textOf(step.find(part("step-title")));
|
|
143
|
+
const content = step.find(part("step-content")).first();
|
|
144
|
+
const out = $("<div>").attr("data-dogsbay-block", "step");
|
|
145
|
+
if (title)
|
|
146
|
+
out.append($("<p>").append($("<strong>").text(title)));
|
|
147
|
+
out.append(content.contents());
|
|
148
|
+
step.replaceWith(out);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
// Accordions: a group is one accordion; a lone one an accordion of one
|
|
152
|
+
root.find("details.accordion").each((_, d) => {
|
|
153
|
+
const el = $(d);
|
|
154
|
+
const label = textOf(el.find(part("accordion-title")));
|
|
155
|
+
const item = $("<div>").attr({ "data-dogsbay-block": "accordion-item", "data-dogsbay-label": label || "Details" });
|
|
156
|
+
item.append(el.find(part("accordion-content")).first().contents());
|
|
157
|
+
const anchor = el.children("summary").children("[id]").first().attr("id");
|
|
158
|
+
if (anchor)
|
|
159
|
+
item.attr("id", anchor);
|
|
160
|
+
if (el.parent().hasClass("accordion-group"))
|
|
161
|
+
el.replaceWith(item);
|
|
162
|
+
else
|
|
163
|
+
el.replaceWith($("<div>").attr("data-dogsbay-block", "accordion").append(item));
|
|
164
|
+
});
|
|
165
|
+
root.find(".accordion-group").attr("data-dogsbay-block", "accordion");
|
|
166
|
+
// Cards: those side by side are one group; the title is not a heading
|
|
167
|
+
root.find(".card").filter((_, c) => $(c).find(part("card-title")).length > 0).each((_, c) => {
|
|
168
|
+
const el = $(c);
|
|
169
|
+
const title = textOf(el.find(part("card-title")));
|
|
170
|
+
const card = $("<div>").attr({ "data-dogsbay-block": "card", ...(title ? { "data-dogsbay-title": title } : {}) });
|
|
171
|
+
// Its link: its own (an <a> card), else the one the site adapter took from the `.md`
|
|
172
|
+
const href = el.is("a[href]") ? el.attr("href") : el.attr("data-mintlify-card-href");
|
|
173
|
+
if (href)
|
|
174
|
+
card.attr("data-dogsbay-href", href);
|
|
175
|
+
card.append(el.find(part("card-content")).first().contents());
|
|
176
|
+
el.replaceWith(card);
|
|
177
|
+
});
|
|
178
|
+
// Each run of cards side by side is one group, wrapped where it is: never by marking the
|
|
179
|
+
// element holding it (a tab, an accordion item, a step, or a box with text in it). Each
|
|
180
|
+
// parent's children are walked once.
|
|
181
|
+
const parents = new Set();
|
|
182
|
+
root.find('[data-dogsbay-block="card"]').each((_, c) => { if (c.parent)
|
|
183
|
+
parents.add(c.parent); });
|
|
184
|
+
for (const parent of parents) {
|
|
185
|
+
let run = [];
|
|
186
|
+
const close = () => {
|
|
187
|
+
if (run.length) {
|
|
188
|
+
const box = $("<div>").attr("data-dogsbay-block", "cards");
|
|
189
|
+
$(run[0]).before(box);
|
|
190
|
+
box.append(run);
|
|
191
|
+
}
|
|
192
|
+
run = [];
|
|
193
|
+
};
|
|
194
|
+
for (const n of [...(parent.children ?? [])]) {
|
|
195
|
+
if (n.type === "tag" && n.attribs?.["data-dogsbay-block"] === "card")
|
|
196
|
+
run.push(n);
|
|
197
|
+
else if (n.type === "text" && !(n.data ?? "").trim())
|
|
198
|
+
continue;
|
|
199
|
+
else if (n.type === "comment")
|
|
200
|
+
continue;
|
|
201
|
+
else
|
|
202
|
+
close();
|
|
203
|
+
}
|
|
204
|
+
close();
|
|
205
|
+
}
|
|
206
|
+
// Fields: name, type and "required" as a line, the description under it
|
|
207
|
+
root.find(part("field-name")).each((_, f) => {
|
|
208
|
+
const name = one($(f).text());
|
|
209
|
+
const head = $(f).parent();
|
|
210
|
+
const type = textOf(head.find(part("field-info-pill")));
|
|
211
|
+
const required = head.find(part("field-required-pill")).length > 0;
|
|
212
|
+
const p = $("<p>").append($("<strong>").append($("<code>").text(name)));
|
|
213
|
+
if (type)
|
|
214
|
+
p.append(" ").append($("<code>").text(type));
|
|
215
|
+
if (required)
|
|
216
|
+
p.append(" required");
|
|
217
|
+
head.replaceWith(p);
|
|
218
|
+
});
|
|
219
|
+
root.find(part("frame-caption")).each((_, c) => rename(c, "figcaption"));
|
|
220
|
+
root.find("video[src]").each((_, v) => {
|
|
221
|
+
$(v).replaceWith($("<p>").append($("<a>").attr("href", $(v).attr("src")).text("Video")));
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
description($) {
|
|
225
|
+
const text = one($("#header").children("div").not(":has(#page-title)").find("p").first().text());
|
|
226
|
+
return text || undefined;
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
/** The longest prompt kept (the longest seen was 13 KB). */
|
|
230
|
+
const MAX_PROMPT = 1024 * 1024;
|
|
231
|
+
/** A prompt's text from its "Open in Cursor" link: the `text` parameter, as written. */
|
|
232
|
+
function promptText(href) {
|
|
233
|
+
if (!href || href.length > MAX_PROMPT * 4)
|
|
234
|
+
return undefined;
|
|
235
|
+
let url;
|
|
236
|
+
try {
|
|
237
|
+
url = new URL(href);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
// Cursor's prompt link only: another host's `text` is not the prompt
|
|
243
|
+
if (url.protocol !== "https:" || url.hostname !== "cursor.com")
|
|
244
|
+
return undefined;
|
|
245
|
+
const text = url.searchParams.get("text");
|
|
246
|
+
if (!text || text.length > MAX_PROMPT)
|
|
247
|
+
return undefined;
|
|
248
|
+
// trimEnd, not /\s+$/: that pattern is quadratic on a long run of spaces inside the text
|
|
249
|
+
return text.replace(/\r\n?/g, "\n").trimEnd() || undefined;
|
|
250
|
+
}
|
|
251
|
+
/** A Shiki `<pre>` as a plain one: its text, its language and title. */
|
|
252
|
+
function codeOf($, pre, language, title) {
|
|
253
|
+
const lines = pre.find(".line");
|
|
254
|
+
const text = lines.length ? lines.map((_, l) => $(l).text()).get().join("\n") : pre.text();
|
|
255
|
+
const out = $("<pre>").append($("<code>").text(text));
|
|
256
|
+
if (language && /^[\w+#.-]{1,40}$/.test(language))
|
|
257
|
+
out.attr("data-lang", language);
|
|
258
|
+
if (title)
|
|
259
|
+
out.attr("data-dogsbay-title", title);
|
|
260
|
+
return out;
|
|
261
|
+
}
|
package/dist/site/book.d.ts
CHANGED
|
@@ -20,8 +20,12 @@ export interface ImportBookOptions {
|
|
|
20
20
|
html: string;
|
|
21
21
|
url?: string;
|
|
22
22
|
};
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* A page's HTML (or its text, for an adapter with `readText`), or null when it cannot be had.
|
|
25
|
+
* `what` is `"text"` when importBook fetches the site's text of a page read from its HTML
|
|
26
|
+
* (`SiteAdapter.textFor`): not the page's source.
|
|
27
|
+
*/
|
|
28
|
+
fetch: (url: string, key: string, signal?: AbortSignal, what?: "text") => Promise<string | null>;
|
|
25
29
|
/**
|
|
26
30
|
* An image's bytes, or null when it cannot be had (or is not an image). When given, the pages'
|
|
27
31
|
* images on the site's own host are saved in the book (`content/assets/`, in `binaries` or handed
|
package/dist/site/book.js
CHANGED
|
@@ -289,10 +289,30 @@ export async function importBook(opts) {
|
|
|
289
289
|
dataShown.set(key, shows);
|
|
290
290
|
converted.set(key, { tree: page.tree, title: page.title ?? entryTitle(order, key), description: page.description, type: page.type ?? "topic", anchored, fidelity, info, links, assets: [] });
|
|
291
291
|
};
|
|
292
|
+
// What a page's HTML lacks, from the site's own text of it (one more fetch, only when needed)
|
|
293
|
+
const fillText = async (key, $) => {
|
|
294
|
+
const url = adapter.textFor?.($, key);
|
|
295
|
+
if (!url || !adapter.fillFromText)
|
|
296
|
+
return;
|
|
297
|
+
if (opts.signal?.aborted)
|
|
298
|
+
throw new Error("cancelled");
|
|
299
|
+
if (delay > 0)
|
|
300
|
+
await sleep(delay, opts.signal);
|
|
301
|
+
const text = await opts.fetch(url, key, opts.signal, "text").catch(() => null);
|
|
302
|
+
if (opts.signal?.aborted)
|
|
303
|
+
throw new Error("cancelled");
|
|
304
|
+
// Someone else's text: a page it cannot fill is converted as it is
|
|
305
|
+
if (text)
|
|
306
|
+
try {
|
|
307
|
+
adapter.fillFromText($, text, url);
|
|
308
|
+
}
|
|
309
|
+
catch { /* as it is */ }
|
|
310
|
+
};
|
|
292
311
|
for (const [index, e] of order.entries()) {
|
|
293
312
|
const key = e.key;
|
|
294
313
|
// The start page as read, unless the adapter fetches content from elsewhere (fetchUrlOf)
|
|
295
314
|
if (key === startKey && !adapter.fetchUrlOf && !adapter.readText) {
|
|
315
|
+
await fillText(key, $start);
|
|
296
316
|
// Found by title or slug: its links resolve against its own address, not the one it was read at
|
|
297
317
|
convert(key, $start, key === startInfo.key ? startInfo : { ...startInfo, key, url: adapter.urlOf(key) });
|
|
298
318
|
}
|
|
@@ -335,6 +355,8 @@ export async function importBook(opts) {
|
|
|
335
355
|
else {
|
|
336
356
|
const $ = body ? load(body) : null;
|
|
337
357
|
const info = $ ? adapter.readPage($, adapter.urlOf(key)) : null;
|
|
358
|
+
if ($ && info)
|
|
359
|
+
await fillText(key, $);
|
|
338
360
|
if ($ && info)
|
|
339
361
|
convert(key, $, info);
|
|
340
362
|
else
|
package/dist/site/gitbook.d.ts
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
import { type CheerioAPI } from "cheerio";
|
|
26
26
|
import { type PageIntegration } from "@dogsbay/format-gitbook";
|
|
27
|
+
import type { TreeNode } from "@dogsbay/types";
|
|
27
28
|
import type { Counts } from "../core/fidelity.js";
|
|
28
29
|
import type { SiteAdapter, TextPage } from "./types.js";
|
|
29
30
|
/** One entry of GitBook's page tree, as its page data writes it. */
|
|
@@ -64,6 +65,13 @@ export declare function gitbookIntegrations($: CheerioAPI): PageIntegration[];
|
|
|
64
65
|
* page's integration blocks (`gitbookIntegrations`), its own are filled from them.
|
|
65
66
|
*/
|
|
66
67
|
export declare function readGitbookText(text: string, pageIntegrations?: PageIntegration[]): TextPage | null;
|
|
68
|
+
/**
|
|
69
|
+
* Raw HTML a page's Markdown holds, read by the HTML core where it can be (GitBook's and
|
|
70
|
+
* Mintlify's `.md`): a node that is one block element (a table, a centred image…), a table cell
|
|
71
|
+
* holding HTML, GitBook's hints. Links in it become links (settled with the page's), images
|
|
72
|
+
* become images (saved); what cannot be read stays as written.
|
|
73
|
+
*/
|
|
74
|
+
export declare function readRawHtml(tree: TreeNode[]): TreeNode[];
|
|
67
75
|
/**
|
|
68
76
|
* What a GitBook page's text holds, counted from the text (outside code), for the fidelity
|
|
69
77
|
* report: headings, code blocks, tables, list items, images, callouts (hints), links. Every scan
|
package/dist/site/gitbook.js
CHANGED
|
@@ -299,14 +299,18 @@ function readIntegrations($) {
|
|
|
299
299
|
export function readGitbookText(text, pageIntegrations) {
|
|
300
300
|
let body = text.replace(/^/, "").replace(/\r\n?/g, "\n").replace(BANNER, "");
|
|
301
301
|
const agent = body.lastIndexOf(AGENT);
|
|
302
|
-
if (agent >= 0)
|
|
303
|
-
|
|
302
|
+
if (agent >= 0) {
|
|
303
|
+
// As /\s+$/ → "\n" did, without its quadratic time on a long run of spaces
|
|
304
|
+
const head = body.slice(0, agent);
|
|
305
|
+
const trimmed = head.trimEnd();
|
|
306
|
+
body = trimmed.length < head.length ? trimmed + "\n" : head;
|
|
307
|
+
}
|
|
304
308
|
if (/^#\s+Page Not Found\s*$/m.test(body.split("\n").find((l) => /^#\s/.test(l)) ?? ""))
|
|
305
309
|
return null;
|
|
306
310
|
// The book is a Dogsbay project: a block's records go in its data/, shared by the pages showing them
|
|
307
311
|
const parsed = parseGitbook(body, { pageIntegrations, dataFiles: true });
|
|
308
312
|
const { integrations, bodyLine, dataFiles } = parsed;
|
|
309
|
-
const tree =
|
|
313
|
+
const tree = readRawHtml(parsed.tree);
|
|
310
314
|
const h1 = tree.findIndex((n) => n.type === "heading" && n.props?.level === 1);
|
|
311
315
|
const title = h1 >= 0 ? plain(tree[h1]) : undefined;
|
|
312
316
|
// GitBook writes the page's description as the paragraph right after the title
|
|
@@ -317,6 +321,15 @@ export function readGitbookText(text, pageIntegrations) {
|
|
|
317
321
|
const data = Object.keys(dataFiles).length ? { ...dataFiles } : undefined;
|
|
318
322
|
return { tree, title, description, type: "topic", source: countGitbook(realize(body, integrations, bodyLine)), ...(data ? { data } : {}) };
|
|
319
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Raw HTML a page's Markdown holds, read by the HTML core where it can be (GitBook's and
|
|
326
|
+
* Mintlify's `.md`): a node that is one block element (a table, a centred image…), a table cell
|
|
327
|
+
* holding HTML, GitBook's hints. Links in it become links (settled with the page's), images
|
|
328
|
+
* become images (saved); what cannot be read stays as written.
|
|
329
|
+
*/
|
|
330
|
+
export function readRawHtml(tree) {
|
|
331
|
+
return htmlCells(htmlHints(tree, 0), 0);
|
|
332
|
+
}
|
|
320
333
|
/** GitBook's hint styles as callout types (format-gitbook's `{% hint %}` mapping). */
|
|
321
334
|
const HINT_STYLES = { info: "info", success: "success", warning: "warning", danger: "danger", note: "note", tip: "tip" };
|
|
322
335
|
const HINT_DIV = /^\s*<div\s[^<>]*\bdata-gb-custom-block\b/i;
|
package/dist/site/index.d.ts
CHANGED
|
@@ -5,14 +5,18 @@
|
|
|
5
5
|
import type { CheerioAPI } from "cheerio";
|
|
6
6
|
import { gitbook, gitbookSite, gitbookData, readGitbookText, countGitbook } from "./gitbook.js";
|
|
7
7
|
import { ibm } from "./ibm.js";
|
|
8
|
+
import { mintlify, mintlifySite, mintlifyData, mintlifyContents, readMintlifyText, countMintlify, fillCardLinks } from "./mintlify.js";
|
|
8
9
|
import { redhat } from "./redhat.js";
|
|
9
10
|
import type { SiteAdapter } from "./types.js";
|
|
10
|
-
/** Sites by their own domain first; GitBook (any domain, by
|
|
11
|
+
/** Sites by their own domain first; GitBook and Mintlify (any domain, by their generator tags) last. */
|
|
11
12
|
export declare const SITES: SiteAdapter[];
|
|
12
13
|
export declare function siteByName(name: string): SiteAdapter | undefined;
|
|
13
14
|
/** The adapter for a page: an adapter for any site (GitBook) bound to the page's site, or none when it cannot be. */
|
|
14
15
|
export declare function detectSite($: CheerioAPI, url?: string): SiteAdapter | undefined;
|
|
15
16
|
export { redhat, ibm, gitbook, gitbookSite, gitbookData, readGitbookText, countGitbook };
|
|
17
|
+
export { mintlify, mintlifySite, mintlifyData, mintlifyContents, readMintlifyText, countMintlify, fillCardLinks };
|
|
18
|
+
export type { MintlifyRead } from "./mintlify.js";
|
|
19
|
+
export type { MintlifyData } from "./mintlify.js";
|
|
16
20
|
/** Every adapter's pages, as extension match patterns. */
|
|
17
21
|
export declare const SITE_MATCHES: string[];
|
|
18
22
|
export { importBook, planBook, planScopes, findBook, navYaml } from "./book.js";
|
package/dist/site/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { gitbook, gitbookSite, gitbookData, readGitbookText, countGitbook } from "./gitbook.js";
|
|
2
2
|
import { ibm } from "./ibm.js";
|
|
3
|
+
import { mintlify, mintlifySite, mintlifyData, mintlifyContents, readMintlifyText, countMintlify, fillCardLinks } from "./mintlify.js";
|
|
3
4
|
import { redhat } from "./redhat.js";
|
|
4
|
-
/** Sites by their own domain first; GitBook (any domain, by
|
|
5
|
-
export const SITES = [redhat, ibm, gitbook];
|
|
5
|
+
/** Sites by their own domain first; GitBook and Mintlify (any domain, by their generator tags) last. */
|
|
6
|
+
export const SITES = [redhat, ibm, gitbook, mintlify];
|
|
6
7
|
export function siteByName(name) {
|
|
7
8
|
return SITES.find((s) => s.name === name);
|
|
8
9
|
}
|
|
@@ -14,6 +15,7 @@ export function detectSite($, url) {
|
|
|
14
15
|
return site.bind($, url) ?? undefined;
|
|
15
16
|
}
|
|
16
17
|
export { redhat, ibm, gitbook, gitbookSite, gitbookData, readGitbookText, countGitbook };
|
|
18
|
+
export { mintlify, mintlifySite, mintlifyData, mintlifyContents, readMintlifyText, countMintlify, fillCardLinks };
|
|
17
19
|
/** Every adapter's pages, as extension match patterns. */
|
|
18
20
|
export const SITE_MATCHES = [...new Set(SITES.flatMap((s) => s.matches))];
|
|
19
21
|
export { importBook, planBook, planScopes, findBook, navYaml } from "./book.js";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mintlify published sites, on any domain (code.claude.com/docs, resend.com/docs,
|
|
3
|
+
* docs.pinecone.io). One book is **what the sidebar shows on the reader's page**: the tab, menu
|
|
4
|
+
* item, dropdown or anchor the page is in.
|
|
5
|
+
*
|
|
6
|
+
* Measured 2026-11-24 on ten sites (Claude Code, Mintlify, Resend, Pinecone, Together,
|
|
7
|
+
* Perplexity, Dub, Browserbase, Firecrawl, Cartesia; plans/import-extension-mintlify.md):
|
|
8
|
+
* - Every page carries `<template data-mint="manifest">`: JSON with the page (`page.slug`), the
|
|
9
|
+
* site's name, `agentPointerHref` (the page's `.md`, with the site's base path) and `nav`, the
|
|
10
|
+
* sidebar: `languages`/`versions`/`products`/`dropdowns`/`tabs`/`anchors`/`menu`, any of them
|
|
11
|
+
* left out, down to `groups` (`group`, `pages`) and pages (`title`, `sidebarTitle`, `href`
|
|
12
|
+
* from the base path). The part the page is in is whole; every other part is folded to its
|
|
13
|
+
* first page, which is why a book is the reader's part. `basePath` is empty even on sites at
|
|
14
|
+
* `/docs`: the base is `agentPointerHref` less the page's slug. `<`, `&` are `<`, `&`.
|
|
15
|
+
* (Cartesia has no manifest, only an older `scopedNav`: not read.)
|
|
16
|
+
* - `docsConfig` in `<template id="mint-config">` has its navigation removed: no whole-site tree.
|
|
17
|
+
* - Pages are read from their `.md` (`@dogsbay/format-mdx/mintlify-export`): Mintlify's
|
|
18
|
+
* documented Markdown export, every component written as Markdown. A missing page is a 404
|
|
19
|
+
* with a "Page Not Found" Markdown body.
|
|
20
|
+
* - Images are on `mintcdn.com`, which answers any origin (`Access-Control-Allow-Origin: *`).
|
|
21
|
+
*
|
|
22
|
+
* Detected by `<meta name="generator" content="Mintlify">` or the manifest (sites with
|
|
23
|
+
* Mintlify's branding removed); `bind` makes the adapter of the page's site and part. Reads,
|
|
24
|
+
* never fetches.
|
|
25
|
+
*/
|
|
26
|
+
import type { CheerioAPI } from "cheerio";
|
|
27
|
+
import type { Counts } from "../core/fidelity.js";
|
|
28
|
+
import type { ContentsEntry, SiteAdapter, TextPage } from "./types.js";
|
|
29
|
+
/** What a Mintlify page's manifest says. */
|
|
30
|
+
export interface MintlifyData {
|
|
31
|
+
/** The sidebar, as the manifest has it (read by `mintlifyContents`, never trusted). */
|
|
32
|
+
nav: unknown;
|
|
33
|
+
siteName?: string;
|
|
34
|
+
/** The site's base path (`/docs`, or `""`): `agentPointerHref` less the page's slug. */
|
|
35
|
+
base: string;
|
|
36
|
+
/** The page's path from the base (`/en/overview`; `/` for the site's first page). */
|
|
37
|
+
slug: string;
|
|
38
|
+
}
|
|
39
|
+
/** The page's manifest, or null when it has none that reads. */
|
|
40
|
+
export declare function mintlifyData($: CheerioAPI): MintlifyData | null;
|
|
41
|
+
/**
|
|
42
|
+
* The book's contents: the page's part of the sidebar as one book (its first page the book's,
|
|
43
|
+
* everything else under it), in a section named after the site. A site's home page the sidebar
|
|
44
|
+
* does not list (a custom landing page: LangChain, Novu, 2026-11-24) is a book of its own: which
|
|
45
|
+
* part a reader wants is not on it (every part is folded there), so they open a page of it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function mintlifyContents(origin: string, data: MintlifyData): ContentsEntry[];
|
|
48
|
+
/** Where a card's link, taken from the page's `.md`, is put for the `mintlify` profile. */
|
|
49
|
+
export declare const CARD_HREF = "data-mintlify-card-href";
|
|
50
|
+
/**
|
|
51
|
+
* Each card's link, from the `.md`'s card lines, `> [card] [Title](href)` (or `> [card] Title`,
|
|
52
|
+
* a card with no link), at any quote depth: matched by title, in document order, so every card
|
|
53
|
+
* with a title takes its turn, linked or not, and only one with no link of its own is given
|
|
54
|
+
* one. Links on the site lose their `.md` and are resolved against the `.md`'s address
|
|
55
|
+
* (`url`); others are kept as written. A card the `.md` does not name stays unlinked. Linear.
|
|
56
|
+
*/
|
|
57
|
+
export declare function fillCardLinks($: CheerioAPI, text: string, url?: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* How a Mintlify page is read: `md`, its `.md` (Mintlify's Markdown export); `html`, the page
|
|
60
|
+
* itself with the `mintlify` profile.
|
|
61
|
+
*/
|
|
62
|
+
export type MintlifyRead = "md" | "html";
|
|
63
|
+
/** The adapter of one Mintlify site and part, from a page of it. */
|
|
64
|
+
export declare function mintlifySite(origin: string, data: MintlifyData, read?: MintlifyRead): SiteAdapter;
|
|
65
|
+
/** The adapter every Mintlify site gets bound from: detects, then `bind`s to the page's site. */
|
|
66
|
+
export declare const mintlify: SiteAdapter;
|
|
67
|
+
/** A page's `.md` as a tree, or null when it is Mintlify's "Page Not Found". */
|
|
68
|
+
export declare function readMintlifyText(text: string): TextPage | null;
|
|
69
|
+
/**
|
|
70
|
+
* What a Mintlify page's Markdown holds, for the fidelity report: headings, code blocks, tables,
|
|
71
|
+
* list items, images, callouts (alerts) and links, counted at any blockquote depth (tabs, cards
|
|
72
|
+
* and accordions are blockquotes), outside code. A card's title link counts as a link, as the
|
|
73
|
+
* card's `href` does in the tree. Every scan is linear.
|
|
74
|
+
*/
|
|
75
|
+
export declare function countMintlify(text: string): Counts;
|
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
import { readMintlifyExport } from "@dogsbay/format-mdx/mintlify-export";
|
|
2
|
+
import { readRawHtml } from "./gitbook.js";
|
|
3
|
+
import { MINTLIFY_ROOT } from "../profiles/mintlify.js";
|
|
4
|
+
const MAX_MANIFEST = 16 * 1024 * 1024;
|
|
5
|
+
const isMintlify = ($) => $('meta[name="generator" i]').toArray().some((e) => /^Mintlify\b/i.test($(e).attr("content") ?? "")) ||
|
|
6
|
+
$('template[data-mint="manifest"]').length > 0;
|
|
7
|
+
/** The page's manifest, or null when it has none that reads. */
|
|
8
|
+
export function mintlifyData($) {
|
|
9
|
+
const text = $('template[data-mint="manifest"]').first().text();
|
|
10
|
+
if (!text || text.length > MAX_MANIFEST)
|
|
11
|
+
return null;
|
|
12
|
+
let m;
|
|
13
|
+
try {
|
|
14
|
+
m = JSON.parse(text);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
if (!m || typeof m !== "object")
|
|
20
|
+
return null;
|
|
21
|
+
// Both as a URL encodes them (the slug as the nav's hrefs are read), so one ends with the other
|
|
22
|
+
const slug = navPath(m.page?.slug);
|
|
23
|
+
const pointer = typeof m.agentPointerHref === "string" && !m.agentPointerHref.startsWith("//") ? navPath(m.agentPointerHref.replace(/\.md(?=$|[?#])/i, "")) : null;
|
|
24
|
+
if (!slug || !pointer)
|
|
25
|
+
return null;
|
|
26
|
+
const path = pointer;
|
|
27
|
+
let base;
|
|
28
|
+
if (slug === "/")
|
|
29
|
+
base = path.replace(/\/index$/i, "");
|
|
30
|
+
else if (path.endsWith(slug))
|
|
31
|
+
base = path.slice(0, path.length - slug.length);
|
|
32
|
+
if (base === undefined || (base && (!/^(?:\/[^/?#\\]+)+$/.test(base) || base.split("/").some(isDotSegment))))
|
|
33
|
+
return null;
|
|
34
|
+
return { nav: m.nav, siteName: typeof m.siteName === "string" ? m.siteName : undefined, base, slug };
|
|
35
|
+
}
|
|
36
|
+
/** Paths on a Mintlify site that are never pages. */
|
|
37
|
+
// `/mcp` is the site's MCP server; a page may live under `/mcp/` (Cline's `/mcp/mcp-overview`)
|
|
38
|
+
const NOT_PAGES = /^\/(?:(?:_next|_mintlify|mintlify-assets)(?:[/.?]|$)|(?:llms(?:-full)?\.txt|sitemap[^/]*|robots\.txt|mcp)$)/i;
|
|
39
|
+
/** A page's key: its path on the site, no `.md`, no trailing slash. */
|
|
40
|
+
function pathKey(pathname) {
|
|
41
|
+
const p = pathname.replace(/\.md$/i, "").replace(/\/+$/, "");
|
|
42
|
+
return p || "/";
|
|
43
|
+
}
|
|
44
|
+
const DIVISIONS = ["languages", "versions", "products", "dropdowns", "tabs", "anchors", "menu"];
|
|
45
|
+
const LABELS = ["tab", "item", "anchor", "dropdown", "product", "version", "language"];
|
|
46
|
+
const MAX_DEPTH = 32;
|
|
47
|
+
const MAX_NODES = 100_000;
|
|
48
|
+
const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
49
|
+
/** A nav href (from the base) as a path (`""` is the home page, `/`), or null when it is not one of the site's pages. */
|
|
50
|
+
function navPath(href) {
|
|
51
|
+
// The site's home page is listed as `""` (CrewAI, Fireworks, 2026-11-24)
|
|
52
|
+
if (href === "")
|
|
53
|
+
return "/";
|
|
54
|
+
if (typeof href !== "string" || !href.startsWith("/") || href.startsWith("//") || href.includes("\\") || href.length > 2048)
|
|
55
|
+
return null;
|
|
56
|
+
const p = href.replace(/[?#].*$/, "");
|
|
57
|
+
// A dot segment, written or encoded (`%2e%2e`, which a URL resolves), is no page under the base
|
|
58
|
+
if (p.split("/").some(isDotSegment))
|
|
59
|
+
return null;
|
|
60
|
+
// Encoded as a link's URL is (`/my page` and `/café` as `keyOf` gives them), so both name a page alike
|
|
61
|
+
let path;
|
|
62
|
+
try {
|
|
63
|
+
path = new URL(p, "https://mintlify.invalid").pathname;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return path === "/" ? "/" : path.replace(/\/+$/, "");
|
|
69
|
+
}
|
|
70
|
+
/** `.` or `..`, however it is written. */
|
|
71
|
+
function isDotSegment(s) {
|
|
72
|
+
return /^(?:\.|%2e){1,2}$/i.test(s);
|
|
73
|
+
}
|
|
74
|
+
/** Every page href under a nav node (bounded). */
|
|
75
|
+
function hrefsUnder(node, budget, out, depth = 0) {
|
|
76
|
+
if (depth > MAX_DEPTH || --budget.left < 0)
|
|
77
|
+
return out;
|
|
78
|
+
if (Array.isArray(node))
|
|
79
|
+
for (const v of node)
|
|
80
|
+
hrefsUnder(v, budget, out, depth + 1);
|
|
81
|
+
else if (isObj(node)) {
|
|
82
|
+
const p = navPath(node.href);
|
|
83
|
+
if (p && typeof node.group !== "string")
|
|
84
|
+
out.add(p);
|
|
85
|
+
for (const [k, v] of Object.entries(node))
|
|
86
|
+
if (k !== "href" && (Array.isArray(v) || isObj(v)))
|
|
87
|
+
hrefsUnder(v, budget, out, depth + 1);
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The part of the sidebar a page is in, and its name: from the top, the division holding the
|
|
93
|
+
* page at each level (the one with most pages, when its first page is also another's folded
|
|
94
|
+
* entry), until a node with no division holding it.
|
|
95
|
+
*/
|
|
96
|
+
function bookNode(nav, slug) {
|
|
97
|
+
if (!isObj(nav))
|
|
98
|
+
return null;
|
|
99
|
+
const budget = { left: MAX_NODES };
|
|
100
|
+
let node = nav;
|
|
101
|
+
let label;
|
|
102
|
+
for (let depth = 0; depth < MAX_DEPTH; depth++) {
|
|
103
|
+
let next;
|
|
104
|
+
// (`global`: links shown on every part, Zapier's changelog anchor)
|
|
105
|
+
const lists = DIVISIONS.flatMap((key) => [node[key], isObj(node.global) ? node.global[key] : undefined]);
|
|
106
|
+
for (const list of lists) {
|
|
107
|
+
if (!Array.isArray(list))
|
|
108
|
+
continue;
|
|
109
|
+
for (const child of list) {
|
|
110
|
+
if (!isObj(child))
|
|
111
|
+
continue;
|
|
112
|
+
const under = hrefsUnder(child, budget, new Set());
|
|
113
|
+
if (under.has(slug) && (!next || under.size > next.size))
|
|
114
|
+
next = { node: child, size: under.size };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!next)
|
|
118
|
+
break;
|
|
119
|
+
node = next.node;
|
|
120
|
+
const l = LABELS.map((k) => node[k]).find((v) => typeof v === "string" && v.trim());
|
|
121
|
+
if (typeof l === "string")
|
|
122
|
+
label = l.trim();
|
|
123
|
+
}
|
|
124
|
+
return { node, label };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The book's contents: the page's part of the sidebar as one book (its first page the book's,
|
|
128
|
+
* everything else under it), in a section named after the site. A site's home page the sidebar
|
|
129
|
+
* does not list (a custom landing page: LangChain, Novu, 2026-11-24) is a book of its own: which
|
|
130
|
+
* part a reader wants is not on it (every part is folded there), so they open a page of it.
|
|
131
|
+
*/
|
|
132
|
+
export function mintlifyContents(origin, data) {
|
|
133
|
+
const slug = navPath(data.slug) ?? data.slug;
|
|
134
|
+
const all = hrefsUnder(data.nav, { left: MAX_NODES }, new Set());
|
|
135
|
+
const index = slug === "/" ? "/index" : `${slug}/index`;
|
|
136
|
+
const listed = all.has(slug) ? slug : all.has(index) ? index : null;
|
|
137
|
+
const found = listed === null ? (slug === "/" && isObj(data.nav) ? { node: {}, label: undefined } : null) : bookNode(data.nav, listed);
|
|
138
|
+
if (!found)
|
|
139
|
+
return [];
|
|
140
|
+
const key = (p) => pathKey(data.base + (p === "/" ? "" : p)) || "/";
|
|
141
|
+
const md = (p) => `${origin}${data.base}${p === "/" ? "/index" : p}.md`;
|
|
142
|
+
let nodes = 0;
|
|
143
|
+
const entryOf = (v, depth) => {
|
|
144
|
+
if (!isObj(v) || depth > MAX_DEPTH || ++nodes > MAX_NODES)
|
|
145
|
+
return null;
|
|
146
|
+
if (typeof v.group === "string") {
|
|
147
|
+
const children = (Array.isArray(v.pages) ? v.pages : []).map((c) => entryOf(c, depth + 1)).filter((e) => !!e);
|
|
148
|
+
// A group with a page of its own (`root`): that page is the group's
|
|
149
|
+
const own = navPath(v.href);
|
|
150
|
+
if (own)
|
|
151
|
+
return { title: v.group, key: key(own), aliases: aliasesOf(own, key), fetchUrl: md(own), children };
|
|
152
|
+
return children.length ? { title: v.group, key: null, children } : null;
|
|
153
|
+
}
|
|
154
|
+
const p = navPath(v.href);
|
|
155
|
+
if (!p)
|
|
156
|
+
return null;
|
|
157
|
+
const title = [v.sidebarTitle, v.title].find((t) => typeof t === "string" && !!t.trim())?.trim() ?? p;
|
|
158
|
+
return { title, key: key(p), aliases: aliasesOf(p, key), fetchUrl: md(p), children: [] };
|
|
159
|
+
};
|
|
160
|
+
const node = found.node;
|
|
161
|
+
const list = Array.isArray(node.groups) && node.groups.length ? node.groups : Array.isArray(node.pages) ? node.pages : [];
|
|
162
|
+
const top = list.map((v) => entryOf(v, 0)).filter((e) => !!e);
|
|
163
|
+
// A part that is one page (an anchor or tab linking a page: Zapier's changelog)
|
|
164
|
+
const own = navPath(node.href);
|
|
165
|
+
if (!top.length && own)
|
|
166
|
+
top.push({ title: found.label || own, key: key(own), aliases: aliasesOf(own, key), fetchUrl: md(own), children: [] });
|
|
167
|
+
const title = found.label || data.siteName;
|
|
168
|
+
if (listed === null) {
|
|
169
|
+
// The home page, alone
|
|
170
|
+
const home = { title: data.siteName || "Home", key: key("/"), aliases: aliasesOf("/", key), fetchUrl: md("/"), children: [] };
|
|
171
|
+
return [{ title: data.siteName || home.title, key: null, children: [home] }];
|
|
172
|
+
}
|
|
173
|
+
// The first page is the book's own; it leaves the group it was in (a group left empty goes)
|
|
174
|
+
let first;
|
|
175
|
+
const lift = (es) => es.flatMap((e) => {
|
|
176
|
+
if (!first && e.key) {
|
|
177
|
+
first = e;
|
|
178
|
+
return e.children;
|
|
179
|
+
}
|
|
180
|
+
const children = lift(e.children);
|
|
181
|
+
return e.key === null && !children.length ? [] : [{ ...e, children }];
|
|
182
|
+
});
|
|
183
|
+
const rest = lift(top);
|
|
184
|
+
const book = first;
|
|
185
|
+
if (!book)
|
|
186
|
+
return [];
|
|
187
|
+
const entry = { ...book, title: title || book.title, children: rest };
|
|
188
|
+
return [{ title: data.siteName || entry.title, key: null, children: [entry] }];
|
|
189
|
+
}
|
|
190
|
+
const CARD = ".card";
|
|
191
|
+
const CARD_TITLE = '[data-component-part="card-title"]';
|
|
192
|
+
/** Cards with a title, in document order. */
|
|
193
|
+
function titledCards($) {
|
|
194
|
+
return $(CARD).filter((_, c) => $(c).find(CARD_TITLE).length > 0).toArray();
|
|
195
|
+
}
|
|
196
|
+
/** Has a card a link of its own (an `<a>` card), or one already filled in? */
|
|
197
|
+
const linked = ($, c) => $(c).is("a[href]") || !!$(c).attr(CARD_HREF);
|
|
198
|
+
/** Cards with a title and no link of their own. */
|
|
199
|
+
function unlinkedCards($) {
|
|
200
|
+
return titledCards($).filter((c) => !linked($, c));
|
|
201
|
+
}
|
|
202
|
+
/** Where a card's link, taken from the page's `.md`, is put for the `mintlify` profile. */
|
|
203
|
+
export const CARD_HREF = "data-mintlify-card-href";
|
|
204
|
+
const MAX_CARD_LINES = 100_000;
|
|
205
|
+
/** How deep a card line may be quoted (a card in a tab in an accordion…). */
|
|
206
|
+
const MAX_QUOTES = 24;
|
|
207
|
+
const MAX_TITLE = 1000;
|
|
208
|
+
/**
|
|
209
|
+
* A title as both sources can be compared: the `.md`'s Markdown (`**`, `` ` ``, `_`, links,
|
|
210
|
+
* backslash escapes) and the HTML's text both reduced to their words. At most MAX_TITLE
|
|
211
|
+
* characters, so the patterns stay cheap.
|
|
212
|
+
*/
|
|
213
|
+
function cardKey(title) {
|
|
214
|
+
// A card's title is short; past this it is no title (and the link pattern stays cheap)
|
|
215
|
+
if (title.length > MAX_TITLE)
|
|
216
|
+
return "";
|
|
217
|
+
return title
|
|
218
|
+
.replace(/\\(.)/g, "$1")
|
|
219
|
+
.replace(/[*_`~]+/g, "")
|
|
220
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
221
|
+
.replace(/\s+/g, " ")
|
|
222
|
+
.trim()
|
|
223
|
+
.toLowerCase();
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Each card's link, from the `.md`'s card lines, `> [card] [Title](href)` (or `> [card] Title`,
|
|
227
|
+
* a card with no link), at any quote depth: matched by title, in document order, so every card
|
|
228
|
+
* with a title takes its turn, linked or not, and only one with no link of its own is given
|
|
229
|
+
* one. Links on the site lose their `.md` and are resolved against the `.md`'s address
|
|
230
|
+
* (`url`); others are kept as written. A card the `.md` does not name stays unlinked. Linear.
|
|
231
|
+
*/
|
|
232
|
+
export function fillCardLinks($, text, url) {
|
|
233
|
+
let base;
|
|
234
|
+
try {
|
|
235
|
+
base = url ? new URL(url) : undefined;
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
base = undefined;
|
|
239
|
+
}
|
|
240
|
+
const byTitle = new Map();
|
|
241
|
+
let n = 0;
|
|
242
|
+
for (const raw of text.split("\n")) {
|
|
243
|
+
if (++n > MAX_CARD_LINES)
|
|
244
|
+
break;
|
|
245
|
+
// The quote markers, at most MAX_QUOTES of them
|
|
246
|
+
let i = 0, quotes = 0;
|
|
247
|
+
while (i < raw.length && quotes <= MAX_QUOTES) {
|
|
248
|
+
if (raw[i] === ">") {
|
|
249
|
+
quotes++;
|
|
250
|
+
i++;
|
|
251
|
+
}
|
|
252
|
+
else if (raw[i] === " " || raw[i] === "\t")
|
|
253
|
+
i++;
|
|
254
|
+
else
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (!quotes || quotes > MAX_QUOTES || !raw.startsWith("[card]", i))
|
|
258
|
+
continue;
|
|
259
|
+
const rest = raw.slice(i + 6).trim();
|
|
260
|
+
let title, href = null;
|
|
261
|
+
const close = rest.lastIndexOf("](");
|
|
262
|
+
if (rest.startsWith("[") && rest.endsWith(")") && close > 0) {
|
|
263
|
+
title = rest.slice(1, close);
|
|
264
|
+
href = rest.slice(close + 2, -1).trim();
|
|
265
|
+
if (!href || /\s/.test(href))
|
|
266
|
+
href = null;
|
|
267
|
+
}
|
|
268
|
+
else
|
|
269
|
+
title = rest;
|
|
270
|
+
const key = cardKey(title);
|
|
271
|
+
if (!key)
|
|
272
|
+
continue;
|
|
273
|
+
const list = byTitle.get(key) ?? [];
|
|
274
|
+
list.push(href === null ? null : siteHref(href, base));
|
|
275
|
+
byTitle.set(key, list);
|
|
276
|
+
}
|
|
277
|
+
if (!byTitle.size)
|
|
278
|
+
return;
|
|
279
|
+
for (const c of titledCards($)) {
|
|
280
|
+
const key = cardKey($(c).find(CARD_TITLE).first().text());
|
|
281
|
+
const href = byTitle.get(key)?.shift();
|
|
282
|
+
if (href && !linked($, c))
|
|
283
|
+
$(c).attr(CARD_HREF, href);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** A card's link: resolved against the `.md`'s address; on the same site, without its `.md`. */
|
|
287
|
+
function siteHref(href, base) {
|
|
288
|
+
if (!base)
|
|
289
|
+
return href;
|
|
290
|
+
let u;
|
|
291
|
+
try {
|
|
292
|
+
u = new URL(href, base);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return href;
|
|
296
|
+
}
|
|
297
|
+
if (u.origin !== base.origin)
|
|
298
|
+
return href;
|
|
299
|
+
u.pathname = u.pathname.replace(/\.md$/i, "");
|
|
300
|
+
return u.href;
|
|
301
|
+
}
|
|
302
|
+
function withoutFetchUrls(entries) {
|
|
303
|
+
return entries.map(({ fetchUrl: _, children, ...e }) => ({ ...e, children: withoutFetchUrls(children) }));
|
|
304
|
+
}
|
|
305
|
+
/** Other paths of a page: `/x/index` is also `/x`, and the reverse. */
|
|
306
|
+
function aliasesOf(p, key) {
|
|
307
|
+
if (p === "/")
|
|
308
|
+
return [key("/index")];
|
|
309
|
+
if (/\/index$/i.test(p))
|
|
310
|
+
return [key(p.replace(/\/index$/i, "") || "/")];
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
/** The adapter of one Mintlify site and part, from a page of it. */
|
|
314
|
+
export function mintlifySite(origin, data, read = "md") {
|
|
315
|
+
const html = read === "html";
|
|
316
|
+
const listed = mintlifyContents(origin, data);
|
|
317
|
+
// The HTML route fetches each page at its address; its `.md` only for what the HTML lacks
|
|
318
|
+
const contents = html ? withoutFetchUrls(listed) : listed;
|
|
319
|
+
const mdUrls = new Map();
|
|
320
|
+
const collect = (es) => { for (const e of es) {
|
|
321
|
+
if (e.key && e.fetchUrl)
|
|
322
|
+
mdUrls.set(e.key, e.fetchUrl);
|
|
323
|
+
collect(e.children);
|
|
324
|
+
} };
|
|
325
|
+
collect(listed);
|
|
326
|
+
const base = data.base;
|
|
327
|
+
const site = {
|
|
328
|
+
name: "mintlify",
|
|
329
|
+
profile: html ? "mintlify" : "generic",
|
|
330
|
+
delayMs: 1500,
|
|
331
|
+
matches: [],
|
|
332
|
+
detect: ($, url) => isMintlify($) && !!url && new URL(url).origin === origin,
|
|
333
|
+
readPage($, url) {
|
|
334
|
+
if (!url)
|
|
335
|
+
return null;
|
|
336
|
+
const key = site.keyOf(new URL(url));
|
|
337
|
+
if (!key)
|
|
338
|
+
return null;
|
|
339
|
+
// A page of the site (a 404 page has no content root)
|
|
340
|
+
if (html && !$(MINTLIFY_ROOT).length)
|
|
341
|
+
return null;
|
|
342
|
+
return { key, url: origin + key, root: html ? MINTLIFY_ROOT : "main", meta: data.siteName ? { site: data.siteName } : {} };
|
|
343
|
+
},
|
|
344
|
+
readContents: () => contents,
|
|
345
|
+
keyOf(url) {
|
|
346
|
+
if (url.origin !== origin)
|
|
347
|
+
return null;
|
|
348
|
+
const p = url.pathname;
|
|
349
|
+
if (base && p !== base && !p.startsWith(base + "/"))
|
|
350
|
+
return null;
|
|
351
|
+
// As a key reads it (no `.md`, no trailing slash): `/mcp/` and `/llms.txt.md` are not pages either
|
|
352
|
+
if (NOT_PAGES.test(pathKey(base ? p.slice(base.length) || "/" : p)))
|
|
353
|
+
return null;
|
|
354
|
+
return pathKey(p);
|
|
355
|
+
},
|
|
356
|
+
urlOf: (key) => origin + key,
|
|
357
|
+
fileOf(key) {
|
|
358
|
+
// The page's path from the base as one file name: letters, digits, `.`, `_`, `-` only
|
|
359
|
+
const rel = base && key.startsWith(base + "/") ? key.slice(base.length + 1) : key === base ? "" : key.replace(/^\//, "");
|
|
360
|
+
let decoded = rel;
|
|
361
|
+
try {
|
|
362
|
+
decoded = decodeURIComponent(rel);
|
|
363
|
+
}
|
|
364
|
+
catch { /* as written */ }
|
|
365
|
+
const name = decoded.replace(/\//g, "-").replace(/[^\p{L}\p{N}._-]/gu, "_").replace(/^[.]+/, "").slice(0, 120);
|
|
366
|
+
return `${name || "index"}.md`;
|
|
367
|
+
},
|
|
368
|
+
...(html
|
|
369
|
+
? {
|
|
370
|
+
// Cards: their links are not in the HTML (the page's script opens them), but are in the `.md`
|
|
371
|
+
textFor: ($, key) => (unlinkedCards($).length ? mdUrls.get(key) : undefined),
|
|
372
|
+
fillFromText: ($, text, url) => fillCardLinks($, text, url),
|
|
373
|
+
}
|
|
374
|
+
: { readText: (text) => readMintlifyText(text) }),
|
|
375
|
+
imageHost: (url) => url.protocol === "https:" && /(^|\.)mintcdn\.com$/i.test(url.hostname),
|
|
376
|
+
};
|
|
377
|
+
return site;
|
|
378
|
+
}
|
|
379
|
+
/** The adapter every Mintlify site gets bound from: detects, then `bind`s to the page's site. */
|
|
380
|
+
export const mintlify = {
|
|
381
|
+
name: "mintlify",
|
|
382
|
+
profile: "generic",
|
|
383
|
+
delayMs: 1500,
|
|
384
|
+
matches: [],
|
|
385
|
+
detect: ($) => isMintlify($),
|
|
386
|
+
bind($, url) {
|
|
387
|
+
if (!url || !isMintlify($))
|
|
388
|
+
return null;
|
|
389
|
+
try {
|
|
390
|
+
const data = mintlifyData($);
|
|
391
|
+
if (!data)
|
|
392
|
+
return null;
|
|
393
|
+
// Read from the page itself (what the reader sees); the `.md` only for what the HTML lacks
|
|
394
|
+
const site = mintlifySite(new URL(url).origin, data, "html");
|
|
395
|
+
return site.readContents?.($, { key: "", url, root: "", meta: {} }).length ? site : null;
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
// A manifest that does not read as Mintlify's: no adapter, not an error
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
readPage: () => null,
|
|
403
|
+
keyOf: () => null,
|
|
404
|
+
urlOf: (key) => key,
|
|
405
|
+
fileOf: (key) => `${key.split("/").pop() || "index"}.md`,
|
|
406
|
+
};
|
|
407
|
+
/** A page's `.md` as a tree, or null when it is Mintlify's "Page Not Found". */
|
|
408
|
+
export function readMintlifyText(text) {
|
|
409
|
+
// An HTML document is not the page's Markdown: a sidebar entry whose `.md` lands on another
|
|
410
|
+
// site's page (Claude Code's `en/claude-tag`, 2026-11-24) is missing, not a page of code
|
|
411
|
+
if (/^\s*<(?:!doctype\s+html|html)[\s>]/i.test(text.slice(0, 1024)))
|
|
412
|
+
return null;
|
|
413
|
+
const page = readMintlifyExport(text);
|
|
414
|
+
if (!page)
|
|
415
|
+
return null;
|
|
416
|
+
// HTML left in the export (tables whose cells hold links, `<kbd>`) read by the HTML core
|
|
417
|
+
return { tree: readRawHtml(page.tree), title: page.title, description: page.description, type: "topic", source: countMintlify(page.source) };
|
|
418
|
+
}
|
|
419
|
+
/** Longer lines are counted on their first this many characters (a page's text is untrusted). */
|
|
420
|
+
const MAX_LINE = 20_000;
|
|
421
|
+
/**
|
|
422
|
+
* What a Mintlify page's Markdown holds, for the fidelity report: headings, code blocks, tables,
|
|
423
|
+
* list items, images, callouts (alerts) and links, counted at any blockquote depth (tabs, cards
|
|
424
|
+
* and accordions are blockquotes), outside code. A card's title link counts as a link, as the
|
|
425
|
+
* card's `href` does in the tree. Every scan is linear.
|
|
426
|
+
*/
|
|
427
|
+
export function countMintlify(text) {
|
|
428
|
+
const c = { headings: 0, codeBlocks: 0, tables: 0, listItems: 0, images: 0, callouts: 0, links: 0 };
|
|
429
|
+
let fence = null;
|
|
430
|
+
let inTable = false;
|
|
431
|
+
let prevText = false;
|
|
432
|
+
// Links and images are counted per paragraph: a link's text can wrap onto the next line
|
|
433
|
+
let para = [];
|
|
434
|
+
const flush = () => {
|
|
435
|
+
if (!para.length)
|
|
436
|
+
return;
|
|
437
|
+
const joined = para.join(" ");
|
|
438
|
+
para = [];
|
|
439
|
+
const md = markdownLinks(joined.replace(/`[^`]*`/g, (span) => (span.includes("](") ? "" : span)));
|
|
440
|
+
c.images += md.images;
|
|
441
|
+
c.links += md.links;
|
|
442
|
+
for (const m of joined.replace(/`[^`]*`/g, "").matchAll(/(?<!\]\()<(?:https?|mailto):[^\s<>]*>/gi))
|
|
443
|
+
if (m[0])
|
|
444
|
+
c.links++;
|
|
445
|
+
};
|
|
446
|
+
for (const whole of text.split("\n")) {
|
|
447
|
+
const raw = whole.length > MAX_LINE ? whole.slice(0, MAX_LINE) : whole;
|
|
448
|
+
// The line inside its blockquotes
|
|
449
|
+
const line = raw.replace(/^(?:\s*>[ \t]?)+/, "");
|
|
450
|
+
const f = /^[ \t]*(`{3,}|~{3,})(.*)$/.exec(line);
|
|
451
|
+
if (fence) {
|
|
452
|
+
if (f && f[1][0] === fence[0] && f[1].length >= fence.length && !f[2].trim())
|
|
453
|
+
fence = null;
|
|
454
|
+
prevText = false;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (f) {
|
|
458
|
+
flush();
|
|
459
|
+
fence = f[1];
|
|
460
|
+
c.codeBlocks++;
|
|
461
|
+
inTable = false;
|
|
462
|
+
prevText = false;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
// A blank line, a heading, a list item or a table row starts a new run of text
|
|
466
|
+
if (!line.trim() || /^ {0,3}#|^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]|^\s*\|/.test(line))
|
|
467
|
+
flush();
|
|
468
|
+
if (line.trim())
|
|
469
|
+
para.push(line);
|
|
470
|
+
if (/^ {0,3}#{1,6}(?:[ \t]|$)/.test(line))
|
|
471
|
+
c.headings++;
|
|
472
|
+
if (/^\[!(?:NOTE|TIP|WARNING|CAUTION|IMPORTANT)\]/i.test(line.trim()))
|
|
473
|
+
c.callouts++;
|
|
474
|
+
const t = line.trim();
|
|
475
|
+
const row = t.length > 1 && t.startsWith("|") && t.endsWith("|");
|
|
476
|
+
if (row && !inTable)
|
|
477
|
+
c.tables++;
|
|
478
|
+
inTable = row;
|
|
479
|
+
// HTML tables and list items (the HTML core reads them), outside code spans
|
|
480
|
+
// (not one written as text, `\<table`: Devin's and Together's exports, 2026-11-24)
|
|
481
|
+
const tagText = line.replace(/`[^`]*`/g, "").toLowerCase();
|
|
482
|
+
for (const m of tagText.matchAll(/(?<!\\)<table[\s>]/g))
|
|
483
|
+
if (m[0])
|
|
484
|
+
c.tables++;
|
|
485
|
+
for (const m of tagText.matchAll(/(?<!\\)<li[\s>]/g))
|
|
486
|
+
if (m[0])
|
|
487
|
+
c.listItems++;
|
|
488
|
+
// A list item; an empty one (a marker alone) too, unless it follows a paragraph's text (it
|
|
489
|
+
// cannot interrupt one: `-` there underlines a heading)
|
|
490
|
+
const item = /^[ \t]*([-*+]|\d{1,9}[.)])[ \t]+\S/.test(line);
|
|
491
|
+
const empty = /^[ \t]*([-*+]|\d{1,9}[.)])[ \t]*$/.test(line);
|
|
492
|
+
// A line of `=` or `-` under a paragraph's text makes it a heading (setext)
|
|
493
|
+
if (prevText && /^ {0,3}(?:=+|-+)[ \t]*$/.test(line))
|
|
494
|
+
c.headings++;
|
|
495
|
+
else if (item || (empty && !prevText))
|
|
496
|
+
c.listItems++;
|
|
497
|
+
prevText = !!line.trim() && !row && !item && !(empty && !prevText) && !/^ {0,3}#/.test(line);
|
|
498
|
+
}
|
|
499
|
+
flush();
|
|
500
|
+
return c;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* `[label](dest` and `](href)`, a badge or a button) is an image and a link.
|
|
505
|
+
*/
|
|
506
|
+
function markdownLinks(line) {
|
|
507
|
+
let links = 0, images = 0;
|
|
508
|
+
const open = [];
|
|
509
|
+
for (let i = 0; i < line.length; i++) {
|
|
510
|
+
const ch = line[i];
|
|
511
|
+
if (ch === "\\")
|
|
512
|
+
i++;
|
|
513
|
+
else if (ch === "[") {
|
|
514
|
+
if (open.length < 64)
|
|
515
|
+
open.push(i);
|
|
516
|
+
}
|
|
517
|
+
else if (ch === "]" && open.length) {
|
|
518
|
+
const at = open.pop();
|
|
519
|
+
const dest = line[i + 1] === "(" ? line[i + 2] : undefined;
|
|
520
|
+
if (dest && dest !== ")" && !/\s/.test(dest)) {
|
|
521
|
+
if (line[at - 1] === "!")
|
|
522
|
+
images++;
|
|
523
|
+
else if (i > at + 1)
|
|
524
|
+
links++;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return { links, images };
|
|
529
|
+
}
|
package/dist/site/types.d.ts
CHANGED
|
@@ -112,6 +112,14 @@ export interface SiteAdapter {
|
|
|
112
112
|
* hand) and gives it to `readText`.
|
|
113
113
|
*/
|
|
114
114
|
pageFor?(text: string): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* A page read from its HTML whose HTML lacks something the site's own text of it has (Mintlify:
|
|
117
|
+
* cards, whose links only the page's script knows): where that text is, else undefined.
|
|
118
|
+
* importBook then fetches it after the page (same delay) and hands it to `fillFromText`.
|
|
119
|
+
*/
|
|
120
|
+
textFor?($: CheerioAPI, key: string): string | undefined;
|
|
121
|
+
/** Put into the page's HTML, in place, what `textFor`'s text (fetched from `url`) says that the HTML does not. */
|
|
122
|
+
fillFromText?($: CheerioAPI, text: string, url: string): void;
|
|
115
123
|
/**
|
|
116
124
|
* Images on a host other than the site's own that a book keeps (GitBook's file host, which
|
|
117
125
|
* lets any site read it). Fetched without the reader's cookies, as any cross-site request.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/format-html",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.184",
|
|
4
4
|
"description": "HTML importer for Dogsbay: semantic HTML to TreeNode[], generator profiles (DITA-OT HTML5 first), site adapters (docs.redhat.com first) and a per-page fidelity report. Never fetches; runs in Node, browsers and Workers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,9 +34,10 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"cheerio": "^1.2.0",
|
|
37
|
-
"@dogsbay/types": "0.2.0-beta.
|
|
38
|
-
"@dogsbay/format-
|
|
39
|
-
"@dogsbay/format-
|
|
37
|
+
"@dogsbay/types": "0.2.0-beta.184",
|
|
38
|
+
"@dogsbay/format-mdx": "0.2.0-beta.184",
|
|
39
|
+
"@dogsbay/format-dogsbay-md": "0.2.0-beta.184",
|
|
40
|
+
"@dogsbay/format-gitbook": "0.2.0-beta.184"
|
|
40
41
|
},
|
|
41
42
|
"license": "MIT",
|
|
42
43
|
"repository": {
|