webforai 4.0.0 → 4.1.1

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.
Files changed (113) hide show
  1. package/README.md +74 -69
  2. package/dist/bin.js +1895 -74
  3. package/dist/cjs/extract-mdast.js +58 -0
  4. package/dist/cjs/extractors/index.js +5 -0
  5. package/dist/cjs/extractors/lib/block-features.js +465 -0
  6. package/dist/cjs/extractors/lib/block-model.generated.js +45 -0
  7. package/dist/cjs/extractors/lib/block-model.js +72 -0
  8. package/dist/cjs/extractors/lib/blocks.js +212 -0
  9. package/dist/cjs/extractors/lib/code-tabs.js +191 -0
  10. package/dist/cjs/extractors/lib/constants.js +1 -1
  11. package/dist/cjs/extractors/lib/layout-tables.js +123 -0
  12. package/dist/cjs/extractors/lib/sanitize.js +144 -28
  13. package/dist/cjs/extractors/lib/takumi-signal.js +76 -0
  14. package/dist/cjs/extractors/lib/ui-chrome.js +7 -0
  15. package/dist/cjs/extractors/presets/auto.js +3 -3
  16. package/dist/cjs/extractors/presets/kiwame.js +231 -0
  17. package/dist/cjs/extractors/presets/takumi.js +34 -3
  18. package/dist/cjs/html-to-markdown.js +58 -4
  19. package/dist/cjs/html-to-mdast.js +2 -2
  20. package/dist/cjs/index.js +4 -0
  21. package/dist/cjs/mdast-handlers/custom-code-handler.js +16 -4
  22. package/dist/cjs/mdast-handlers/custom-div-handler.js +3 -3
  23. package/dist/cjs/mdast-handlers/custom-img-handler.js +10 -1
  24. package/dist/cjs/mdast-handlers/custom-table-handler.js +18 -1
  25. package/dist/cjs/mdast-handlers/table-cell-flatten.js +195 -0
  26. package/dist/cjs/mdast-handlers/twoslash.js +71 -0
  27. package/dist/cjs/metadata/index.js +4 -3
  28. package/dist/cjs/utils/hast-fast.js +1 -1
  29. package/dist/cjs/utils/parse-html.js +113 -0
  30. package/dist/cjs/utils/pre-text.js +104 -0
  31. package/dist/extract-mdast.js +58 -0
  32. package/dist/extractors/index.js +3 -0
  33. package/dist/extractors/lib/block-features.js +437 -0
  34. package/dist/extractors/lib/block-model.generated.js +21 -0
  35. package/dist/extractors/lib/block-model.js +48 -0
  36. package/dist/extractors/lib/blocks.js +185 -0
  37. package/dist/extractors/lib/code-tabs.js +168 -0
  38. package/dist/extractors/lib/constants.js +1 -1
  39. package/dist/extractors/lib/layout-tables.js +99 -0
  40. package/dist/extractors/lib/sanitize.js +142 -28
  41. package/dist/extractors/lib/takumi-signal.js +53 -0
  42. package/dist/extractors/lib/ui-chrome.js +5 -0
  43. package/dist/extractors/presets/auto.js +3 -3
  44. package/dist/extractors/presets/kiwame.js +206 -0
  45. package/dist/extractors/presets/takumi.js +34 -3
  46. package/dist/html-to-markdown.js +57 -4
  47. package/dist/html-to-mdast.js +2 -2
  48. package/dist/index.js +4 -0
  49. package/dist/mdast-handlers/custom-code-handler.js +15 -4
  50. package/dist/mdast-handlers/custom-div-handler.js +4 -4
  51. package/dist/mdast-handlers/custom-img-handler.js +10 -1
  52. package/dist/mdast-handlers/custom-table-handler.js +19 -2
  53. package/dist/mdast-handlers/table-cell-flatten.js +170 -0
  54. package/dist/mdast-handlers/twoslash.js +48 -0
  55. package/dist/metadata/index.js +4 -3
  56. package/dist/types/cli/commands/jobs/options.d.ts +2 -2
  57. package/dist/types/cli/commands/jobs/options.js.map +1 -1
  58. package/dist/types/cli/commands/webforai/options.js.map +1 -1
  59. package/dist/types/cli/commands/webforai/run.js.map +1 -1
  60. package/dist/types/cli/constants.d.ts +3 -1
  61. package/dist/types/cli/constants.js.map +1 -1
  62. package/dist/types/extract-mdast.js.map +1 -1
  63. package/dist/types/extractors/index.d.ts +1 -0
  64. package/dist/types/extractors/index.js.map +1 -1
  65. package/dist/types/extractors/lib/block-features.d.ts +43 -0
  66. package/dist/types/extractors/lib/block-features.js.map +1 -0
  67. package/dist/types/extractors/lib/block-model.d.ts +28 -0
  68. package/dist/types/extractors/lib/block-model.generated.d.ts +5 -0
  69. package/dist/types/extractors/lib/block-model.generated.js.map +1 -0
  70. package/dist/types/extractors/lib/block-model.js.map +1 -0
  71. package/dist/types/extractors/lib/blocks.d.ts +53 -0
  72. package/dist/types/extractors/lib/blocks.js.map +1 -0
  73. package/dist/types/extractors/lib/code-tabs.d.ts +30 -0
  74. package/dist/types/extractors/lib/code-tabs.js.map +1 -0
  75. package/dist/types/extractors/lib/constants.js.map +1 -1
  76. package/dist/types/extractors/lib/layout-tables.d.ts +17 -0
  77. package/dist/types/extractors/lib/layout-tables.js.map +1 -0
  78. package/dist/types/extractors/lib/sanitize.d.ts +33 -3
  79. package/dist/types/extractors/lib/sanitize.js.map +1 -1
  80. package/dist/types/extractors/lib/takumi-signal.d.ts +12 -0
  81. package/dist/types/extractors/lib/takumi-signal.js.map +1 -0
  82. package/dist/types/extractors/lib/ui-chrome.d.ts +8 -0
  83. package/dist/types/extractors/lib/ui-chrome.js.map +1 -1
  84. package/dist/types/extractors/presets/auto.d.ts +6 -1
  85. package/dist/types/extractors/presets/auto.js.map +1 -1
  86. package/dist/types/extractors/presets/kiwame.d.ts +31 -0
  87. package/dist/types/extractors/presets/kiwame.js.map +1 -0
  88. package/dist/types/extractors/presets/takumi.js.map +1 -1
  89. package/dist/types/html-to-markdown.d.ts +9 -0
  90. package/dist/types/html-to-markdown.js.map +1 -1
  91. package/dist/types/html-to-mdast.js.map +1 -1
  92. package/dist/types/index.d.ts +1 -1
  93. package/dist/types/index.js.map +1 -1
  94. package/dist/types/mdast-handlers/custom-code-handler.d.ts +7 -0
  95. package/dist/types/mdast-handlers/custom-code-handler.js.map +1 -1
  96. package/dist/types/mdast-handlers/custom-div-handler.js.map +1 -1
  97. package/dist/types/mdast-handlers/custom-img-handler.js.map +1 -1
  98. package/dist/types/mdast-handlers/custom-table-handler.js.map +1 -1
  99. package/dist/types/mdast-handlers/table-cell-flatten.d.ts +18 -0
  100. package/dist/types/mdast-handlers/table-cell-flatten.js.map +1 -0
  101. package/dist/types/mdast-handlers/twoslash.d.ts +14 -0
  102. package/dist/types/mdast-handlers/twoslash.js.map +1 -0
  103. package/dist/types/metadata/index.js.map +1 -1
  104. package/dist/types/utils/hast-fast.d.ts +1 -1
  105. package/dist/types/utils/hast-fast.js.map +1 -1
  106. package/dist/types/utils/parse-html.d.ts +33 -0
  107. package/dist/types/utils/parse-html.js.map +1 -0
  108. package/dist/types/utils/pre-text.d.ts +12 -0
  109. package/dist/types/utils/pre-text.js.map +1 -0
  110. package/dist/utils/hast-fast.js +1 -1
  111. package/dist/utils/parse-html.js +87 -0
  112. package/dist/utils/pre-text.js +81 -0
  113. package/package.json +3 -1
@@ -24,6 +24,7 @@ module.exports = __toCommonJS(custom_table_handler_exports);
24
24
  var import_hast_util_to_mdast = require("hast-util-to-mdast");
25
25
  var import_hast_util_to_text = require("hast-util-to-text");
26
26
  var import_hast_fast = require("../utils/hast-fast");
27
+ var import_table_cell_flatten = require("./table-cell-flatten");
27
28
  const MAX_SPAN = 1e3;
28
29
  const clampSpans = (node) => {
29
30
  (0, import_hast_fast.walk)(node, (candidate) => {
@@ -38,6 +39,14 @@ const clampSpans = (node) => {
38
39
  }
39
40
  });
40
41
  };
42
+ const startsWithHeaderRow = (table) => {
43
+ const row = (0, import_hast_fast.findElement)(table, (element) => element.tagName === "tr");
44
+ if (!row) {
45
+ return false;
46
+ }
47
+ const cells = row.children.filter(import_hast_fast.isElement);
48
+ return cells.length > 0 && cells.every((cell) => cell.tagName === "th");
49
+ };
41
50
  const customTableHandler = (options) => (state, node) => {
42
51
  if (options?.asText) {
43
52
  const paragraph = { type: "paragraph", children: [{ type: "text", value: (0, import_hast_util_to_text.toText)(node) }] };
@@ -45,7 +54,15 @@ const customTableHandler = (options) => (state, node) => {
45
54
  return paragraph;
46
55
  }
47
56
  clampSpans(node);
48
- return import_hast_util_to_mdast.defaultHandlers.table(state, node);
57
+ const result = import_hast_util_to_mdast.defaultHandlers.table(state, node);
58
+ if (!result || Array.isArray(result) || result.type !== "table") {
59
+ return result;
60
+ }
61
+ if ((0, import_table_cell_flatten.tableHasCodeSamples)(result)) {
62
+ return (0, import_table_cell_flatten.unfoldTable)(result, startsWithHeaderRow(node));
63
+ }
64
+ (0, import_table_cell_flatten.flattenTableCells)(result);
65
+ return result;
49
66
  };
50
67
  // Annotate the CommonJS export names for ESM import in node:
51
68
  0 && (module.exports = {
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var table_cell_flatten_exports = {};
20
+ __export(table_cell_flatten_exports, {
21
+ flattenTableCells: () => flattenTableCells,
22
+ tableHasCodeSamples: () => tableHasCodeSamples,
23
+ unfoldTable: () => unfoldTable
24
+ });
25
+ module.exports = __toCommonJS(table_cell_flatten_exports);
26
+ const PHRASING_TYPES = /* @__PURE__ */ new Set([
27
+ "text",
28
+ "emphasis",
29
+ "strong",
30
+ "delete",
31
+ "inlineCode",
32
+ "link",
33
+ "linkReference",
34
+ "image",
35
+ "imageReference",
36
+ "html",
37
+ "footnoteReference",
38
+ "inlineMath"
39
+ ]);
40
+ const PHRASING_PARENTS = /* @__PURE__ */ new Set(["emphasis", "strong", "delete", "link", "linkReference"]);
41
+ const lineBreak = () => ({ type: "html", value: "<br>" });
42
+ const needsFlattening = (nodes) => nodes.some((node) => {
43
+ if (node.type === "break") {
44
+ return true;
45
+ }
46
+ if (!PHRASING_TYPES.has(node.type)) {
47
+ return true;
48
+ }
49
+ if ((node.type === "text" || node.type === "inlineCode") && node.value.includes("\n")) {
50
+ return true;
51
+ }
52
+ return PHRASING_PARENTS.has(node.type) && "children" in node && needsFlattening(node.children);
53
+ });
54
+ class CellBuilder {
55
+ constructor() {
56
+ this.out = [];
57
+ this.#pendingBlock = false;
58
+ }
59
+ #pendingBlock;
60
+ /** Starts a new visual line when the previous block left content on the current one. */
61
+ block() {
62
+ const last = this.out.at(-1);
63
+ if (this.#pendingBlock && last && !(last.type === "html" && last.value === "<br>")) {
64
+ this.out.push(lineBreak());
65
+ }
66
+ this.#pendingBlock = true;
67
+ }
68
+ add(nodes) {
69
+ for (const node of nodes) {
70
+ this.#addOne(node);
71
+ }
72
+ }
73
+ #addOne(node) {
74
+ switch (node.type) {
75
+ case "break":
76
+ this.out.push(lineBreak());
77
+ return;
78
+ case "text":
79
+ this.out.push({ type: "text", value: node.value.replace(/\s*\n\s*/g, " ") });
80
+ return;
81
+ case "inlineCode":
82
+ this.#codeLines(node.value);
83
+ return;
84
+ case "code":
85
+ this.block();
86
+ this.#codeLines(node.value);
87
+ return;
88
+ case "math":
89
+ this.block();
90
+ this.out.push({ type: "inlineMath", value: node.value.replace(/\s*\n\s*/g, " ") });
91
+ return;
92
+ case "list":
93
+ node.children.forEach((item, index) => {
94
+ this.block();
95
+ const marker = node.ordered ? `${(node.start ?? 1) + index}. ` : "- ";
96
+ this.out.push({ type: "text", value: marker });
97
+ this.#pendingBlock = false;
98
+ this.add(item.children);
99
+ });
100
+ return;
101
+ case "thematicBreak":
102
+ return;
103
+ default:
104
+ this.#addContainer(node);
105
+ }
106
+ }
107
+ #addContainer(node) {
108
+ if (PHRASING_PARENTS.has(node.type) && "children" in node) {
109
+ const inner = new CellBuilder();
110
+ inner.add(node.children);
111
+ this.out.push({ ...node, children: inner.out });
112
+ return;
113
+ }
114
+ if (PHRASING_TYPES.has(node.type)) {
115
+ this.out.push(node);
116
+ return;
117
+ }
118
+ if ("children" in node) {
119
+ this.block();
120
+ this.add(node.children);
121
+ }
122
+ }
123
+ #codeLines(value) {
124
+ value.split("\n").forEach((line, index) => {
125
+ if (index > 0) {
126
+ this.out.push(lineBreak());
127
+ }
128
+ if (line.trim().length > 0) {
129
+ this.out.push({ type: "inlineCode", value: line });
130
+ }
131
+ });
132
+ }
133
+ }
134
+ const flattenTableCells = (table) => {
135
+ for (const row of table.children) {
136
+ for (const cell of row.children) {
137
+ if (!needsFlattening(cell.children)) {
138
+ continue;
139
+ }
140
+ const builder = new CellBuilder();
141
+ builder.add(cell.children);
142
+ cell.children = builder.out;
143
+ }
144
+ }
145
+ };
146
+ const hasMultilineCode = (nodes) => nodes.some(
147
+ (node) => node.type === "code" && node.value.includes("\n") || "children" in node && hasMultilineCode(node.children)
148
+ );
149
+ const tableHasCodeSamples = (table) => table.children.some((row) => row.children.some((cell) => hasMultilineCode(cell.children)));
150
+ const unfoldTable = (table, hasHeader) => {
151
+ const [first, ...rest] = table.children;
152
+ const labelled = hasHeader && rest.length > 0;
153
+ const labels = labelled && first ? first.children.map((cell) => cell.children) : [];
154
+ const rows = labelled ? rest : table.children;
155
+ const out = [];
156
+ rows.forEach((row, rowIndex) => {
157
+ if (rowIndex > 0) {
158
+ out.push({ type: "thematicBreak" });
159
+ }
160
+ row.children.forEach((cell, cellIndex) => {
161
+ const label = labels[cellIndex];
162
+ if (label && label.length > 0) {
163
+ out.push({ type: "paragraph", children: [{ type: "strong", children: label }] });
164
+ }
165
+ out.push(...asBlocks(cell.children));
166
+ });
167
+ });
168
+ return out;
169
+ };
170
+ const asBlocks = (nodes) => {
171
+ const blocks = [];
172
+ let run = [];
173
+ const flush = () => {
174
+ if (run.some((node) => node.type !== "text" || node.value.trim().length > 0)) {
175
+ blocks.push({ type: "paragraph", children: run });
176
+ }
177
+ run = [];
178
+ };
179
+ for (const node of nodes) {
180
+ if (PHRASING_TYPES.has(node.type) || node.type === "break") {
181
+ run.push(node);
182
+ } else {
183
+ flush();
184
+ blocks.push(node);
185
+ }
186
+ }
187
+ flush();
188
+ return blocks;
189
+ };
190
+ // Annotate the CommonJS export names for ESM import in node:
191
+ 0 && (module.exports = {
192
+ flattenTableCells,
193
+ tableHasCodeSamples,
194
+ unfoldTable
195
+ });
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var twoslash_exports = {};
20
+ __export(twoslash_exports, {
21
+ cleanTwoslash: () => cleanTwoslash
22
+ });
23
+ module.exports = __toCommonJS(twoslash_exports);
24
+ var import_hast_fast = require("../utils/hast-fast");
25
+ var import_pre_text = require("../utils/pre-text");
26
+ const cleanTwoslash = (pre) => {
27
+ if (!(0, import_hast_fast.classList)(pre).includes("twoslash")) {
28
+ return void 0;
29
+ }
30
+ let language;
31
+ const clean = (element) => {
32
+ element.children = element.children.flatMap((child) => {
33
+ if (!(0, import_hast_fast.isElement)(child)) {
34
+ return [child];
35
+ }
36
+ const classes = (0, import_hast_fast.classList)(child);
37
+ if (classes.includes("language-id")) {
38
+ language ||= (0, import_pre_text.preText)(child).trim() || void 0;
39
+ return [];
40
+ }
41
+ if (classes.includes("playground-link") || classes.includes("error-behind")) {
42
+ return [];
43
+ }
44
+ if (classes.includes("error")) {
45
+ return [errorLine(child)];
46
+ }
47
+ clean(child);
48
+ return [child];
49
+ });
50
+ };
51
+ clean(pre);
52
+ return language && /^[\w+#.-]+$/.test(language) ? language : void 0;
53
+ };
54
+ const errorLine = (error) => {
55
+ let code = "";
56
+ let message = "";
57
+ for (const child of error.children) {
58
+ if ((0, import_hast_fast.isElement)(child) && (0, import_hast_fast.classList)(child).includes("code")) {
59
+ code = (0, import_pre_text.preText)(child).trim();
60
+ } else {
61
+ message += (0, import_hast_fast.isElement)(child) ? (0, import_pre_text.preText)(child) : child.type === "text" ? child.value : "";
62
+ }
63
+ }
64
+ const prefix = /^\d+$/.test(code) ? `error TS${code}: ` : "error: ";
65
+ const lines = `${prefix}${message.trim()}`.split("\n").map((line) => `// ${line}`);
66
+ return { type: "element", tagName: "div", properties: {}, children: [{ type: "text", value: lines.join("\n") }] };
67
+ };
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ cleanTwoslash
71
+ });
@@ -116,9 +116,10 @@ const readName = (value) => {
116
116
  };
117
117
  const asString = (value) => typeof value === "string" && value.length > 0 ? value : void 0;
118
118
  const MAX_TITLE_LENGTH = 160;
119
- const asTitle = (value) => {
119
+ const MAX_DOCUMENT_TITLE_LENGTH = 300;
120
+ const asTitle = (value, maxLength = MAX_TITLE_LENGTH) => {
120
121
  const cleaned = clean(value);
121
- return cleaned && cleaned.length <= MAX_TITLE_LENGTH ? cleaned : void 0;
122
+ return cleaned && cleaned.length <= maxLength ? cleaned : void 0;
122
123
  };
123
124
  const clean = (value) => {
124
125
  if (value === void 0) {
@@ -184,7 +185,7 @@ const extractMetadata = (tree) => {
184
185
  }
185
186
  return void 0;
186
187
  };
187
- const title = asTitle(get("og:title", "twitter:title")) ?? asTitle(asString(article?.headline)) ?? asTitle(asString(article?.name)) ?? asTitle(documentTitle(tree)) ?? asTitle(firstHeading(tree));
188
+ const title = asTitle(get("og:title", "twitter:title")) ?? asTitle(asString(article?.headline)) ?? asTitle(asString(article?.name)) ?? asTitle(documentTitle(tree), MAX_DOCUMENT_TITLE_LENGTH) ?? asTitle(firstHeading(tree));
188
189
  return {
189
190
  title,
190
191
  description: clean(get("og:description", "twitter:description", "description")) ?? clean(asString(article?.description)),
@@ -136,7 +136,7 @@ const pruneInPlace = (tree, keep) => {
136
136
  let write = 0;
137
137
  for (let read = 0; read < children.length; read++) {
138
138
  const child = children[read];
139
- if (!keep(child)) {
139
+ if (!keep(child, parent)) {
140
140
  continue;
141
141
  }
142
142
  children[write++] = child;
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var parse_html_exports = {};
20
+ __export(parse_html_exports, {
21
+ LARGE_DOCUMENT_CHARS: () => LARGE_DOCUMENT_CHARS,
22
+ MAX_ELEMENT_DEPTH: () => MAX_ELEMENT_DEPTH,
23
+ parseHtml: () => parseHtml,
24
+ stripScriptBodies: () => stripScriptBodies
25
+ });
26
+ module.exports = __toCommonJS(parse_html_exports);
27
+ var import_hast_util_from_parse5 = require("hast-util-from-parse5");
28
+ var import_parse5 = require("parse5");
29
+ const MAX_ELEMENT_DEPTH = 256;
30
+ const isParent = (node) => "childNodes" in node;
31
+ const LINE_ELEMENTS = /* @__PURE__ */ new Set([
32
+ "p",
33
+ "div",
34
+ "li",
35
+ "tr",
36
+ "td",
37
+ "th",
38
+ "h1",
39
+ "h2",
40
+ "h3",
41
+ "h4",
42
+ "h5",
43
+ "h6",
44
+ "br",
45
+ "section",
46
+ "article",
47
+ "pre",
48
+ "blockquote"
49
+ ]);
50
+ const textNode = (value, parentNode) => ({ nodeName: "#text", value, parentNode });
51
+ const coalesceText = (parent) => {
52
+ const merged = [];
53
+ for (const child of parent.childNodes) {
54
+ const previous = merged.at(-1);
55
+ if (child.nodeName === "#text" && previous?.nodeName === "#text") {
56
+ previous.value += child.value;
57
+ } else {
58
+ merged.push(child);
59
+ }
60
+ }
61
+ parent.childNodes = merged;
62
+ };
63
+ const flattenDeep = (root, maxDepth) => {
64
+ const stack = [[root, 0]];
65
+ while (stack.length > 0) {
66
+ const [parent, depth] = stack.pop();
67
+ const children = parent.childNodes;
68
+ let unwrapped = false;
69
+ for (let index = 0; index < children.length; index++) {
70
+ const child = children[index];
71
+ if (!isParent(child)) {
72
+ continue;
73
+ }
74
+ if (depth + 1 > maxDepth && "tagName" in child) {
75
+ const replacement = [...child.childNodes];
76
+ if (LINE_ELEMENTS.has(child.tagName)) {
77
+ replacement.unshift(textNode("\n", parent));
78
+ replacement.push(textNode("\n", parent));
79
+ }
80
+ for (const node of replacement) {
81
+ node.parentNode = parent;
82
+ }
83
+ children.splice(index, 1, ...replacement);
84
+ index -= 1;
85
+ unwrapped = true;
86
+ continue;
87
+ }
88
+ stack.push([child, depth + 1]);
89
+ }
90
+ if (unwrapped) {
91
+ coalesceText(parent);
92
+ }
93
+ }
94
+ };
95
+ const SCRIPT_OR_STYLE = /<(script|style)\b([^>]*)>[\s\S]*?<\/\1\s*>/gi;
96
+ const LARGE_DOCUMENT_CHARS = 2e6;
97
+ const stripScriptBodies = (html) => html.replace(
98
+ SCRIPT_OR_STYLE,
99
+ (match, tag, attributes) => /application\/ld\+json/i.test(attributes) ? match : `<${tag}${attributes}></${tag}>`
100
+ );
101
+ const parseHtml = (html, options = {}) => {
102
+ const source = html.length > LARGE_DOCUMENT_CHARS ? stripScriptBodies(html) : html;
103
+ const document = options.fragment ? (0, import_parse5.parseFragment)(source, { scriptingEnabled: false }) : (0, import_parse5.parse)(source, { scriptingEnabled: false });
104
+ flattenDeep(document, MAX_ELEMENT_DEPTH);
105
+ return (0, import_hast_util_from_parse5.fromParse5)(document);
106
+ };
107
+ // Annotate the CommonJS export names for ESM import in node:
108
+ 0 && (module.exports = {
109
+ LARGE_DOCUMENT_CHARS,
110
+ MAX_ELEMENT_DEPTH,
111
+ parseHtml,
112
+ stripScriptBodies
113
+ });
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var pre_text_exports = {};
20
+ __export(pre_text_exports, {
21
+ preText: () => preText
22
+ });
23
+ module.exports = __toCommonJS(pre_text_exports);
24
+ const LINE_BLOCK_TAGS = /* @__PURE__ */ new Set([
25
+ "address",
26
+ "article",
27
+ "aside",
28
+ "blockquote",
29
+ "dd",
30
+ "div",
31
+ "dl",
32
+ "dt",
33
+ "figcaption",
34
+ "figure",
35
+ "footer",
36
+ "form",
37
+ "h1",
38
+ "h2",
39
+ "h3",
40
+ "h4",
41
+ "h5",
42
+ "h6",
43
+ "header",
44
+ "hr",
45
+ "li",
46
+ "main",
47
+ "nav",
48
+ "ol",
49
+ "p",
50
+ "pre",
51
+ "section",
52
+ "table",
53
+ "tr",
54
+ "ul"
55
+ ]);
56
+ const INLINE_CONTEXT_TAGS = /* @__PURE__ */ new Set(["span", "a", "b", "i", "em", "strong", "mark", "small", "u", "s", "label"]);
57
+ const preText = (node) => {
58
+ let out = "";
59
+ const ensureLineStart = () => {
60
+ if (out.length > 0 && out.charCodeAt(out.length - 1) !== 10) {
61
+ out += "\n";
62
+ }
63
+ };
64
+ const visitChildren = (parent, inline) => {
65
+ let cells = 0;
66
+ for (const child of parent.children) {
67
+ const isCell = child.type === "element" && (child.tagName === "td" || child.tagName === "th");
68
+ visit(child, isCell ? cells++ : 0, inline);
69
+ }
70
+ };
71
+ const visitElement = (element, cellIndex, inline) => {
72
+ const tag = element.tagName;
73
+ if (tag === "br") {
74
+ out += "\n";
75
+ return;
76
+ }
77
+ if (cellIndex > 0) {
78
+ out += " ";
79
+ }
80
+ const block = !inline && LINE_BLOCK_TAGS.has(tag);
81
+ if (block) {
82
+ ensureLineStart();
83
+ }
84
+ visitChildren(element, inline || INLINE_CONTEXT_TAGS.has(tag));
85
+ if (block) {
86
+ ensureLineStart();
87
+ }
88
+ };
89
+ const visit = (current, cellIndex, inline) => {
90
+ if (current.type === "text") {
91
+ out += current.value;
92
+ } else if (current.type === "element") {
93
+ visitElement(current, cellIndex, inline);
94
+ } else if (current.type === "root") {
95
+ visitChildren(current, inline);
96
+ }
97
+ };
98
+ visit(node, 0, false);
99
+ return out;
100
+ };
101
+ // Annotate the CommonJS export names for ESM import in node:
102
+ 0 && (module.exports = {
103
+ preText
104
+ });
@@ -10,6 +10,61 @@ var emptyDeclarationFilter = (node) => {
10
10
  }
11
11
  return true;
12
12
  };
13
+ var trimEdge = (children, edge) => {
14
+ const index = edge === "start" ? 0 : children.length - 1;
15
+ const node = children[index];
16
+ if (node?.type !== "text") {
17
+ return children;
18
+ }
19
+ const value = edge === "start" ? node.value.trimStart() : node.value.trimEnd();
20
+ const copy = [...children];
21
+ if (value.length === 0) {
22
+ copy.splice(index, 1);
23
+ } else {
24
+ copy[index] = { ...node, value };
25
+ }
26
+ return copy;
27
+ };
28
+ var liftDisplayMath = (paragraph) => {
29
+ if (!paragraph.children.some((child) => child.type === "math")) {
30
+ return void 0;
31
+ }
32
+ const out = [];
33
+ let run = [];
34
+ const flush = () => {
35
+ const trimmed = trimEdge(trimEdge(run, "start"), "end");
36
+ if (trimmed.length > 0) {
37
+ out.push({ ...paragraph, children: trimmed });
38
+ }
39
+ run = [];
40
+ };
41
+ for (const child of paragraph.children) {
42
+ if (child.type === "math") {
43
+ flush();
44
+ out.push(child);
45
+ } else {
46
+ run.push(child);
47
+ }
48
+ }
49
+ flush();
50
+ return out;
51
+ };
52
+ var liftDisplayMathIn = (node) => {
53
+ if (!("children" in node)) {
54
+ return;
55
+ }
56
+ const parent = node;
57
+ for (let index = 0; index < parent.children.length; index++) {
58
+ const child = parent.children[index];
59
+ const lifted = child.type === "paragraph" ? liftDisplayMath(child) : void 0;
60
+ if (lifted) {
61
+ parent.children.splice(index, 1, ...lifted);
62
+ index += lifted.length - 1;
63
+ continue;
64
+ }
65
+ liftDisplayMathIn(child);
66
+ }
67
+ };
13
68
  var extractMdast = (node) => {
14
69
  const extracted = filter(node, (node2) => {
15
70
  if (!emptyDeclarationFilter(node2)) {
@@ -17,6 +72,9 @@ var extractMdast = (node) => {
17
72
  }
18
73
  return true;
19
74
  });
75
+ if (extracted) {
76
+ liftDisplayMathIn(extracted);
77
+ }
20
78
  return extracted;
21
79
  };
22
80
  export {
@@ -3,11 +3,14 @@ import {
3
3
  pipeExtractors
4
4
  } from "./pipeExtractors.js";
5
5
  import { takumiExtractor } from "./presets/takumi.js";
6
+ import { createKiwameExtractor, kiwameExtractor } from "./presets/kiwame.js";
6
7
  import { minimalFilter } from "./presets/minimal-filter.js";
7
8
  import { autoExtractor, createAutoExtractor } from "./presets/auto.js";
8
9
  export {
9
10
  autoExtractor,
10
11
  createAutoExtractor,
12
+ createKiwameExtractor,
13
+ kiwameExtractor,
11
14
  minimalFilter,
12
15
  pipeExtractors,
13
16
  takumiExtractor