@mdream/js 1.4.0 → 1.5.2

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.
package/README.md CHANGED
@@ -21,7 +21,8 @@ yarn add @mdream/js@beta
21
21
 
22
22
  | Import | Description |
23
23
  |---|---|
24
- | `@mdream/js` | Core `htmlToMarkdown` and `streamHtmlToMarkdown` APIs |
24
+ | `@mdream/js` | Full `htmlToMarkdown` and `streamHtmlToMarkdown` APIs, including declarative plugins and cleanup |
25
+ | `@mdream/js/core` | Tree-shakable pre-v1-style conversion APIs with composable plugin arrays and tag overrides |
25
26
  | `@mdream/js/plugins` | Plugin utilities: `createPlugin`, `extractionPlugin`, `extractionCollectorPlugin`, `filterPlugin`, `frontmatterPlugin`, `isolateMainPlugin`, `tailwindPlugin` |
26
27
  | `@mdream/js/preset/minimal` | `withMinimalPreset` for declarative config combining frontmatter, isolateMain, tailwind, and filter plugins |
27
28
  | `@mdream/js/negotiate` | HTTP content negotiation: `shouldServeMarkdown`, `parseAcceptHeader` |
@@ -31,6 +32,10 @@ yarn add @mdream/js@beta
31
32
 
32
33
  ## API Reference
33
34
 
35
+ The package root remains the full conversion API. Import `@mdream/js/core` when
36
+ you want the smaller tree-shakable implementation and explicitly composed
37
+ plugins.
38
+
34
39
  ### `htmlToMarkdown(html, options?)`
35
40
 
36
41
  Converts an HTML string to Markdown synchronously.
@@ -40,6 +45,11 @@ import { htmlToMarkdown } from '@mdream/js'
40
45
 
41
46
  const md = htmlToMarkdown('<h1>Hello</h1><p>World</p>')
42
47
  // # Hello\n\nWorld
48
+
49
+ const text = htmlToMarkdown('<h1>Hello</h1><p><strong>World</strong></p>', {
50
+ format: 'text',
51
+ })
52
+ // Hello\n\nWorld
43
53
  ```
44
54
 
45
55
  **Parameters:**
@@ -76,6 +86,20 @@ for await (const chunk of stream) {
76
86
 
77
87
  **Returns:** `AsyncIterable<string>`
78
88
 
89
+ ### Tree-shakable core
90
+
91
+ The `/core` subpath restores the original composable JS interface without
92
+ loading declarative built-in plugins or cleanup:
93
+
94
+ ```typescript
95
+ import { htmlToMarkdown } from '@mdream/js/core'
96
+ import { filterPlugin } from '@mdream/js/plugins'
97
+
98
+ const markdown = htmlToMarkdown(html, {
99
+ plugins: [filterPlugin({ exclude: ['nav', 'footer'] })],
100
+ })
101
+ ```
102
+
79
103
  ---
80
104
 
81
105
  ## Options
@@ -88,6 +112,8 @@ for await (const chunk of stream) {
88
112
  | `plugins` | `BuiltinPlugins` | `undefined` | Declarative built-in plugin configuration (see [BuiltinPlugins](#builtinplugins)) |
89
113
  | `clean` | `boolean \| CleanOptions` | `undefined` | Post-processing cleanup. Pass `true` for all cleanup rules or an object for specific features (see [CleanOptions](#cleanoptions)). Sync API only for `fragments`. |
90
114
  | `hooks` | `TransformPlugin[]` | `undefined` | Imperative hook-based transform plugins for custom behavior (see [Plugins](#plugins)) |
115
+ | `wrapWidth` | `number` | `undefined` | Hard-wrap prose at this many characters on word boundaries |
116
+ | `format` | `'markdown' \| 'text'` | `'markdown'` | Output Markdown or plain text with Markdown/HTML markup omitted |
91
117
 
92
118
  ### `BuiltinPlugins`
93
119
 
@@ -252,9 +278,11 @@ import { tailwindPlugin } from '@mdream/js/plugins'
252
278
  const plugin = tailwindPlugin()
253
279
  ```
254
280
 
255
- ### `extractionPlugin(selectors)` (Deprecated)
281
+ ### `extractionPlugin(selectors)`
256
282
 
257
- > Deprecated. Use `plugins.extraction` config for declarative extraction that works with both JS and Rust engines.
283
+ Use this explicitly imported plugin with the tree-shakable core converter. Use
284
+ the `plugins.extraction` declarative config instead when sharing options with
285
+ the Rust engine.
258
286
 
259
287
  Extracts elements matching CSS selectors during conversion. Callbacks receive matching elements with their accumulated text content.
260
288
 
@@ -562,6 +590,9 @@ curl -s https://example.com | npx @mdream/js --origin https://example.com
562
590
 
563
591
  # With minimal preset
564
592
  curl -s https://example.com | npx @mdream/js --origin https://example.com --preset minimal
593
+
594
+ # Plain text output
595
+ curl -s https://example.com | npx @mdream/js --format text
565
596
  ```
566
597
 
567
598
  ### CLI Options
@@ -570,6 +601,9 @@ curl -s https://example.com | npx @mdream/js --origin https://example.com --pres
570
601
  |---|---|
571
602
  | `--origin <url>` | Origin URL for resolving relative image paths and links |
572
603
  | `--preset <preset>` | Conversion preset. Currently supports: `minimal` |
604
+ | `--wrap-width <n>` | Hard-wrap prose at `n` characters |
605
+ | `--format <format>` | Output format: `markdown`, `text` |
606
+ | `--text` | Alias for `--format text` |
573
607
  | `-v, --version` | Show version number |
574
608
  | `-h, --help` | Show help |
575
609
 
@@ -582,7 +616,7 @@ curl -s https://example.com | npx @mdream/js --origin https://example.com --pres
582
616
  Map custom HTML elements to standard Markdown behavior:
583
617
 
584
618
  ```typescript
585
- import { htmlToMarkdown } from '@mdream/js'
619
+ import { htmlToMarkdown } from '@mdream/js/core'
586
620
 
587
621
  const md = htmlToMarkdown('<x-heading>Title</x-heading>', {
588
622
  plugins: {
@@ -651,14 +685,14 @@ console.log('Title:', metadata.title)
651
685
  console.log('Description:', metadata.description)
652
686
  ```
653
687
 
654
- ### Custom Hooks for Content Filtering
688
+ ### Composable Plugin for Content Filtering
655
689
 
656
690
  ```typescript
657
- import { htmlToMarkdown } from '@mdream/js'
691
+ import { htmlToMarkdown } from '@mdream/js/core'
658
692
  import { createPlugin } from '@mdream/js/plugins'
659
693
 
660
694
  const md = htmlToMarkdown(html, {
661
- hooks: [
695
+ plugins: [
662
696
  createPlugin({
663
697
  beforeNodeProcess(event) {
664
698
  const { node } = event
@@ -720,6 +754,8 @@ import type {
720
754
  TextNode,
721
755
  TransformPlugin,
722
756
  } from '@mdream/js'
757
+
758
+ import type { CoreOptions, Plugin } from '@mdream/js/core'
723
759
  ```
724
760
 
725
761
  ## Exported Constants
@@ -117,9 +117,9 @@ const TagIdMap = {
117
117
  header: 105,
118
118
  figcaption: 106,
119
119
  caption: 107,
120
- datalist: 108
120
+ datalist: 108,
121
+ optgroup: 109
121
122
  };
122
- Object.entries(TagIdMap).map(([name, id]) => [id, name]);
123
123
  const NO_SPACING = [0, 0];
124
124
  const DEFAULT_BLOCK_SPACING = [2, 2];
125
125
  const BLOCKQUOTE_SPACING = [1, 1];
@@ -1,4 +1,5 @@
1
1
  import { MdreamOptions, TransformPlugin } from "./types.mjs";
2
+ import "./minimal.mjs";
2
3
  /**
3
4
  * Create a plugin with type-safe hook definitions.
4
5
  * All TransformPlugin fields are optional, so this is a typed identity function.
@@ -0,0 +1,404 @@
1
+ import { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
2
+ import { continuationPrefix, finalizeParse, parseHtmlStream } from "./parse.mjs";
3
+ function processPluginsForEvent(event, plugins, state, processEvent) {
4
+ if (plugins?.length) {
5
+ if (event.node.type === 1 && event.type === 0) {
6
+ const element = event.node;
7
+ for (const plugin of plugins) if (plugin.processAttributes) plugin.processAttributes(element, state);
8
+ }
9
+ let shouldSkip = false;
10
+ for (const plugin of plugins) {
11
+ const res = plugin.beforeNodeProcess?.(event, state);
12
+ if (typeof res === "object") shouldSkip = res.skip;
13
+ }
14
+ if (shouldSkip) return true;
15
+ if (event.node.type === 1) {
16
+ const element = event.node;
17
+ const fn = event.type === 0 ? "onNodeEnter" : "onNodeExit";
18
+ const pluginOutputs = [];
19
+ for (const plugin of plugins) if (plugin[fn]) {
20
+ const result = plugin[fn](element, state);
21
+ if (result) pluginOutputs.push(result);
22
+ }
23
+ if (pluginOutputs.length > 0) element.pluginOutput = [...element.pluginOutput || [], ...pluginOutputs];
24
+ } else if (event.node.type === 2 && event.type === 0) {
25
+ const textNode = event.node;
26
+ for (const plugin of plugins) if (plugin.processTextNode) {
27
+ const result = plugin.processTextNode(textNode, state);
28
+ if (result) {
29
+ if (result.skip) return true;
30
+ textNode.value = result.content;
31
+ }
32
+ }
33
+ }
34
+ }
35
+ processEvent(event);
36
+ return false;
37
+ }
38
+ function updateListIndent(state, element, eventType) {
39
+ if (element.tagId !== 25) return;
40
+ if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) return;
41
+ if (eventType === 0) {
42
+ const isOrdered = element.parent?.tagId === 33;
43
+ const width = state.plainText ? 0 : isOrdered ? 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
+ }
51
+ function needsSpacing(lastChar, firstChar, state) {
52
+ if (lastChar === " " || lastChar === "\n" || lastChar === " ") return false;
53
+ if (firstChar === " " || firstChar === "\n" || firstChar === " ") return false;
54
+ const noSpaceAfter = /* @__PURE__ */ new Set([
55
+ "[",
56
+ "(",
57
+ ">",
58
+ "*",
59
+ "_",
60
+ "`"
61
+ ]);
62
+ const noSpaceBefore = /* @__PURE__ */ new Set([
63
+ "]",
64
+ ")",
65
+ "<",
66
+ ".",
67
+ ",",
68
+ "!",
69
+ "?",
70
+ ":",
71
+ ";",
72
+ "*",
73
+ "_",
74
+ "`"
75
+ ]);
76
+ if (lastChar === "|" && firstChar === "<" && state && (state.depthMap[28] || 0) > 0) return true;
77
+ if (noSpaceAfter.has(lastChar) || noSpaceBefore.has(firstChar)) return false;
78
+ return true;
79
+ }
80
+ function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
81
+ if (!lastChar || lastChar === "\n" || lastChar === " " || lastChar === " " || lastChar === "[" || lastChar === ">") return false;
82
+ if (lastNode?.tagHandler?.isInline) return false;
83
+ const firstChar = textNode.value[0];
84
+ if (firstChar === " ") return false;
85
+ if (firstChar === "." || firstChar === "," || firstChar === "!" || firstChar === "?" || firstChar === ":" || firstChar === ";" || firstChar === "_" || firstChar === "*" || firstChar === "`" || firstChar === ")" || firstChar === "]") return false;
86
+ return true;
87
+ }
88
+ function canWrapHere(depthMap) {
89
+ if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
90
+ for (let h = 7; h <= 12; h++) if (depthMap[h]) return false;
91
+ return true;
92
+ }
93
+ function currentColumn(buffer) {
94
+ let col = 0;
95
+ for (let i = buffer.length - 1; i >= 0; i--) {
96
+ const s = buffer[i];
97
+ const nl = s.lastIndexOf("\n");
98
+ if (nl >= 0) return col + [...s.slice(nl + 1)].length;
99
+ col += [...s].length;
100
+ }
101
+ return col;
102
+ }
103
+ function wrapText(value, col, width, prefix) {
104
+ const leading = value.charCodeAt(0) === 32;
105
+ const trailing = value.charCodeAt(value.length - 1) === 32;
106
+ const prefixLen = [...prefix].length;
107
+ let out = "";
108
+ let first = true;
109
+ let i = 0;
110
+ const len = value.length;
111
+ while (i < len) {
112
+ let next = value.indexOf(" ", i);
113
+ if (next === -1) next = len;
114
+ if (next > i) {
115
+ const word = value.slice(i, next);
116
+ const wordLen = [...word].length;
117
+ const needSpace = first ? leading : true;
118
+ if (needSpace && col > prefixLen && col + 1 + wordLen > width) {
119
+ out += `\n${prefix}`;
120
+ col = prefixLen;
121
+ } else if (needSpace) {
122
+ out += " ";
123
+ col += 1;
124
+ }
125
+ out += word;
126
+ col += wordLen;
127
+ first = false;
128
+ }
129
+ i = next + 1;
130
+ }
131
+ if (trailing && out !== "" && !out.endsWith(" ") && !out.endsWith("\n")) out += " ";
132
+ if (out === "" && (leading || trailing)) out = " ";
133
+ return out;
134
+ }
135
+ function calculateNewLineConfig(node, depthMap, plainText) {
136
+ const tagId = node.tagId;
137
+ if (tagId !== 25 && (depthMap[25] || 0) > 0 || tagId !== 22 && (depthMap[22] || 0) > 0) {
138
+ if (plainText && tagId === 34) return [1, 1];
139
+ return NO_SPACING;
140
+ }
141
+ const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36);
142
+ let currParent = node.parent;
143
+ while (currParent) {
144
+ if (currParent.tagHandler?.collapsesInnerWhiteSpace) {
145
+ if (isBlockElement && currParent.tagId === 37) {
146
+ currParent = currParent.parent;
147
+ continue;
148
+ }
149
+ return NO_SPACING;
150
+ }
151
+ currParent = currParent.parent;
152
+ }
153
+ if (node.tagHandler?.spacing) return node.tagHandler?.spacing;
154
+ if (tagId === -1) return NO_SPACING;
155
+ return DEFAULT_BLOCK_SPACING;
156
+ }
157
+ function hasNonWhitespace(value) {
158
+ for (let i = 0; i < value.length; i++) {
159
+ const c = value.charCodeAt(i);
160
+ if (c !== 32 && c !== 9 && c !== 10 && c !== 13) return true;
161
+ }
162
+ return false;
163
+ }
164
+ function flushPreFence(state) {
165
+ if (state.plainText) {
166
+ state.preFencePending = false;
167
+ state.preOwnFence = false;
168
+ return;
169
+ }
170
+ state.preFencePending = false;
171
+ state.preOwnFence = true;
172
+ const lang = state.preFenceLang || "";
173
+ const fence = (state.depthMap[25] || 0) > 0 ? `\n\n${state.listIndent}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
174
+ state.buffer.push(fence);
175
+ state.lastContentCache = fence;
176
+ }
177
+ function getPlainTextOutput(node, eventType, state) {
178
+ const override = state.options?.plugins?.tagOverrides?.[node.name];
179
+ if (override && typeof override !== "string") {
180
+ const explicitOutput = eventType === 0 ? override.enter : override.exit;
181
+ if (explicitOutput !== void 0) return explicitOutput;
182
+ }
183
+ const tagId = node.tagId;
184
+ const depthMap = state.depthMap;
185
+ if (eventType === 0) {
186
+ if (tagId === 6) return "\n";
187
+ if (tagId === 35 && ((depthMap[22] || 0) > 0 || (depthMap[25] || 0) > 0 && !(depthMap[32] || 0) && !(depthMap[31] || 0))) {
188
+ const lastEntry = state.buffer.at(-1);
189
+ const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
190
+ if (lastChar && lastChar !== " " && lastChar !== "\n") return "\n\n";
191
+ }
192
+ if (tagId === 32 || tagId === 31) return (depthMap[28] || 0) > 1 || node.index === 0 ? "" : " ";
193
+ if (tagId === 27) return node.attributes?.alt || void 0;
194
+ if (tagId === 81) return "\"";
195
+ return;
196
+ }
197
+ if (tagId === 81) return "\"";
198
+ }
199
+ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverrideHandlers) {
200
+ const state = {
201
+ options,
202
+ buffer: [],
203
+ depthMap: /* @__PURE__ */ new Uint8Array(110),
204
+ listIndent: "",
205
+ listIndentWidths: [],
206
+ plainText: options.format === "text"
207
+ };
208
+ let lastYieldedLength = 0;
209
+ let hasYieldedContent = false;
210
+ let preserveLeadingWhitespace = false;
211
+ function processEvent(event) {
212
+ const { type: eventType, node } = event;
213
+ state.depth = node.depth;
214
+ if (node.type === 1 ? node.excludedFromMarkdown : node.parent?.excludedFromMarkdown) return;
215
+ const lastNode = state.lastNode;
216
+ state.lastNode = event.node;
217
+ const buff = state.buffer;
218
+ if (!state.plainText && state.preFencePending && eventType === 0) {
219
+ if (node.type === 1) {
220
+ const el = node;
221
+ if (el.tagId === 23 && el.parent?.tagId === 34) state.preFencePending = false;
222
+ else if (el.tagId !== 34) flushPreFence(state);
223
+ } else if (node.type === 2 && hasNonWhitespace(node.value)) flushPreFence(state);
224
+ }
225
+ const lastBuffEntry = buff.at(-1);
226
+ const lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
227
+ let secondLastChar;
228
+ if (lastBuffEntry && lastBuffEntry.length > 1) secondLastChar = lastBuffEntry.charAt(lastBuffEntry.length - 2);
229
+ else if (buff.length > 1) {
230
+ const prevBuff = buff[buff.length - 2];
231
+ if (prevBuff) secondLastChar = prevBuff.charAt(prevBuff.length - 1);
232
+ }
233
+ if (node.type === 2 && eventType === 0) {
234
+ const textNode = node;
235
+ if (textNode.value) {
236
+ if (textNode.excludedFromMarkdown) return;
237
+ if (state.preFencePending) return;
238
+ if (state.pendingInlineWhitespace) {
239
+ if (!textNode.value.trim()) return;
240
+ if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "")) textNode.value = ` ${textNode.value}`;
241
+ state.pendingInlineWhitespace = false;
242
+ }
243
+ if (state.plainText && state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
244
+ if (textNode.value === " " && (lastChar === " " || lastChar === "\n" || lastChar === " " || lastChar === "\r")) return;
245
+ if (!(state.plainText && state.depthMap[34]) && shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
246
+ if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
247
+ const indent = state.listIndent;
248
+ let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
249
+ if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
250
+ textNode.value = value;
251
+ }
252
+ const wrapWidth = state.options?.wrapWidth;
253
+ if (wrapWidth && canWrapHere(state.depthMap)) {
254
+ const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, continuationPrefix(textNode, state.listIndentWidths));
255
+ state.buffer.push(wrapped);
256
+ state.lastContentCache = wrapped;
257
+ } else {
258
+ state.buffer.push(textNode.value);
259
+ state.lastContentCache = textNode.value;
260
+ }
261
+ }
262
+ state.lastTextNode = textNode;
263
+ return;
264
+ }
265
+ if (node.type !== 1) return;
266
+ let output;
267
+ const element = node;
268
+ if (element.pluginOutput?.length) {
269
+ output = element.pluginOutput;
270
+ element.pluginOutput = void 0;
271
+ }
272
+ const lastFragment = state.lastContentCache;
273
+ let lastNewLines = 0;
274
+ if (lastChar === "\n") lastNewLines++;
275
+ if (secondLastChar === "\n") lastNewLines++;
276
+ const eventFn = eventType === 0 ? "enter" : "exit";
277
+ const handler = node.tagHandler;
278
+ if (!output && handler?.[eventFn]) {
279
+ const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
280
+ node: element,
281
+ state
282
+ });
283
+ if (res) output = [res];
284
+ }
285
+ if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
286
+ const configuredNewLines = calculateNewLineConfig(node, state.depthMap, state.plainText === true)[eventType] || 0;
287
+ const newLines = Math.max(0, configuredNewLines - lastNewLines);
288
+ const isInlineElement = node.tagHandler?.isInline === true;
289
+ if (state.pendingInlineWhitespace) {
290
+ const firstOutput = output?.[0]?.[0] || "";
291
+ if (eventType === 0) {
292
+ if (!isInlineElement || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
293
+ else if (firstOutput) {
294
+ if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput)) state.buffer.push(" ");
295
+ state.pendingInlineWhitespace = false;
296
+ }
297
+ } else if (!isInlineElement || newLines > 0) state.pendingInlineWhitespace = false;
298
+ }
299
+ if (newLines > 0) {
300
+ if (!buff.length) {
301
+ if (output) {
302
+ for (const fragment of output) if (fragment) {
303
+ state.buffer.push(fragment);
304
+ state.lastContentCache = fragment;
305
+ }
306
+ }
307
+ updateListIndent(state, element, eventType);
308
+ return;
309
+ }
310
+ const newlinesStr = "\n".repeat(newLines);
311
+ if (lastChar === " " && buff?.length) {
312
+ buff[buff.length - 1] = buff.at(-1).substring(0, buff.at(-1).length - 1);
313
+ state.lastTextNode = void 0;
314
+ }
315
+ if (eventType === 0) if (output) output.unshift(newlinesStr);
316
+ else output = [newlinesStr];
317
+ else if (output) output.push(newlinesStr);
318
+ else output = [newlinesStr];
319
+ } else if (lastFragment && state.lastTextNode?.containsWhitespace && (!!node.parent || isInlineElement) && "value" in state.lastTextNode && typeof state.lastTextNode.value === "string") {
320
+ let parent = node.parent;
321
+ let parentInPre = false;
322
+ while (parent) {
323
+ if (parent.tagId === 34) {
324
+ parentInPre = true;
325
+ break;
326
+ }
327
+ parent = parent.parent;
328
+ }
329
+ if (!parentInPre || node.parent?.tagId === 34) {
330
+ const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
331
+ const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
332
+ const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
333
+ if (element.tagId === 6 && output?.[0]?.[0] === "\n" && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0)) {
334
+ const originalLength = lastFragment.length;
335
+ const trimmed = lastFragment.trimEnd();
336
+ if (originalLength - trimmed.length > 0) {
337
+ if (buff?.length && buff.at(-1) === lastFragment) buff[buff.length - 1] = trimmed;
338
+ if (eventType === 1 && isInlineElement) state.pendingInlineWhitespace = true;
339
+ }
340
+ }
341
+ state.lastTextNode = void 0;
342
+ }
343
+ }
344
+ if (output?.[0]?.[0] && eventType === 0 && !node.tagHandler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
345
+ state.buffer.push(" ");
346
+ state.lastContentCache = " ";
347
+ }
348
+ if (output) {
349
+ for (const fragment of output) if (fragment) {
350
+ state.buffer.push(fragment);
351
+ state.lastContentCache = fragment;
352
+ }
353
+ }
354
+ updateListIndent(state, element, eventType);
355
+ }
356
+ function processHtml(html) {
357
+ const parseState = {
358
+ depthMap: state.depthMap,
359
+ depth: 0,
360
+ resolvedPlugins,
361
+ tagOverrideHandlers,
362
+ plainText: state.plainText
363
+ };
364
+ const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, state, processEvent) : processEvent;
365
+ finalizeParse(parseHtmlStream(html, parseState, handleEvent), parseState, handleEvent);
366
+ }
367
+ function getMarkdown() {
368
+ const content = state.buffer.join("");
369
+ const result = state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
370
+ state.buffer.length = 0;
371
+ preserveLeadingWhitespace = false;
372
+ return result.trimEnd();
373
+ }
374
+ function getMarkdownChunk() {
375
+ const content = state.buffer.join("");
376
+ const currentContent = hasYieldedContent || state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
377
+ const hasMutableTrailingSpace = state.lastTextNode?.containsWhitespace && !state.depthMap[34] && currentContent.endsWith(" ");
378
+ let stableLength = currentContent.length;
379
+ if (hasMutableTrailingSpace) while (stableLength > 0 && currentContent[stableLength - 1] === " ") stableLength--;
380
+ const newContent = currentContent.slice(lastYieldedLength, stableLength);
381
+ lastYieldedLength = stableLength;
382
+ if (newContent) hasYieldedContent = true;
383
+ if (!hasMutableTrailingSpace && !resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
384
+ const tailStart = Math.max(0, stableLength - 2);
385
+ const emittedTail = currentContent.slice(tailStart, stableLength);
386
+ const mutableTail = currentContent.slice(stableLength);
387
+ state.buffer.length = 0;
388
+ if (emittedTail || mutableTail) state.buffer.push(emittedTail + mutableTail);
389
+ lastYieldedLength = emittedTail.length;
390
+ } else if (state.buffer.length > 1 && !hasMutableTrailingSpace) {
391
+ state.buffer.length = 0;
392
+ state.buffer.push(currentContent);
393
+ }
394
+ return newContent;
395
+ }
396
+ return {
397
+ processEvent,
398
+ processHtml,
399
+ getMarkdown,
400
+ getMarkdownChunk,
401
+ state
402
+ };
403
+ }
404
+ export { createMarkdownProcessor, processPluginsForEvent };