@dogsbay/format-html 0.0.0-stage → 0.2.0-beta.179
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/README.md +59 -2
- package/dist/convert.d.ts +56 -0
- package/dist/convert.js +108 -0
- package/dist/core/blocks.d.ts +50 -0
- package/dist/core/blocks.js +372 -0
- package/dist/core/fidelity.d.ts +34 -0
- package/dist/core/fidelity.js +105 -0
- package/dist/core/inline.d.ts +34 -0
- package/dist/core/inline.js +149 -0
- package/dist/dom.d.ts +24 -0
- package/dist/dom.js +34 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +6 -0
- package/dist/profiles/broadcom-techdocs.d.ts +2 -0
- package/dist/profiles/broadcom-techdocs.js +105 -0
- package/dist/profiles/dita-ot-html5.d.ts +2 -0
- package/dist/profiles/dita-ot-html5.js +168 -0
- package/dist/profiles/generic.d.ts +3 -0
- package/dist/profiles/generic.js +18 -0
- package/dist/profiles/index.d.ts +11 -0
- package/dist/profiles/index.js +12 -0
- package/dist/profiles/types.d.ts +20 -0
- package/dist/profiles/types.js +1 -0
- package/dist/site/book.d.ts +127 -0
- package/dist/site/book.js +467 -0
- package/dist/site/ibm.d.ts +2 -0
- package/dist/site/ibm.js +168 -0
- package/dist/site/index.d.ts +17 -0
- package/dist/site/index.js +13 -0
- package/dist/site/redhat.d.ts +2 -0
- package/dist/site/redhat.js +127 -0
- package/dist/site/types.d.ts +76 -0
- package/dist/site/types.js +1 -0
- package/package.json +44 -3
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const ORIGIN = "https://docs.redhat.com";
|
|
2
|
+
const PATH = /^\/(?:([a-z]{2}(?:-[a-z]{2})?)\/)?documentation\/([^/]+)\/([^/]+)\/([^/?#]+)\/?$/;
|
|
3
|
+
const HOSTS = new Set(["docs.redhat.com", "localhost"]);
|
|
4
|
+
/** Red Hat's chrome inside the topic. */
|
|
5
|
+
const CHROME = [
|
|
6
|
+
"rh-tooltip", "rh-button", "rh-icon", ".rhdocs-table-show-more-btn", ".rhdocs-table-expand-btn",
|
|
7
|
+
".copy-link-tooltip", ".content-code-block-container-actions", "rh-alert.banner-wrapper",
|
|
8
|
+
];
|
|
9
|
+
/** The header of an rh-alert names the DITA note type. */
|
|
10
|
+
const ALERT_TYPES = {
|
|
11
|
+
note: "note", tip: "tip", important: "important", warning: "warning", caution: "caution", danger: "danger",
|
|
12
|
+
remember: "note", restriction: "warning", attention: "important", notice: "important",
|
|
13
|
+
};
|
|
14
|
+
const ALERT_STATES = { danger: "danger", warning: "warning", caution: "caution", info: "note", success: "tip", neutral: "note" };
|
|
15
|
+
export const redhat = {
|
|
16
|
+
name: "redhat",
|
|
17
|
+
profile: "dita-ot-html5",
|
|
18
|
+
delayMs: 1500,
|
|
19
|
+
matches: ["https://docs.redhat.com/*"],
|
|
20
|
+
licence: {
|
|
21
|
+
name: "CC BY-SA 4.0",
|
|
22
|
+
url: "https://creativecommons.org/licenses/by-sa/4.0/",
|
|
23
|
+
notice: "Red Hat documentation is licensed CC BY-SA 4.0; keep this attribution and licence with anything made from it.",
|
|
24
|
+
},
|
|
25
|
+
detect($, url) {
|
|
26
|
+
// A Red Hat URL shape alone is not enough (another site may use the same paths):
|
|
27
|
+
// the page was fetched from docs.redhat.com, or it carries Red Hat's components
|
|
28
|
+
const u = pageUrl($, url);
|
|
29
|
+
if (!u || redhat.keyOf(u) === null)
|
|
30
|
+
return false;
|
|
31
|
+
if (url && new URL(url).hostname === "docs.redhat.com")
|
|
32
|
+
return true;
|
|
33
|
+
return $(".topic-toc-list rh-accordion").length > 0 || $("rh-alert, rh-code-block, rh-table, rh-tooltip").length > 0;
|
|
34
|
+
},
|
|
35
|
+
readPage($, url) {
|
|
36
|
+
const u = pageUrl($, url);
|
|
37
|
+
const key = u && redhat.keyOf(u);
|
|
38
|
+
if (!key)
|
|
39
|
+
return null;
|
|
40
|
+
const article = $("article").first();
|
|
41
|
+
if (!article.length)
|
|
42
|
+
return null;
|
|
43
|
+
article.find(CHROME.join(",")).remove();
|
|
44
|
+
article.find("rh-alert").each((_, el) => {
|
|
45
|
+
const e = $(el);
|
|
46
|
+
const header = e.children('[slot="header"]').first();
|
|
47
|
+
const label = header.text().trim();
|
|
48
|
+
const type = ALERT_TYPES[label.toLowerCase()] ?? ALERT_STATES[e.attr("state") ?? ""] ?? "note";
|
|
49
|
+
header.remove();
|
|
50
|
+
const body = e.find(".note__body").first();
|
|
51
|
+
const node = el;
|
|
52
|
+
node.name = "div";
|
|
53
|
+
node.attribs = { "data-callout": type, ...(label && label.toLowerCase() !== type ? { "data-callout-title": label } : {}) };
|
|
54
|
+
if (body.length)
|
|
55
|
+
e.empty().append(body.contents());
|
|
56
|
+
});
|
|
57
|
+
// <h2><a href="#id" class="anchor-heading">Title</a></h2>: the self-link is the title
|
|
58
|
+
article.find("a.anchor-heading").each((_, el) => { $(el).replaceWith($(el).contents()); });
|
|
59
|
+
const m = PATH.exec(u.pathname);
|
|
60
|
+
return {
|
|
61
|
+
key,
|
|
62
|
+
url: redhat.urlOf(key),
|
|
63
|
+
root: "article",
|
|
64
|
+
meta: { lang: m[1] ?? "en", product: m[2], version: m[3], slug: m[4] },
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
readContents($, page) {
|
|
68
|
+
const base = new URL(page.url);
|
|
69
|
+
const acc = $(".topic-toc-list rh-accordion").first();
|
|
70
|
+
const items = (ul) => $(ul).children("li").map((_, li) => {
|
|
71
|
+
const a = $(li).children("div").children("a").first();
|
|
72
|
+
const href = a.attr("href") ?? "";
|
|
73
|
+
let key = null;
|
|
74
|
+
let fragment;
|
|
75
|
+
if (href) {
|
|
76
|
+
const u = new URL(href, base);
|
|
77
|
+
key = redhat.keyOf(u);
|
|
78
|
+
fragment = u.hash ? decodeURIComponent(u.hash.slice(1)) : undefined;
|
|
79
|
+
}
|
|
80
|
+
const children = $(li).children("ul").toArray().flatMap((u) => items(u));
|
|
81
|
+
const entry = { title: a.text().replace(/\s+/g, " ").trim(), key, children };
|
|
82
|
+
if (fragment)
|
|
83
|
+
entry.fragment = fragment;
|
|
84
|
+
return entry;
|
|
85
|
+
}).get();
|
|
86
|
+
return acc.children("rh-accordion-header").map((_, h) => ({
|
|
87
|
+
title: $(h).text().replace(/\s+/g, " ").trim(),
|
|
88
|
+
key: null,
|
|
89
|
+
children: $(h).next("rh-accordion-panel").children("ul").toArray().flatMap((u) => items(u)),
|
|
90
|
+
})).get();
|
|
91
|
+
},
|
|
92
|
+
keyOf(u) {
|
|
93
|
+
if (!HOSTS.has(u.hostname))
|
|
94
|
+
return null;
|
|
95
|
+
const m = PATH.exec(u.pathname);
|
|
96
|
+
return m ? `${m[1] ?? "en"}/documentation/${m[2]}/${m[3]}/${m[4]}` : null;
|
|
97
|
+
},
|
|
98
|
+
urlOf: (key) => `${ORIGIN}/${key}`,
|
|
99
|
+
// The slug, decoded, with anything a file name or a Markdown link target cannot carry made "_"
|
|
100
|
+
fileOf: (key) => {
|
|
101
|
+
const slug = key.split("/").pop();
|
|
102
|
+
let name;
|
|
103
|
+
try {
|
|
104
|
+
name = decodeURIComponent(slug);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
name = slug;
|
|
108
|
+
}
|
|
109
|
+
return `${name.replace(/[^\p{L}\p{N}._-]/gu, "_") || "page"}.md`;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
/** The page's URL: the one it was fetched from, else its canonical link's path on docs.redhat.com. */
|
|
113
|
+
function pageUrl($, url) {
|
|
114
|
+
try {
|
|
115
|
+
if (url)
|
|
116
|
+
return new URL(url);
|
|
117
|
+
const canonical = $('link[rel="canonical"]').attr("href");
|
|
118
|
+
if (!canonical)
|
|
119
|
+
return null;
|
|
120
|
+
// Red Hat's canonical links name localhost (2026); another host is another site
|
|
121
|
+
const c = new URL(canonical);
|
|
122
|
+
return HOSTS.has(c.hostname) ? new URL(c.pathname, ORIGIN) : null;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { CheerioAPI } from "cheerio";
|
|
2
|
+
/**
|
|
3
|
+
* One entry of a site's table of contents. `key` identifies a page (see
|
|
4
|
+
* SiteAdapter.keyOf); a section heading with no page of its own has none.
|
|
5
|
+
*/
|
|
6
|
+
export interface ContentsEntry {
|
|
7
|
+
title: string;
|
|
8
|
+
key: string | null;
|
|
9
|
+
/** A fragment in the entry's link (`page#section`): the entry is part of a page. */
|
|
10
|
+
fragment?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Other keys that name the same page: addresses that do not give the page's own (IBM's
|
|
13
|
+
* `?topic=<name>`), so a page read at one is found in the contents.
|
|
14
|
+
*/
|
|
15
|
+
aliases?: string[];
|
|
16
|
+
children: ContentsEntry[];
|
|
17
|
+
}
|
|
18
|
+
/** What a site adapter learns from one page, after preparing it. */
|
|
19
|
+
export interface SitePageInfo {
|
|
20
|
+
/** The page's identity; the same key keyOf gives its URL. */
|
|
21
|
+
key: string;
|
|
22
|
+
/** The page's absolute URL (relative links resolve against it). */
|
|
23
|
+
url: string;
|
|
24
|
+
/** CSS selector of the content root, for htmlToTree. */
|
|
25
|
+
root: string;
|
|
26
|
+
/** Site facts (product, version, language…) for front matter and the config. */
|
|
27
|
+
meta: Record<string, string>;
|
|
28
|
+
/** When the site says the page last changed (ISO date): the page's `updated` front matter. */
|
|
29
|
+
updated?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* One documentation site: its chrome, its contents, its URLs, how slowly to
|
|
33
|
+
* fetch it, and its licence. Adapters never fetch.
|
|
34
|
+
*/
|
|
35
|
+
export interface SiteAdapter {
|
|
36
|
+
name: string;
|
|
37
|
+
/** Is this page from this site? */
|
|
38
|
+
detect($: CheerioAPI, url?: string): boolean;
|
|
39
|
+
/** The generator profile its content is written in. */
|
|
40
|
+
profile: string;
|
|
41
|
+
/**
|
|
42
|
+
* Read the page's facts and prepare its content in place: remove the chrome,
|
|
43
|
+
* rewrite custom elements to plain HTML (callouts as `data-callout`). Null when
|
|
44
|
+
* the page is not one of the site's content pages.
|
|
45
|
+
*/
|
|
46
|
+
readPage($: CheerioAPI, url?: string): SitePageInfo | null;
|
|
47
|
+
/**
|
|
48
|
+
* The site's table of contents: from the page itself when every page carries it, else
|
|
49
|
+
* from what `contentsUrl` names (importBook fetches it): `$` is that response parsed as
|
|
50
|
+
* HTML, `source` its text (for contents served as JSON).
|
|
51
|
+
*/
|
|
52
|
+
readContents?($: CheerioAPI, page: SitePageInfo, source?: string): ContentsEntry[];
|
|
53
|
+
/** Where the contents are, for a site whose pages do not carry them (IBM's contents API). */
|
|
54
|
+
contentsUrl?(page: SitePageInfo): string | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Where to fetch a page's content, when not from its address (IBM: its content API, as
|
|
57
|
+
* IBM's own app does; the address gives browsers an app with no content in it). When set,
|
|
58
|
+
* the start page is fetched from here too: the page the reader is on only says which it is.
|
|
59
|
+
*/
|
|
60
|
+
fetchUrlOf?(key: string): string;
|
|
61
|
+
/** A URL's page key, or null when the URL is not a content page of this site. */
|
|
62
|
+
keyOf(url: URL): string | null;
|
|
63
|
+
/** The URL to fetch a page by key. */
|
|
64
|
+
urlOf(key: string): string;
|
|
65
|
+
/** File name (no folder) for a page in a project. */
|
|
66
|
+
fileOf(key: string): string;
|
|
67
|
+
/** Extension match patterns for the site's pages (a browser extension's content scripts). */
|
|
68
|
+
matches: string[];
|
|
69
|
+
/** Wait at least this long between two fetches. */
|
|
70
|
+
delayMs: number;
|
|
71
|
+
licence?: {
|
|
72
|
+
name: string;
|
|
73
|
+
url: string;
|
|
74
|
+
notice: string;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/format-html",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0-beta.179",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./site": {
|
|
14
|
+
"types": "./dist/site/index.d.ts",
|
|
15
|
+
"import": "./dist/site/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^25.9.5",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vitest": "^4.1.10"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"cheerio": "^1.2.0",
|
|
29
|
+
"@dogsbay/types": "0.2.0-beta.179",
|
|
30
|
+
"@dogsbay/format-dogsbay-md": "0.2.0-beta.179"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/dogsbay/dogsbay.git",
|
|
36
|
+
"directory": "packages/format-html"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/dogsbay/dogsbay/tree/main/packages/format-html",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/dogsbay/dogsbay/issues"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"test:watch": "vitest"
|
|
46
|
+
}
|
|
6
47
|
}
|