@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,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source against output, per page: how many of each countable thing the HTML has,
|
|
3
|
+
* and how many the tree has. A difference is a loss (or a gain) to look at; the
|
|
4
|
+
* report lists it rather than hiding it.
|
|
5
|
+
*/
|
|
6
|
+
import type { TreeNode } from "@dogsbay/types";
|
|
7
|
+
import { type DomNode } from "../dom.js";
|
|
8
|
+
export interface Counts {
|
|
9
|
+
headings: number;
|
|
10
|
+
codeBlocks: number;
|
|
11
|
+
tables: number;
|
|
12
|
+
listItems: number;
|
|
13
|
+
images: number;
|
|
14
|
+
callouts: number;
|
|
15
|
+
/**
|
|
16
|
+
* Links, counted in the source wherever they are, a skipped <nav> too: a topic's link lists
|
|
17
|
+
* dropped as chrome show up here. Not inside code blocks (a code block keeps text only).
|
|
18
|
+
*/
|
|
19
|
+
links: number;
|
|
20
|
+
}
|
|
21
|
+
export interface FidelityReport {
|
|
22
|
+
source: Counts;
|
|
23
|
+
output: Counts;
|
|
24
|
+
/** The kinds whose counts differ. Empty when the page converted whole. */
|
|
25
|
+
differs: Array<keyof Counts>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Count the source, skipping what the converter skips by rule (scripts, nav, hidden), except
|
|
29
|
+
* that an SVG shown to the reader is an image and a link inside a <nav> is still a link: what
|
|
30
|
+
* the converter drops of those is a loss to report.
|
|
31
|
+
*/
|
|
32
|
+
export declare function countSource(nodes: DomNode[]): Counts;
|
|
33
|
+
export declare function countTree(tree: TreeNode[]): Counts;
|
|
34
|
+
export declare function compare(source: Counts, output: Counts): FidelityReport;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { tagOf } from "../dom.js";
|
|
2
|
+
import { isSkipped } from "./blocks.js";
|
|
3
|
+
const zero = () => ({ headings: 0, codeBlocks: 0, tables: 0, listItems: 0, images: 0, callouts: 0, links: 0 });
|
|
4
|
+
/** A link the converter writes as one: an href (or a caller's `data-resolved-href`), and text or an image to carry it. */
|
|
5
|
+
const hasHref = (n) => tagOf(n) === "a" && !!(n.attribs?.href?.trim() || n.attribs?.["data-resolved-href"]?.trim()) && hasContent(n);
|
|
6
|
+
const hasContent = (n) => (n.children ?? []).some((c) => (c.type === "text" ? /\S/.test(c.data ?? "") : c.type === "tag" && (tagOf(c) === "img" || hasContent(c))));
|
|
7
|
+
const shown = (n) => n.attribs?.hidden === undefined && n.attribs?.["aria-hidden"] !== "true";
|
|
8
|
+
/**
|
|
9
|
+
* Count the source, skipping what the converter skips by rule (scripts, nav, hidden), except
|
|
10
|
+
* that an SVG shown to the reader is an image and a link inside a <nav> is still a link: what
|
|
11
|
+
* the converter drops of those is a loss to report.
|
|
12
|
+
*/
|
|
13
|
+
export function countSource(nodes) {
|
|
14
|
+
const c = zero();
|
|
15
|
+
const links = (ns) => {
|
|
16
|
+
for (const n of ns) {
|
|
17
|
+
if (n.type !== "tag" || !shown(n) || ["script", "style", "template", "pre"].includes(tagOf(n)))
|
|
18
|
+
continue;
|
|
19
|
+
if (hasHref(n))
|
|
20
|
+
c.links++;
|
|
21
|
+
links(n.children ?? []);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const rec = (ns, inList) => {
|
|
25
|
+
for (const n of ns) {
|
|
26
|
+
if (n.type !== "tag")
|
|
27
|
+
continue;
|
|
28
|
+
const name = tagOf(n);
|
|
29
|
+
if (name === "svg" && shown(n)) {
|
|
30
|
+
c.images++;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (name === "nav" && shown(n)) {
|
|
34
|
+
links(n.children ?? []);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (isSkipped(n))
|
|
38
|
+
continue;
|
|
39
|
+
if (hasHref(n))
|
|
40
|
+
c.links++;
|
|
41
|
+
if (/^h[1-6]$/.test(name))
|
|
42
|
+
c.headings++;
|
|
43
|
+
else if (name === "pre") {
|
|
44
|
+
c.codeBlocks++;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
else if (name === "table")
|
|
48
|
+
c.tables++;
|
|
49
|
+
else if (name === "li" && inList)
|
|
50
|
+
c.listItems++;
|
|
51
|
+
else if (name === "img")
|
|
52
|
+
c.images++;
|
|
53
|
+
if (n.attribs?.["data-callout"] !== undefined)
|
|
54
|
+
c.callouts++;
|
|
55
|
+
rec(n.children ?? [], name === "ul" || name === "ol" || name === "menu");
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
rec(nodes, false);
|
|
59
|
+
return c;
|
|
60
|
+
}
|
|
61
|
+
export function countTree(tree) {
|
|
62
|
+
const c = zero();
|
|
63
|
+
const inl = (ns) => {
|
|
64
|
+
for (const n of ns ?? []) {
|
|
65
|
+
if (n.type === "image")
|
|
66
|
+
c.images++;
|
|
67
|
+
else if (n.type === "link") {
|
|
68
|
+
c.links++;
|
|
69
|
+
inl(n.children);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const rec = (ns) => {
|
|
74
|
+
for (const n of ns) {
|
|
75
|
+
switch (n.type) {
|
|
76
|
+
case "heading":
|
|
77
|
+
c.headings++;
|
|
78
|
+
break;
|
|
79
|
+
case "code":
|
|
80
|
+
c.codeBlocks++;
|
|
81
|
+
break;
|
|
82
|
+
case "table":
|
|
83
|
+
c.tables++;
|
|
84
|
+
break;
|
|
85
|
+
case "list-item":
|
|
86
|
+
c.listItems++;
|
|
87
|
+
break;
|
|
88
|
+
case "image":
|
|
89
|
+
c.images++;
|
|
90
|
+
break;
|
|
91
|
+
case "callout":
|
|
92
|
+
c.callouts++;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
inl(n.inline);
|
|
96
|
+
rec(n.children ?? []);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
rec(tree);
|
|
100
|
+
return c;
|
|
101
|
+
}
|
|
102
|
+
export function compare(source, output) {
|
|
103
|
+
const differs = Object.keys(source).filter((k) => source[k] !== output[k]);
|
|
104
|
+
return { source, output, differs };
|
|
105
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline HTML (a node list) to InlineNode[]: the flat-annotation model, where bold,
|
|
3
|
+
* italic and strikethrough are flags on text spans and links, code, images and
|
|
4
|
+
* breaks are their own nodes.
|
|
5
|
+
*
|
|
6
|
+
* The core reads plain HTML only. A generator profile rewrites its own vocabulary
|
|
7
|
+
* first (DITA-OT's `span.codeph` becomes `<code>`), so nothing here knows a class name.
|
|
8
|
+
*/
|
|
9
|
+
import type { InlineNode } from "@dogsbay/types";
|
|
10
|
+
import { type DomNode } from "../dom.js";
|
|
11
|
+
export interface InlineOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Rewrites a link's href. Return undefined to drop the link and keep its text.
|
|
14
|
+
* The element is passed so a caller can read other attributes.
|
|
15
|
+
*/
|
|
16
|
+
resolveHref?: (href: string, el: DomNode) => string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Rewrites an image's src (to an asset path, an absolute URL, …). An image whose element
|
|
19
|
+
* carries `data-resolved-src` uses that value as is.
|
|
20
|
+
*/
|
|
21
|
+
onImage?: (img: {
|
|
22
|
+
src: string;
|
|
23
|
+
alt?: string;
|
|
24
|
+
attribs: Record<string, string>;
|
|
25
|
+
}) => string;
|
|
26
|
+
}
|
|
27
|
+
/** Convert a node list to InlineNode[], with the run's outer whitespace and breaks trimmed. */
|
|
28
|
+
export declare function inlineFrom(nodes: DomNode[] | undefined, opts?: InlineOptions): InlineNode[];
|
|
29
|
+
/**
|
|
30
|
+
* Trim whitespace and breaks at the edges of a run: a `<br>` that ends a list item
|
|
31
|
+
* or a paragraph would be written as a Markdown hard break (`\`) that the source
|
|
32
|
+
* does not show.
|
|
33
|
+
*/
|
|
34
|
+
export declare function trimEdges(nodes: InlineNode[]): InlineNode[];
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { tagOf, textContent } from "../dom.js";
|
|
2
|
+
/** Inline elements whose text is code. */
|
|
3
|
+
const CODE_TAGS = new Set(["code", "kbd", "samp", "tt"]);
|
|
4
|
+
/** Convert a node list to InlineNode[], with the run's outer whitespace and breaks trimmed. */
|
|
5
|
+
export function inlineFrom(nodes, opts = {}) {
|
|
6
|
+
const out = [];
|
|
7
|
+
walk(nodes ?? [], { bold: false, italic: false, strike: false }, out, opts);
|
|
8
|
+
return trimEdges(merge(out));
|
|
9
|
+
}
|
|
10
|
+
function walk(nodes, ann, out, opts) {
|
|
11
|
+
for (const n of nodes) {
|
|
12
|
+
if (n.type === "text") {
|
|
13
|
+
const text = (n.data ?? "").replace(/\s+/g, " ");
|
|
14
|
+
if (text)
|
|
15
|
+
out.push(makeText(text, ann));
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (n.type !== "tag")
|
|
19
|
+
continue;
|
|
20
|
+
const name = tagOf(n);
|
|
21
|
+
if (CODE_TAGS.has(name)) {
|
|
22
|
+
const text = textContent(n);
|
|
23
|
+
if (text)
|
|
24
|
+
out.push({ type: "code", text });
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
switch (name) {
|
|
28
|
+
case "strong":
|
|
29
|
+
case "b":
|
|
30
|
+
walk(n.children ?? [], { ...ann, bold: true }, out, opts);
|
|
31
|
+
break;
|
|
32
|
+
case "em":
|
|
33
|
+
case "i":
|
|
34
|
+
case "var":
|
|
35
|
+
case "cite":
|
|
36
|
+
case "dfn":
|
|
37
|
+
walk(n.children ?? [], { ...ann, italic: true }, out, opts);
|
|
38
|
+
break;
|
|
39
|
+
case "s":
|
|
40
|
+
case "del":
|
|
41
|
+
case "strike":
|
|
42
|
+
walk(n.children ?? [], { ...ann, strike: true }, out, opts);
|
|
43
|
+
break;
|
|
44
|
+
case "a": {
|
|
45
|
+
const fixed = n.attribs?.["data-resolved-href"];
|
|
46
|
+
const raw = n.attribs?.href;
|
|
47
|
+
const kids = [];
|
|
48
|
+
walk(n.children ?? [], ann, kids, opts);
|
|
49
|
+
const href = fixed ?? (raw ? (opts.resolveHref ? opts.resolveHref(raw, n) : raw) : undefined);
|
|
50
|
+
const merged = merge(kids);
|
|
51
|
+
const children = trimEdges(merged);
|
|
52
|
+
if (!href || !children.length) {
|
|
53
|
+
out.push(...kids);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
const title = n.attribs?.title?.replace(/\s+/g, " ").trim();
|
|
57
|
+
// Space inside the anchor's edges belongs outside the link: "the guide\n</a>to" is "the guide to"
|
|
58
|
+
const first = merged[0], last = merged[merged.length - 1];
|
|
59
|
+
if (first?.type === "text" && /^\s/.test(first.text))
|
|
60
|
+
out.push(makeText(" ", ann));
|
|
61
|
+
out.push(title ? { type: "link", href, title, children } : { type: "link", href, children });
|
|
62
|
+
if (last?.type === "text" && /\s$/.test(last.text))
|
|
63
|
+
out.push(makeText(" ", ann));
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case "img": {
|
|
67
|
+
const attribs = n.attribs ?? {};
|
|
68
|
+
const raw = attribs.src ?? "";
|
|
69
|
+
const alt = attribs.alt || undefined;
|
|
70
|
+
// `data-resolved-src`: the converter already placed the image (an SVG kept as a file)
|
|
71
|
+
const fixedSrc = attribs["data-resolved-src"];
|
|
72
|
+
const src = fixedSrc ?? (opts.onImage ? opts.onImage({ src: raw, alt, attribs }) : raw);
|
|
73
|
+
const img = { type: "image", src };
|
|
74
|
+
if (alt)
|
|
75
|
+
img.alt = alt;
|
|
76
|
+
if (attribs.title)
|
|
77
|
+
img.title = attribs.title;
|
|
78
|
+
out.push(img);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case "br":
|
|
82
|
+
out.push({ type: "break" });
|
|
83
|
+
break;
|
|
84
|
+
case "wbr":
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
// span, abbr, sup, sub, small, mark, q, time, custom elements: their content
|
|
88
|
+
walk(n.children ?? [], ann, out, opts);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function makeText(text, ann) {
|
|
93
|
+
const span = { type: "text", text };
|
|
94
|
+
if (ann.bold)
|
|
95
|
+
span.bold = true;
|
|
96
|
+
if (ann.italic)
|
|
97
|
+
span.italic = true;
|
|
98
|
+
if (ann.strike)
|
|
99
|
+
span.strikethrough = true;
|
|
100
|
+
return span;
|
|
101
|
+
}
|
|
102
|
+
const sameMarks = (a, b) => !!a.bold === !!b.bold && !!a.italic === !!b.italic && !!a.strikethrough === !!b.strikethrough;
|
|
103
|
+
/** Join neighbouring text spans with the same marks, and collapse the space between them. */
|
|
104
|
+
function merge(nodes) {
|
|
105
|
+
const out = [];
|
|
106
|
+
for (let n of nodes) {
|
|
107
|
+
const prev = out[out.length - 1];
|
|
108
|
+
// Source line breaks around a <br> are not the reader's: "title<br>\n text" is a break
|
|
109
|
+
// then "text", not a break then indented text
|
|
110
|
+
if (n.type === "text" && prev?.type === "break") {
|
|
111
|
+
n = { ...n, text: n.text.replace(/^\s+/, "") };
|
|
112
|
+
if (!n.text)
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (n.type === "break" && prev?.type === "text") {
|
|
116
|
+
const text = prev.text.replace(/[ \t\n]+$/, "");
|
|
117
|
+
if (text)
|
|
118
|
+
out[out.length - 1] = { ...prev, text };
|
|
119
|
+
else
|
|
120
|
+
out.pop();
|
|
121
|
+
}
|
|
122
|
+
if (n.type === "text" && prev?.type === "text" && sameMarks(prev, n)) {
|
|
123
|
+
out[out.length - 1] = { ...prev, text: (prev.text + n.text).replace(/ {2,}/g, " ") };
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
out.push(n);
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Trim whitespace and breaks at the edges of a run: a `<br>` that ends a list item
|
|
132
|
+
* or a paragraph would be written as a Markdown hard break (`\`) that the source
|
|
133
|
+
* does not show.
|
|
134
|
+
*/
|
|
135
|
+
export function trimEdges(nodes) {
|
|
136
|
+
const a = [...nodes];
|
|
137
|
+
const blank = (n) => !!n && (n.type === "break" || (n.type === "text" && n.text.trim() === ""));
|
|
138
|
+
while (blank(a[0]))
|
|
139
|
+
a.shift();
|
|
140
|
+
while (blank(a[a.length - 1]))
|
|
141
|
+
a.pop();
|
|
142
|
+
const first = a[0];
|
|
143
|
+
if (first?.type === "text")
|
|
144
|
+
a[0] = { ...first, text: first.text.replace(/^\s+/, "") };
|
|
145
|
+
const last = a[a.length - 1];
|
|
146
|
+
if (last?.type === "text")
|
|
147
|
+
a[a.length - 1] = { ...last, text: last.text.replace(/\s+$/, "") };
|
|
148
|
+
return a;
|
|
149
|
+
}
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal structural view of a domhandler node (what cheerio holds), so the
|
|
3
|
+
* converter does not depend on cheerio's types and works on any parser that
|
|
4
|
+
* produces the same shape.
|
|
5
|
+
*/
|
|
6
|
+
export interface DomNode {
|
|
7
|
+
/** "text" | "tag" | "comment" | "root" | "directive" | "cdata" | "script" | "style" */
|
|
8
|
+
type: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
data?: string;
|
|
11
|
+
attribs?: Record<string, string>;
|
|
12
|
+
children?: DomNode[];
|
|
13
|
+
}
|
|
14
|
+
export declare const tagOf: (n: DomNode) => string;
|
|
15
|
+
export declare const classOf: (n: DomNode) => string;
|
|
16
|
+
export declare const hasClass: (n: DomNode, cls: string) => boolean;
|
|
17
|
+
/** Element children only. */
|
|
18
|
+
export declare const elements: (n: DomNode) => DomNode[];
|
|
19
|
+
/** Plain text of a node and its descendants, whitespace collapsed and trimmed. */
|
|
20
|
+
export declare function textContent(n: DomNode): string;
|
|
21
|
+
/** Text of a node and its descendants as written (for code). `<br>` is a line break. */
|
|
22
|
+
export declare function rawText(n: DomNode, brAsNewline?: boolean): string;
|
|
23
|
+
/** Depth-first walk over element descendants (not the node itself). */
|
|
24
|
+
export declare function descendants(n: DomNode): Generator<DomNode>;
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const tagOf = (n) => (n.type === "tag" || n.type === "script" || n.type === "style" ? (n.name ?? "").toLowerCase() : "");
|
|
2
|
+
export const classOf = (n) => n.attribs?.class ?? "";
|
|
3
|
+
export const hasClass = (n, cls) => classOf(n).split(/\s+/).includes(cls);
|
|
4
|
+
/** Element children only. */
|
|
5
|
+
export const elements = (n) => (n.children ?? []).filter((c) => c.type === "tag");
|
|
6
|
+
/** Plain text of a node and its descendants, whitespace collapsed and trimmed. */
|
|
7
|
+
export function textContent(n) {
|
|
8
|
+
return rawText(n, false).replace(/\s+/g, " ").trim();
|
|
9
|
+
}
|
|
10
|
+
/** Text of a node and its descendants as written (for code). `<br>` is a line break. */
|
|
11
|
+
export function rawText(n, brAsNewline = true) {
|
|
12
|
+
let s = "";
|
|
13
|
+
const rec = (nodes) => {
|
|
14
|
+
for (const c of nodes) {
|
|
15
|
+
if (c.type === "text")
|
|
16
|
+
s += c.data ?? "";
|
|
17
|
+
else if (brAsNewline && tagOf(c) === "br")
|
|
18
|
+
s += "\n";
|
|
19
|
+
else if (c.children)
|
|
20
|
+
rec(c.children);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
rec(n.children ?? []);
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
/** Depth-first walk over element descendants (not the node itself). */
|
|
27
|
+
export function* descendants(n) {
|
|
28
|
+
for (const c of n.children ?? []) {
|
|
29
|
+
if (c.type !== "tag")
|
|
30
|
+
continue;
|
|
31
|
+
yield c;
|
|
32
|
+
yield* descendants(c);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dogsbay/format-html
|
|
3
|
+
*
|
|
4
|
+
* HTML to Dogsbay's TreeNode[]. Three layers:
|
|
5
|
+
* - the core: plain semantic HTML to the tree, with a fidelity report per page;
|
|
6
|
+
* - generator profiles: a publishing tool's vocabulary to plain HTML (DITA-OT HTML5);
|
|
7
|
+
* - site adapters (to come): one site's chrome, contents and URLs.
|
|
8
|
+
*
|
|
9
|
+
* Never fetches. Give it HTML from wherever it comes from.
|
|
10
|
+
*/
|
|
11
|
+
export type { TreeNode, InlineNode } from "@dogsbay/types";
|
|
12
|
+
export { htmlToTree } from "./convert.js";
|
|
13
|
+
export type { HtmlToTreeOptions, HtmlToTreeResult, PageAsset } from "./convert.js";
|
|
14
|
+
export { blocksFrom, newLedger, isBlock, escapeCaption, youtubeIdFromUrl } from "./core/blocks.js";
|
|
15
|
+
export type { BlockOptions, IdLedger } from "./core/blocks.js";
|
|
16
|
+
export { inlineFrom, trimEdges } from "./core/inline.js";
|
|
17
|
+
export type { InlineOptions } from "./core/inline.js";
|
|
18
|
+
export { countSource, countTree, compare } from "./core/fidelity.js";
|
|
19
|
+
export type { Counts, FidelityReport } from "./core/fidelity.js";
|
|
20
|
+
export { PROFILES, detectProfile, profileByName, broadcomTechdocs, ditaOtHtml5, generic } from "./profiles/index.js";
|
|
21
|
+
export type { Profile } from "./profiles/types.js";
|
|
22
|
+
export { textContent, rawText } from "./dom.js";
|
|
23
|
+
export type { DomNode } from "./dom.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { htmlToTree } from "./convert.js";
|
|
2
|
+
export { blocksFrom, newLedger, isBlock, escapeCaption, youtubeIdFromUrl } from "./core/blocks.js";
|
|
3
|
+
export { inlineFrom, trimEdges } from "./core/inline.js";
|
|
4
|
+
export { countSource, countTree, compare } from "./core/fidelity.js";
|
|
5
|
+
export { PROFILES, detectProfile, profileByName, broadcomTechdocs, ditaOtHtml5, generic } from "./profiles/index.js";
|
|
6
|
+
export { textContent, rawText } from "./dom.js";
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { ditaOtHtml5 } from "./dita-ot-html5.js";
|
|
2
|
+
const INLINE_WRAPPER = /^\s*display\s*:\s*inline\s*;?\s*$/i;
|
|
3
|
+
/** DITA's note types, as DITA-OT's note rule knows them. */
|
|
4
|
+
const NOTE_TYPES = ["note", "tip", "important", "warning", "caution", "danger", "attention", "notice", "remember", "restriction", "fastpath", "trouble", "other"];
|
|
5
|
+
const TYPES = ["concept", "task", "reference", "glossentry", "troubleshooting"];
|
|
6
|
+
export const broadcomTechdocs = {
|
|
7
|
+
name: "broadcom-techdocs",
|
|
8
|
+
// A topic page, or a page of TechDocs' own (a product's landing page: a card grid in div.dita
|
|
9
|
+
// under the page title), so neither falls to the generic profile and takes the site's chrome
|
|
10
|
+
detect($) {
|
|
11
|
+
if (!$("div.dita").length)
|
|
12
|
+
return false;
|
|
13
|
+
return $("div.dita div.topic > h1.title").length > 0 || $("h1.main-mid-top-title").length > 0;
|
|
14
|
+
},
|
|
15
|
+
root($) {
|
|
16
|
+
const r = $("div.dita").first();
|
|
17
|
+
return r.length ? r : undefined;
|
|
18
|
+
},
|
|
19
|
+
normalize($, root) {
|
|
20
|
+
// 1. The inline wrappers go, innermost first, their content kept as it is (text in <pre> too)
|
|
21
|
+
root.find("div[style]").toArray().reverse().forEach((el) => {
|
|
22
|
+
if (INLINE_WRAPPER.test(el.attribs?.style ?? ""))
|
|
23
|
+
$(el).replaceWith($(el).contents());
|
|
24
|
+
});
|
|
25
|
+
// 2. Titles: the outermost topic's h1 is DITA-OT's topictitle1. A title id built from its
|
|
26
|
+
// topic's (`<topic>_<id>`) gives way to the topic's id, kept on a marker so it is reported.
|
|
27
|
+
root.find("div.topic > h1.title").first().addClass("topictitle1");
|
|
28
|
+
// A section's title has an id of its own too (`<topic>_TITLE_…`), but links point at the
|
|
29
|
+
// section (`<topic>_SECTION_…`): the section's id anchors it.
|
|
30
|
+
root.find("div.topic[id], section[id]").each((_, el) => {
|
|
31
|
+
const id = el.attribs.id;
|
|
32
|
+
const title = $(el).children("h1, h2, h3, h4, h5, h6").first();
|
|
33
|
+
const own = title.attr("id");
|
|
34
|
+
if (own && (own.startsWith(`${id}_`) || el.name === "section")) {
|
|
35
|
+
title.removeAttr("id");
|
|
36
|
+
title.prepend($("<span>").attr("id", own));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
// 3. Paragraphs drawn as div.p: a <p> when they hold only text, else a container
|
|
40
|
+
root.find("div.p").each((_, el) => {
|
|
41
|
+
const d = el;
|
|
42
|
+
if (!$(el).find("ul, ol, dl, table, pre, div, figure, blockquote").length)
|
|
43
|
+
d.name = "p";
|
|
44
|
+
});
|
|
45
|
+
// 4. A note's type from its data-label: a DITA type (in any word of it: "IMPORTANT NOTE")
|
|
46
|
+
// is the class DITA-OT's note rule reads; any other label (a translated one) is the title
|
|
47
|
+
root.find("div.note[data-label]").each((_, el) => {
|
|
48
|
+
const label = ($(el).attr("data-label") ?? "").trim();
|
|
49
|
+
const type = label.toLowerCase().split(/\s+/).find((w) => NOTE_TYPES.includes(w));
|
|
50
|
+
if (type)
|
|
51
|
+
$(el).addClass(`note_${type}`);
|
|
52
|
+
else if (label)
|
|
53
|
+
$(el).attr("data-callout", "note").attr("data-callout-title", label[0] + label.slice(1).toLowerCase());
|
|
54
|
+
});
|
|
55
|
+
// 5. Link groups: child links (a linkpool inside a linkpool) become DITA-OT's child list,
|
|
56
|
+
// the link with its short description under it; other groups a plain list of links. The
|
|
57
|
+
// box's label ("Related information") becomes its heading line.
|
|
58
|
+
// Innermost groups first; only the run of links is replaced, as a parent link and the nested
|
|
59
|
+
// linkpool of child links can share one <div>.
|
|
60
|
+
root.find("a.link").parent().toArray().reverse().forEach((p) => {
|
|
61
|
+
const group = $(p);
|
|
62
|
+
const links = group.children("a.link");
|
|
63
|
+
if (!links.length)
|
|
64
|
+
return;
|
|
65
|
+
const children = group.parents(".linkpool").length > 1;
|
|
66
|
+
const ul = $("<ul>").addClass(children ? "ullinks" : "linklist");
|
|
67
|
+
// In place first: the links are moved into it below
|
|
68
|
+
links.first().before(ul);
|
|
69
|
+
links.each((_, a) => {
|
|
70
|
+
const link = $(a);
|
|
71
|
+
const li = $("<li>");
|
|
72
|
+
if (children) {
|
|
73
|
+
const desc = (link.attr("title") ?? "").replace(/\s+/g, " ").trim();
|
|
74
|
+
link.removeAttr("title");
|
|
75
|
+
li.addClass("ulchildlink").append($("<strong>").append(link));
|
|
76
|
+
if (desc)
|
|
77
|
+
li.append("<br>").append(textNode(desc, $));
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
li.append(link);
|
|
81
|
+
ul.append(li);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
root.find(".relatedlinks[data-label]").each((_, el) => {
|
|
85
|
+
$(el).prepend($("<p>").append($("<strong>").text($(el).attr("data-label"))));
|
|
86
|
+
});
|
|
87
|
+
// 6. Captions' data-label ("Figure: ", "Table: ") is a generated prefix the core does not
|
|
88
|
+
// read; nothing to do. The rest is DITA-OT's vocabulary.
|
|
89
|
+
ditaOtHtml5.normalize($, root);
|
|
90
|
+
},
|
|
91
|
+
description($, root) {
|
|
92
|
+
const text = root.find("div.shortdesc, p.shortdesc").first().text().replace(/\s+/g, " ").trim();
|
|
93
|
+
return text || undefined;
|
|
94
|
+
},
|
|
95
|
+
topicType($, root) {
|
|
96
|
+
const topic = root.find("div.topic").filter((_, e) => TYPES.some((t) => $(e).hasClass(t))).first();
|
|
97
|
+
if (!topic.length)
|
|
98
|
+
return root.find("div.topic").length ? "topic" : undefined;
|
|
99
|
+
return TYPES.find((t) => topic.hasClass(t));
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
/** Text as a text node (never parsed as HTML: a title attribute can hold `<`). */
|
|
103
|
+
function textNode(text, $) {
|
|
104
|
+
return $("<span>").text(text).contents();
|
|
105
|
+
}
|