@mdream/js 1.0.4 → 1.0.6

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.
@@ -139,22 +139,37 @@ const tagHandlers = {
139
139
  isInline: true
140
140
  },
141
141
  [22]: {
142
- enter: ({ node }) => {
142
+ enter: ({ node, state }) => {
143
143
  const depth = node.depthMap[22] || 1;
144
144
  let prefix = "> ".repeat(depth);
145
- const liDepth = node.depthMap[25] || 0;
146
- if (liDepth > 0) prefix = `\n${" ".repeat(liDepth)}${prefix}`;
145
+ if ((node.depthMap[25] || 0) > 0) prefix = `\n${state.listIndent}${prefix}`;
147
146
  return prefix;
148
147
  },
149
148
  spacing: BLOCKQUOTE_SPACING
150
149
  },
151
150
  [23]: {
152
- enter: ({ node }) => {
153
- if ((node.depthMap[34] || 0) > 0) return `\`\`\`${getLanguageFromClass(node.attributes?.class)}\n`;
151
+ enter: ({ node, state }) => {
152
+ if ((node.depthMap[34] || 0) > 0) {
153
+ const language = getLanguageFromClass(node.attributes?.class);
154
+ if ((node.depthMap[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`;
155
+ return `\`\`\`${language}\n`;
156
+ }
157
+ if ((node.depthMap[25] || 0) > 0) {
158
+ const lastEntry = state.buffer.at(-1);
159
+ const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
160
+ if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return ` \``;
161
+ }
154
162
  return "`";
155
163
  },
156
- exit: ({ node }) => {
157
- return (node.depthMap[34] || 0) > 0 ? `\n\`\`\`` : "`";
164
+ exit: ({ node, state }) => {
165
+ if ((node.depthMap[34] || 0) > 0) {
166
+ if ((node.depthMap[25] || 0) > 0) {
167
+ const indent = state.listIndent;
168
+ return `\n${indent}\`\`\`\n\n${indent}`;
169
+ }
170
+ return `\n\`\`\``;
171
+ }
172
+ return "`";
158
173
  },
159
174
  collapsesInnerWhiteSpace: true,
160
175
  spacing: NO_SPACING,
@@ -169,11 +184,10 @@ const tagHandlers = {
169
184
  exit: ({ node }) => isInsideTableCell(node) ? "</ol>" : void 0
170
185
  },
171
186
  [25]: {
172
- enter: ({ node }) => {
187
+ enter: ({ node, state }) => {
173
188
  if (isInsideTableCell(node)) return "<li>";
174
- const depth = (node.depthMap[24] || 0) + (node.depthMap[33] || 0) - 1;
175
- const isOrdered = node.parent?.tagId === 33;
176
- return `${" ".repeat(Math.max(0, depth))}${isOrdered ? `${node.index + 1}. ` : "- "}`;
189
+ const marker = node.parent?.tagId === 33 ? `${node.index + 1}. ` : "- ";
190
+ return `${state.listIndent}${marker}`;
177
191
  },
178
192
  exit: ({ node }) => isInsideTableCell(node) ? "</li>" : void 0,
179
193
  spacing: LIST_ITEM_SPACING
@@ -281,6 +295,11 @@ const tagHandlers = {
281
295
  return `\n${prefix.trimEnd()}\n${prefix}`;
282
296
  }
283
297
  }
298
+ if ((node.depthMap[25] || 0) > 0) {
299
+ const lastEntry = state.buffer.at(-1);
300
+ const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
301
+ if (lastChar && lastChar !== " " && lastChar !== "\n") return " ";
302
+ }
284
303
  } },
285
304
  [36]: {},
286
305
  [37]: {
@@ -36,6 +36,18 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
36
36
  processEvent(event);
37
37
  return false;
38
38
  }
39
+ function updateListIndent(state, element, eventType) {
40
+ if (element.tagId !== 25) return;
41
+ if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) return;
42
+ if (eventType === 0) {
43
+ const width = element.parent?.tagId === 33 ? String(element.index + 1).length + 2 : 2;
44
+ state.listIndentWidths.push(width);
45
+ state.listIndent += " ".repeat(width);
46
+ } else if (eventType === 1) {
47
+ const width = state.listIndentWidths.pop() ?? 0;
48
+ state.listIndent = state.listIndent.slice(0, state.listIndent.length - width);
49
+ }
50
+ }
39
51
  function needsSpacing(lastChar, firstChar, state) {
40
52
  if (lastChar === " " || lastChar === "\n" || lastChar === " ") return false;
41
53
  if (firstChar === " " || firstChar === "\n" || firstChar === " ") return false;
@@ -96,7 +108,9 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
96
108
  const state = {
97
109
  options,
98
110
  buffer: [],
99
- depthMap: new Uint8Array(108)
111
+ depthMap: new Uint8Array(108),
112
+ listIndent: "",
113
+ listIndentWidths: []
100
114
  };
101
115
  let lastYieldedLength = 0;
102
116
  function processEvent(event) {
@@ -119,6 +133,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
119
133
  if (textNode.excludedFromMarkdown) return;
120
134
  if (textNode.value === " " && lastChar === "\n") return;
121
135
  if (shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
136
+ if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
137
+ const indent = state.listIndent;
138
+ let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
139
+ if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
140
+ textNode.value = value;
141
+ }
122
142
  state.buffer.push(textNode.value);
123
143
  state.lastContentCache = textNode.value;
124
144
  }
@@ -154,6 +174,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
154
174
  state.buffer.push(fragment);
155
175
  state.lastContentCache = fragment;
156
176
  }
177
+ updateListIndent(state, element, eventType);
157
178
  return;
158
179
  }
159
180
  const newlinesStr = "\n".repeat(newLines);
@@ -183,6 +204,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
183
204
  state.buffer.push(fragment);
184
205
  state.lastContentCache = fragment;
185
206
  }
207
+ updateListIndent(state, element, eventType);
186
208
  }
187
209
  function processHtml(html) {
188
210
  parseHtmlStream(html, {
@@ -275,7 +275,7 @@ async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins
275
275
  const reader = htmlStream.getReader();
276
276
  const processor = createMarkdownProcessor(options, resolvedPlugins, tagOverrideHandlers);
277
277
  const parseState = {
278
- depthMap: new Uint8Array(1024),
278
+ depthMap: processor.state.depthMap,
279
279
  depth: 0,
280
280
  resolvedPlugins,
281
281
  tagOverrideHandlers
@@ -244,6 +244,13 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
244
244
  /** Reference to the last processed node */
245
245
  lastNode?: Node;
246
246
  context?: PluginContext;
247
+ /**
248
+ * Cumulative indent for list-item continuation. Grows by each ancestor
249
+ * `<li>`'s marker width (`"- "` = 2, `"N. "` = digits(N) + 2) so continuation
250
+ * content lands in the CommonMark content column. Managed by the processor.
251
+ */
252
+ listIndent?: string;
253
+ listIndentWidths?: number[];
247
254
  }
248
255
  type NodeEventEnter = 0;
249
256
  type NodeEventExit = 1;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.0.4",
4
+ "version": "1.0.6",
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",