@mdream/js
Advanced tools
+10
-14
@@ -142,7 +142,6 @@ import { a as NO_SPACING, c as TABLE_ROW_SPACING, g as TagIdMap, i as LIST_ITEM_SPACING, t as BLOCKQUOTE_SPACING } from "./const.mjs"; | ||
| [22]: { | ||
| enter: ({ node }) => { | ||
| enter: ({ node, state }) => { | ||
| const depth = node.depthMap[22] || 1; | ||
| let prefix = "> ".repeat(depth); | ||
| const liDepth = node.depthMap[25] || 0; | ||
| if (liDepth > 0) prefix = `\n${" ".repeat(liDepth)}${prefix}`; | ||
| if ((node.depthMap[25] || 0) > 0) prefix = `\n${state.listIndent}${prefix}`; | ||
| return prefix; | ||
@@ -156,4 +155,3 @@ }, | ||
| const language = getLanguageFromClass(node.attributes?.class); | ||
| const liDepth = node.depthMap[25] || 0; | ||
| if (liDepth > 0) return `\n\n${" ".repeat(liDepth)}\`\`\`${language}\n`; | ||
| if ((node.depthMap[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`; | ||
| return `\`\`\`${language}\n`; | ||
@@ -164,11 +162,10 @@ } | ||
| const lastChar = lastEntry?.charAt(lastEntry.length - 1) || ""; | ||
| if (lastChar && lastChar !== " " && lastChar !== "\n") return ` \``; | ||
| if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return ` \``; | ||
| } | ||
| return "`"; | ||
| }, | ||
| exit: ({ node }) => { | ||
| exit: ({ node, state }) => { | ||
| if ((node.depthMap[34] || 0) > 0) { | ||
| const liDepth = node.depthMap[25] || 0; | ||
| if (liDepth > 0) { | ||
| const indent = " ".repeat(liDepth); | ||
| if ((node.depthMap[25] || 0) > 0) { | ||
| const indent = state.listIndent; | ||
| return `\n${indent}\`\`\`\n\n${indent}`; | ||
@@ -193,7 +190,6 @@ } | ||
| [25]: { | ||
| enter: ({ node }) => { | ||
| enter: ({ node, state }) => { | ||
| if (isInsideTableCell(node)) return "<li>"; | ||
| const depth = (node.depthMap[24] || 0) + (node.depthMap[33] || 0) - 1; | ||
| const isOrdered = node.parent?.tagId === 33; | ||
| return `${" ".repeat(Math.max(0, depth))}${isOrdered ? `${node.index + 1}. ` : "- "}`; | ||
| const marker = node.parent?.tagId === 33 ? `${node.index + 1}. ` : "- "; | ||
| return `${state.listIndent}${marker}`; | ||
| }, | ||
@@ -200,0 +196,0 @@ exit: ({ node }) => isInsideTableCell(node) ? "</li>" : void 0, |
@@ -39,2 +39,14 @@ import { a as NO_SPACING, n as DEFAULT_BLOCK_SPACING } from "./const.mjs"; | ||
| } | ||
| function updateListIndent(state, element, eventType) { | ||
| if (element.tagId !== 25) return; | ||
| if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) return; | ||
| if (eventType === 0) { | ||
| const width = element.parent?.tagId === 33 ? String(element.index + 1).length + 2 : 2; | ||
| state.listIndentWidths.push(width); | ||
| state.listIndent += " ".repeat(width); | ||
| } else if (eventType === 1) { | ||
| const width = state.listIndentWidths.pop() ?? 0; | ||
| state.listIndent = state.listIndent.slice(0, state.listIndent.length - width); | ||
| } | ||
| } | ||
| function needsSpacing(lastChar, firstChar, state) { | ||
@@ -100,3 +112,5 @@ if (lastChar === " " || lastChar === "\n" || lastChar === " ") return false; | ||
| buffer: [], | ||
| depthMap: new Uint8Array(108) | ||
| depthMap: new Uint8Array(108), | ||
| listIndent: "", | ||
| listIndentWidths: [] | ||
| }; | ||
@@ -125,5 +139,5 @@ let lastYieldedLength = 0; | ||
| if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) { | ||
| const indent = " ".repeat(state.depthMap[25]); | ||
| let value = textNode.value.replace(/\n(?=[^ \t\n])/g, `\n${indent}`); | ||
| if (lastChar === "\n" && value[0] && value[0] !== " " && value[0] !== " " && value[0] !== "\n") value = indent + value; | ||
| const indent = state.listIndent; | ||
| let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`); | ||
| if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value; | ||
| textNode.value = value; | ||
@@ -166,2 +180,3 @@ } | ||
| } | ||
| updateListIndent(state, element, eventType); | ||
| return; | ||
@@ -196,2 +211,3 @@ } | ||
| } | ||
| updateListIndent(state, element, eventType); | ||
| } | ||
@@ -198,0 +214,0 @@ function processHtml(html) { |
@@ -247,2 +247,9 @@ declare const TAG_H1 = 7; | ||
| context?: PluginContext; | ||
| /** | ||
| * Cumulative indent for list-item continuation. Grows by each ancestor | ||
| * `<li>`'s marker width (`"- "` = 2, `"N. "` = digits(N) + 2) so continuation | ||
| * content lands in the CommonMark content column. Managed by the processor. | ||
| */ | ||
| listIndent?: string; | ||
| listIndentWidths?: number[]; | ||
| } | ||
@@ -249,0 +256,0 @@ type NodeEventEnter = 0; |
+1
-1
| { | ||
| "name": "@mdream/js", | ||
| "type": "module", | ||
| "version": "1.0.5", | ||
| "version": "1.0.6", | ||
| "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.", | ||
@@ -6,0 +6,0 @@ "author": { |
156824
0.55%3468
0.35%