@mdream/js 1.0.5 → 1.0.7

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,11 +139,10 @@ 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
@@ -152,22 +151,20 @@ const tagHandlers = {
152
151
  enter: ({ node, state }) => {
153
152
  if ((node.depthMap[34] || 0) > 0) {
154
153
  const language = getLanguageFromClass(node.attributes?.class);
155
- const liDepth = node.depthMap[25] || 0;
156
- if (liDepth > 0) return `\n\n${" ".repeat(liDepth)}\`\`\`${language}\n`;
154
+ if ((node.depthMap[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`;
157
155
  return `\`\`\`${language}\n`;
158
156
  }
159
157
  if ((node.depthMap[25] || 0) > 0) {
160
158
  const lastEntry = state.buffer.at(-1);
161
159
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
162
- if (lastChar && lastChar !== " " && lastChar !== "\n") return ` \``;
160
+ if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return ` \``;
163
161
  }
164
162
  return "`";
165
163
  },
166
- exit: ({ node }) => {
164
+ exit: ({ node, state }) => {
167
165
  if ((node.depthMap[34] || 0) > 0) {
168
- const liDepth = node.depthMap[25] || 0;
169
- if (liDepth > 0) {
170
- const indent = " ".repeat(liDepth);
166
+ if ((node.depthMap[25] || 0) > 0) {
167
+ const indent = state.listIndent;
171
168
  return `\n${indent}\`\`\`\n\n${indent}`;
172
169
  }
173
170
  return `\n\`\`\``;
@@ -187,11 +184,10 @@ const tagHandlers = {
187
184
  exit: ({ node }) => isInsideTableCell(node) ? "</ol>" : void 0
188
185
  },
189
186
  [25]: {
190
- enter: ({ node }) => {
187
+ enter: ({ node, state }) => {
191
188
  if (isInsideTableCell(node)) return "<li>";
192
- const depth = (node.depthMap[24] || 0) + (node.depthMap[33] || 0) - 1;
193
- const isOrdered = node.parent?.tagId === 33;
194
- 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}`;
195
191
  },
196
192
  exit: ({ node }) => isInsideTableCell(node) ? "</li>" : void 0,
197
193
  spacing: LIST_ITEM_SPACING
@@ -299,10 +295,10 @@ const tagHandlers = {
299
295
  return `\n${prefix.trimEnd()}\n${prefix}`;
300
296
  }
301
297
  }
302
- if ((node.depthMap[25] || 0) > 0) {
298
+ if ((node.depthMap[25] || 0) > 0 && !isInsideTableCell(node)) {
303
299
  const lastEntry = state.buffer.at(-1);
304
300
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
305
- if (lastChar && lastChar !== " " && lastChar !== "\n") return " ";
301
+ if (lastChar && lastChar !== " " && lastChar !== "\n") return `\n\n${state.listIndent}`;
306
302
  }
307
303
  } },
308
304
  [36]: {},
@@ -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) {
@@ -120,9 +134,9 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
120
134
  if (textNode.value === " " && lastChar === "\n") return;
121
135
  if (shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
122
136
  if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
123
- const indent = " ".repeat(state.depthMap[25]);
124
- let value = textNode.value.replace(/\n(?=[^ \t\n])/g, `\n${indent}`);
125
- if (lastChar === "\n" && value[0] && value[0] !== " " && value[0] !== " " && value[0] !== "\n") value = indent + value;
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;
126
140
  textNode.value = value;
127
141
  }
128
142
  state.buffer.push(textNode.value);
@@ -160,6 +174,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
160
174
  state.buffer.push(fragment);
161
175
  state.lastContentCache = fragment;
162
176
  }
177
+ updateListIndent(state, element, eventType);
163
178
  return;
164
179
  }
165
180
  const newlinesStr = "\n".repeat(newLines);
@@ -189,6 +204,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
189
204
  state.buffer.push(fragment);
190
205
  state.lastContentCache = fragment;
191
206
  }
207
+ updateListIndent(state, element, eventType);
192
208
  }
193
209
  function processHtml(html) {
194
210
  parseHtmlStream(html, {
@@ -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.5",
4
+ "version": "1.0.7",
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",