@mdream/js 1.4.0 → 1.4.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.
@@ -982,6 +982,205 @@ const BACKTICK_CHAR = 96;
982
982
  const PIPE_CHAR = 124;
983
983
  const OPEN_BRACKET_CHAR = 91;
984
984
  const CLOSE_BRACKET_CHAR = 93;
985
+ const HEAD_CONTENT_TAGS = new Set([
986
+ 4,
987
+ 5,
988
+ 54,
989
+ 56,
990
+ 53,
991
+ 52,
992
+ 84,
993
+ 78
994
+ ]);
995
+ const CLOSES_P = (() => {
996
+ const t = new Uint8Array(109);
997
+ for (const id of [
998
+ 36,
999
+ 35,
1000
+ 24,
1001
+ 33,
1002
+ 101,
1003
+ 25,
1004
+ 98,
1005
+ 99,
1006
+ 28,
1007
+ 7,
1008
+ 8,
1009
+ 9,
1010
+ 10,
1011
+ 11,
1012
+ 12,
1013
+ 22,
1014
+ 51,
1015
+ 50,
1016
+ 105,
1017
+ 47,
1018
+ 41,
1019
+ 88,
1020
+ 34,
1021
+ 13,
1022
+ 40,
1023
+ 68,
1024
+ 102,
1025
+ 106,
1026
+ 100,
1027
+ 104,
1028
+ 45,
1029
+ 2,
1030
+ 3,
1031
+ 75
1032
+ ]) t[id] = 1;
1033
+ return t;
1034
+ })();
1035
+ const NEEDS_IMPLIED_END_RECOVERY = (() => {
1036
+ const t = CLOSES_P.slice();
1037
+ for (const id of [
1038
+ 26,
1039
+ 32,
1040
+ 31,
1041
+ 30,
1042
+ 29,
1043
+ 38,
1044
+ 39
1045
+ ]) t[id] = 1;
1046
+ return t;
1047
+ })();
1048
+ const P_SCOPE_BOUNDARY = new Set([
1049
+ 43,
1050
+ 32,
1051
+ 31,
1052
+ 107,
1053
+ 28,
1054
+ 78,
1055
+ 0,
1056
+ 24,
1057
+ 33,
1058
+ 101,
1059
+ 25
1060
+ ]);
1061
+ const LI_SCOPE_BOUNDARY = new Set([
1062
+ 24,
1063
+ 33,
1064
+ 28,
1065
+ 32,
1066
+ 31,
1067
+ 107,
1068
+ 78,
1069
+ 0
1070
+ ]);
1071
+ const DL_SCOPE_BOUNDARY = new Set([
1072
+ 101,
1073
+ 24,
1074
+ 33,
1075
+ 25,
1076
+ 28,
1077
+ 32,
1078
+ 31,
1079
+ 107,
1080
+ 78,
1081
+ 0
1082
+ ]);
1083
+ const CELL_SCOPE_BOUNDARY = new Set([
1084
+ 30,
1085
+ 29,
1086
+ 38,
1087
+ 39,
1088
+ 28,
1089
+ 107,
1090
+ 78,
1091
+ 0
1092
+ ]);
1093
+ const A_SCOPE_BOUNDARY = new Set([
1094
+ 35,
1095
+ 36,
1096
+ 25,
1097
+ 24,
1098
+ 33,
1099
+ 101,
1100
+ 98,
1101
+ 99,
1102
+ 28,
1103
+ 32,
1104
+ 31,
1105
+ 30,
1106
+ 107,
1107
+ 22,
1108
+ 51,
1109
+ 50,
1110
+ 105,
1111
+ 47,
1112
+ 41,
1113
+ 88,
1114
+ 104,
1115
+ 40,
1116
+ 68,
1117
+ 102,
1118
+ 43,
1119
+ 7,
1120
+ 8,
1121
+ 9,
1122
+ 10,
1123
+ 11,
1124
+ 12,
1125
+ 78,
1126
+ 0
1127
+ ]);
1128
+ const HEADINGS = new Set([
1129
+ 7,
1130
+ 8,
1131
+ 9,
1132
+ 10,
1133
+ 11,
1134
+ 12
1135
+ ]);
1136
+ const SINGLE_P = new Set([35]);
1137
+ const SINGLE_LI = new Set([25]);
1138
+ const SINGLE_A = new Set([26]);
1139
+ const DT_DD = new Set([99, 98]);
1140
+ const TD_TH = new Set([32, 31]);
1141
+ const TR_CELLS = new Set([
1142
+ 32,
1143
+ 31,
1144
+ 30
1145
+ ]);
1146
+ const SECTION_CELLS = new Set([
1147
+ 32,
1148
+ 31,
1149
+ 30,
1150
+ 29,
1151
+ 38,
1152
+ 39,
1153
+ 107
1154
+ ]);
1155
+ function closeImpliedTo(state, target, boundary, handleEvent) {
1156
+ let found = false;
1157
+ for (let node = state.currentNode; node; node = node.parent) {
1158
+ const id = node.tagId;
1159
+ if (id !== void 0 && target.has(id)) {
1160
+ found = true;
1161
+ break;
1162
+ }
1163
+ if (id !== void 0 && boundary.has(id)) break;
1164
+ }
1165
+ if (!found) return;
1166
+ while (state.currentNode) {
1167
+ const id = state.currentNode.tagId;
1168
+ const isTarget = id !== void 0 && target.has(id);
1169
+ closeNode(state.currentNode, state, handleEvent);
1170
+ if (isTarget) break;
1171
+ }
1172
+ }
1173
+ function closeTableContext(state, closeable, handleEvent) {
1174
+ while (state.currentNode) {
1175
+ const id = state.currentNode.tagId;
1176
+ if (id === void 0 || !closeable.has(id)) break;
1177
+ closeNode(state.currentNode, state, handleEvent);
1178
+ }
1179
+ }
1180
+ function finalizeParse(leftover, state, handleEvent) {
1181
+ if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1182
+ while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
1183
+ }
985
1184
  const EMPTY_ATTRIBUTES = Object.freeze({});
986
1185
  function copyDepthMap(depthMap) {
987
1186
  return new Uint8Array(depthMap);
@@ -1379,6 +1578,30 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1379
1578
  remainingText: `<${tagName}${result.attrBuffer}`,
1380
1579
  selfClosing: false
1381
1580
  };
1581
+ if ((state.depthMap[1] || 0) > 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1582
+ while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1583
+ const headNode = state.currentNode;
1584
+ if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1585
+ }
1586
+ if (tagId >= 0 && tagId < 109 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 26) {
1587
+ if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1588
+ } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
1589
+ if ((state.depthMap[28] || 0) > 0) if (tagId === 32 || tagId === 31) {
1590
+ if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
1591
+ } else if (tagId === 30) {
1592
+ if ((state.depthMap[30] || 0) > 0) closeTableContext(state, TR_CELLS, handleEvent);
1593
+ } else closeTableContext(state, SECTION_CELLS, handleEvent);
1594
+ } else {
1595
+ if ((state.depthMap[35] || 0) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
1596
+ if (HEADINGS.has(tagId)) {
1597
+ const top = state.currentNode;
1598
+ if (top && top.tagId !== void 0 && HEADINGS.has(top.tagId)) closeNode(top, state, handleEvent);
1599
+ } else if (tagId === 25) {
1600
+ if ((state.depthMap[25] || 0) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
1601
+ } else if (tagId === 99 || tagId === 98) {
1602
+ if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1603
+ }
1604
+ }
1382
1605
  const currentTagCount = result.attributes && result.attributes.id ? void 0 : state.depthMap[tagId] || 0;
1383
1606
  state.depthMap[tagId] = (currentTagCount || 0) + 1;
1384
1607
  state.depth++;
@@ -1554,4 +1777,4 @@ function parseAttributes(attrStr) {
1554
1777
  }
1555
1778
  return result;
1556
1779
  }
1557
- export { buildTagOverrideHandlers, parseAttributes, parseHtml, parseHtmlStream };
1780
+ export { buildTagOverrideHandlers, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
2
- import { parseHtmlStream } from "./parse.mjs";
2
+ import { finalizeParse, parseHtmlStream } from "./parse.mjs";
3
3
  import { extractionCollectorPlugin, filterPlugin, frontmatterPlugin, isolateMainPlugin, tailwindPlugin } from "./plugins.mjs";
4
4
  function processPluginsForEvent(event, plugins, state, processEvent) {
5
5
  if (plugins?.length) {
@@ -305,14 +305,16 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
305
305
  updateListIndent(state, element, eventType);
306
306
  }
307
307
  function processHtml(html) {
308
- parseHtmlStream(html, {
308
+ const parseState = {
309
309
  depthMap: state.depthMap,
310
310
  depth: 0,
311
311
  resolvedPlugins,
312
312
  tagOverrideHandlers
313
- }, (event) => {
313
+ };
314
+ const handleEvent = (event) => {
314
315
  processPluginsForEvent(event, resolvedPlugins, state, processEvent);
315
- });
316
+ };
317
+ finalizeParse(parseHtmlStream(html, parseState, handleEvent), parseState, handleEvent);
316
318
  }
317
319
  function getMarkdown() {
318
320
  const result = state.buffer.join("").trimStart();
@@ -1,5 +1,5 @@
1
1
  import "./const.mjs";
2
- import { buildTagOverrideHandlers, parseHtmlStream } from "./parse.mjs";
2
+ import { buildTagOverrideHandlers, finalizeParse, parseHtmlStream } from "./parse.mjs";
3
3
  import { createMarkdownProcessor, processPluginsForEvent, resolvePlugins } from "./resolve-plugins.mjs";
4
4
  import "./plugins.mjs";
5
5
  import "../preset/minimal.mjs";
@@ -310,9 +310,10 @@ async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins
310
310
  const chunk = processor.getMarkdownChunk();
311
311
  if (chunk) yield chunk;
312
312
  }
313
- if (remainingHtml) parseHtmlStream(remainingHtml, parseState, (event) => {
313
+ const handleEvent = (event) => {
314
314
  processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent);
315
- });
315
+ };
316
+ finalizeParse(remainingHtml ? parseHtmlStream(remainingHtml, parseState, handleEvent) : "", parseState, handleEvent);
316
317
  const finalChunk = processor.getMarkdownChunk();
317
318
  if (finalChunk) yield finalChunk;
318
319
  } finally {
package/dist/parse.d.mts CHANGED
@@ -1,4 +1,18 @@
1
1
  import { ElementNode, Node, NodeEvent, TagHandler, TransformPlugin } from "./_chunks/types.mjs";
2
+ /**
3
+ * Commit end-of-input state: flush trailing buffered text and close any open
4
+ * elements. The streaming parser keeps trailing text and unclosed elements
5
+ * pending (a later chunk might continue them); at true EOF they must be
6
+ * committed so trailing content is not dropped (e.g. `<p>a<p>b`).
7
+ *
8
+ * `leftover` is the residual returned by the final `parseHtmlStream`. Pure
9
+ * trailing text (no leading `<`) is emitted; a residual that is an incomplete
10
+ * start tag (leading `<`) is dropped, matching the browser tokenizer's
11
+ * EOF-in-tag behaviour. The text-buffer flags set while the trailing text was
12
+ * scanned persist on `state`, so `processTextBuffer` commits it as if the next
13
+ * tag had triggered the flush.
14
+ */
15
+ declare function finalizeParse(leftover: string, state: ParseState, handleEvent: (event: NodeEvent) => void): void;
2
16
  interface ParseOptions {
3
17
  resolvedPlugins?: TransformPlugin[];
4
18
  }
@@ -58,4 +72,4 @@ declare function parseHtmlStream(html: string, state: ParseState, onEvent: (even
58
72
  * Parse HTML attributes string into key-value object
59
73
  */
60
74
  declare function parseAttributes(attrStr: string): Record<string, string>;
61
- export { ParseOptions, ParseResult, ParseState, parseAttributes, parseHtml, parseHtmlStream };
75
+ export { ParseOptions, ParseResult, ParseState, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
package/dist/parse.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { parseAttributes, parseHtml, parseHtmlStream } from "./_chunks/parse.mjs";
2
- export { parseAttributes, parseHtml, parseHtmlStream };
1
+ import { finalizeParse, parseAttributes, parseHtml, parseHtmlStream } from "./_chunks/parse.mjs";
2
+ export { finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.4.0",
4
+ "version": "1.4.1",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",