@mdream/js 1.5.5 → 1.5.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.
@@ -227,34 +227,37 @@ function filterPlugin(options = {}) {
227
227
  const excludeSelectors = options.exclude?.map((selector) => compileSelector(selector)) || [];
228
228
  const processChildren = options.processChildren !== false;
229
229
  const skippedSubtrees = /* @__PURE__ */ new WeakSet();
230
- return createPlugin({ beforeNodeProcess(event) {
231
- const { node } = event;
232
- if (node.type === 2) {
233
- const parent = node.parent;
234
- if (parent && skippedSubtrees.has(parent)) return { skip: true };
235
- return;
236
- }
237
- if (node.type !== 1) return;
238
- const element = node;
239
- if (skippedSubtrees.has(element)) return { skip: true };
240
- if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element)) {
230
+ function excludesSubtree(element) {
231
+ if (skippedSubtrees.has(element)) return true;
232
+ if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element) || excludeSelectors.some((selector) => selector.matches(element))) {
241
233
  skippedSubtrees.add(element);
242
- return { skip: true };
234
+ return true;
243
235
  }
244
- if (excludeSelectors.length && excludeSelectors.some((selector) => selector.matches(element))) {
245
- skippedSubtrees.add(element);
246
- return { skip: true };
247
- }
248
- if (includeSelectors.length) {
249
- let currentElement = element;
250
- while (currentElement) {
251
- if (includeSelectors.some((selector) => selector.matches(currentElement))) return;
252
- if (!processChildren) break;
253
- currentElement = currentElement.parent;
236
+ return false;
237
+ }
238
+ return createPlugin({
239
+ excludesOverflowSubtree: excludesSubtree,
240
+ beforeNodeProcess(event) {
241
+ const { node } = event;
242
+ if (node.type === 2) {
243
+ const parent = node.parent;
244
+ if (parent && skippedSubtrees.has(parent)) return { skip: true };
245
+ return;
246
+ }
247
+ if (node.type !== 1) return;
248
+ const element = node;
249
+ if (excludesSubtree(element)) return { skip: true };
250
+ if (includeSelectors.length) {
251
+ let currentElement = element;
252
+ while (currentElement) {
253
+ if (includeSelectors.some((selector) => selector.matches(currentElement))) return;
254
+ if (!processChildren) break;
255
+ currentElement = currentElement.parent;
256
+ }
257
+ return { skip: true };
254
258
  }
255
- return { skip: true };
256
259
  }
257
- } });
260
+ });
258
261
  }
259
262
  const BACKSLASH_RE = /\\/g;
260
263
  const DOUBLE_QUOTE_RE = /"/g;
@@ -546,15 +549,6 @@ function groupByFormattingType(classes) {
546
549
  }
547
550
  return groups;
548
551
  }
549
- function fixRedundantDelimiters(content) {
550
- content = content.replaceAll("****", "**");
551
- content = content.replaceAll("~~~~", "~~");
552
- if (content.includes("***") && content.split("***").length > 3) {
553
- const parts = content.split("***");
554
- if (parts.length >= 4) content = `${parts[0]}***${parts[1]} ${parts[2]}***${parts.slice(3).join("***")}`;
555
- }
556
- return content;
557
- }
558
552
  function normalizeClasses(classes) {
559
553
  const result = [];
560
554
  const mobileClasses = classes.filter((cls) => !hasBreakpoint(cls));
@@ -620,6 +614,11 @@ function processTailwindClasses(classes) {
620
614
  }
621
615
  function tailwindPlugin() {
622
616
  return createPlugin({
617
+ excludesOverflowSubtree(node) {
618
+ if (node.parent?.context?.tailwind?.hidden) return true;
619
+ const classes = node.attributes?.class?.trim().split(" ").filter(Boolean);
620
+ return classes ? processTailwindClasses(classes).hidden : false;
621
+ },
623
622
  processAttributes(node) {
624
623
  const parentHidden = node.parent?.context?.tailwind?.hidden;
625
624
  const classAttr = node.attributes?.class;
@@ -648,16 +647,11 @@ function tailwindPlugin() {
648
647
  content: "",
649
648
  skip: true
650
649
  };
651
- let content = node.value;
652
650
  const prefix = state.options?.format === "text" ? "" : tailwindData?.prefix || "";
653
651
  const suffix = state.options?.format === "text" ? "" : tailwindData?.suffix || "";
654
- if (prefix || suffix) {
655
- content = prefix + content + suffix;
656
- content = fixRedundantDelimiters(content);
657
- }
658
- return {
659
- content,
660
- skip: false
652
+ if (prefix || suffix) node.generatedMarkdown = {
653
+ prefix,
654
+ suffix
661
655
  };
662
656
  },
663
657
  beforeNodeProcess({ node }) {
@@ -1,5 +1,5 @@
1
1
  import "./const.mjs";
2
- import { buildTagOverrideHandlers } from "./parse.mjs";
2
+ import { buildTagOverrideHandlers, isEmptyLinkHref } from "./parse.mjs";
3
3
  import { createMarkdownProcessor } from "./markdown-processor.mjs";
4
4
  import "./plugins.mjs";
5
5
  import { resolvePlugins } from "./resolve-plugins.mjs";
@@ -154,7 +154,7 @@ function cleanEmptyLinks(md) {
154
154
  const link = parseLink(md, i);
155
155
  if (link) {
156
156
  const url = link.url;
157
- if (url === "#" || url.startsWith("javascript:") || url.startsWith("data:") || url.startsWith("vbscript:")) {
157
+ if (isEmptyLinkHref(url)) {
158
158
  result += link.text;
159
159
  i = link.end;
160
160
  continue;
@@ -14,6 +14,11 @@ declare const NodeEventExit$1 = 1;
14
14
  * For declarative config that works with both engines, use `BuiltinPlugins`.
15
15
  */
16
16
  interface TransformPlugin {
17
+ /**
18
+ * Whether a flattened element starts an output-excluded subtree.
19
+ * @internal
20
+ */
21
+ excludesOverflowSubtree?: (node: ElementNode) => boolean;
17
22
  /**
18
23
  * Process a node before it's handled by the parser
19
24
  */
@@ -159,14 +164,19 @@ interface ElementNode extends Node {
159
164
  context?: PluginContext;
160
165
  /** ID of the tag for fast handler lookup */
161
166
  tagId?: number;
162
- /** Map of tag names to their nesting count (using Uint8Array for performance) */
163
- depthMap: Uint8Array;
167
+ /** Map of tag names to their nesting count */
168
+ depthMap: Uint16Array;
164
169
  /** Plugin outputs collected during processing */
165
170
  pluginOutput?: string[];
166
171
  }
167
172
  interface TextNode extends Node {
168
173
  /** Text content (for TEXT_NODE) */
169
174
  value: string;
175
+ /** Markdown delimiters generated by a plugin around this literal text. */
176
+ generatedMarkdown?: {
177
+ prefix: string;
178
+ suffix: string;
179
+ };
170
180
  /** Custom data added by plugins */
171
181
  context?: PluginContext;
172
182
  }
@@ -203,7 +213,7 @@ interface Node {
203
213
  */
204
214
  interface MdreamProcessingState {
205
215
  /** Map of tag names to their current nesting depth - uses TypedArray for performance */
206
- depthMap: Uint8Array;
216
+ depthMap: Uint16Array;
207
217
  /** Current overall nesting depth */
208
218
  depth: number;
209
219
  /** Currently processing element node */
@@ -271,6 +281,8 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
271
281
  preFencePending?: boolean;
272
282
  preFenceLang?: string;
273
283
  preOwnFence?: boolean;
284
+ /** Number of default blockquotes currently buffered for line prefixing. */
285
+ bufferedBlockquoteDepth?: number;
274
286
  /** Whether output should omit Markdown/HTML markup */
275
287
  plainText?: boolean;
276
288
  }
@@ -298,13 +310,38 @@ interface HandlerContext {
298
310
  /** Runtime state */
299
311
  state: MdreamRuntimeState;
300
312
  }
313
+ /** Internal serializer lifecycle emitted only by built-in GFM handlers. */
314
+ type GfmAction = {
315
+ _tag: 'BlockquoteEnter';
316
+ output?: string;
317
+ } | {
318
+ _tag: 'BlockquoteExit';
319
+ } | {
320
+ _tag: 'PreEnter';
321
+ language: string;
322
+ } | {
323
+ _tag: 'PreExit';
324
+ } | {
325
+ _tag: 'CodeSpanEnter';
326
+ output: string;
327
+ } | {
328
+ _tag: 'CodeSpanExit';
329
+ } | {
330
+ _tag: 'CodeFenceEnter';
331
+ language: string;
332
+ output: string;
333
+ } | {
334
+ _tag: 'CodeFenceExit';
335
+ output: string;
336
+ };
337
+ type TagHandlerResult = string | GfmAction | undefined | void;
301
338
  /**
302
339
  * Tag handler interface for HTML elements
303
340
  * Used by plugins to extend or customize tag handling
304
341
  */
305
342
  interface TagHandler {
306
- enter?: (context: HandlerContext) => string | undefined | void;
307
- exit?: (context: HandlerContext) => string | undefined | void;
343
+ enter?: (context: HandlerContext) => TagHandlerResult;
344
+ exit?: (context: HandlerContext) => TagHandlerResult;
308
345
  isSelfClosing?: boolean;
309
346
  isNonNesting?: boolean;
310
347
  collapsesInnerWhiteSpace?: boolean;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { htmlToMarkdown, streamHtmlToMarkdown } from "./_chunks/src.mjs";
2
1
  import { ELEMENT_NODE, NodeEventEnter, NodeEventExit, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE } from "./_chunks/const.mjs";
2
+ import { htmlToMarkdown, streamHtmlToMarkdown } from "./_chunks/src.mjs";
3
3
  import { createPlugin } from "./_chunks/plugins.mjs";
4
4
  import { withMinimalPreset } from "./preset/minimal.mjs";
5
5
  export { ELEMENT_NODE, NodeEventEnter, NodeEventExit, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, createPlugin, htmlToMarkdown, streamHtmlToMarkdown, withMinimalPreset };
package/dist/parse.d.mts CHANGED
@@ -18,11 +18,22 @@ interface ParseOptions {
18
18
  }
19
19
  interface ParseState {
20
20
  /** Map of tag names to their current nesting depth - uses TypedArray for performance */
21
- depthMap: Uint8Array;
21
+ depthMap: Uint16Array;
22
22
  /** Current overall nesting depth */
23
23
  depth: number;
24
- /** Whether parsing stopped after reaching the practical browser depth limit. */
25
- depthLimitReached?: boolean;
24
+ /** Root tag currently flattened beyond the rich element depth. */
25
+ flattenedTagName?: string;
26
+ /** Resolved built-in identity of the flattened root. */
27
+ flattenedTagId?: number;
28
+ /** Same-name nesting below the flattened root. */
29
+ flattenedTagDepth?: number;
30
+ /** Exact output-excluded scope nested inside the flattened subtree. */
31
+ flattenedOpaqueTagName?: string;
32
+ flattenedOpaqueDepth?: number;
33
+ /** Raw-text tag currently open inside the flattened subtree. */
34
+ flattenedRawTagName?: string;
35
+ /** Whether flattened raw text must be excluded from output. */
36
+ flattenedRawExcludesText?: boolean;
26
37
  /** Currently processing element node */
27
38
  currentNode?: ElementNode | null;
28
39
  /** Whether current content contains HTML entities that need decoding */
package/dist/splitter.mjs CHANGED
@@ -23,12 +23,6 @@ function createOptions(options) {
23
23
  resolvedPlugins: resolvePlugins(options).plugins
24
24
  };
25
25
  }
26
- function getCodeLanguage(node) {
27
- const className = node.attributes?.class;
28
- if (!className) return "";
29
- const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
30
- return langParts && langParts.length > 0 ? langParts[0].trim() : "";
31
- }
32
26
  function shouldSplitOnHeader(tagId, options) {
33
27
  return options.headersToSplitOn.includes(tagId);
34
28
  }
@@ -38,10 +32,13 @@ function getCurrentMarkdown(state) {
38
32
  function* htmlToMarkdownSplitChunksStream(html, options = {}) {
39
33
  const opts = createOptions(options);
40
34
  if (opts.chunkOverlap >= opts.chunkSize) throw new Error("chunkOverlap must be less than chunkSize");
35
+ let currentChunkCodeLanguage = "";
41
36
  const processor = createMarkdownProcessor(options, opts.resolvedPlugins);
37
+ processor.state.onCodeFenceOpen = (language) => {
38
+ if (language && !currentChunkCodeLanguage) currentChunkCodeLanguage = language;
39
+ };
42
40
  const headerHierarchy = /* @__PURE__ */ new Map();
43
41
  const seenSplitHeaders = /* @__PURE__ */ new Set();
44
- let currentChunkCodeLanguage = "";
45
42
  let collectingHeaderText = false;
46
43
  let currentHeaderTagId = null;
47
44
  let currentHeaderText = "";
@@ -108,8 +105,7 @@ function* htmlToMarkdownSplitChunksStream(html, options = {}) {
108
105
  if (tagId !== void 0 && tagId >= 0 && tagId < processor.state.depthMap.length) processor.state.depthMap[tagId] = Math.max(0, processor.state.depthMap[tagId] - 1);
109
106
  }
110
107
  if (node.type === 1) {
111
- const element = node;
112
- const tagId = element.tagId;
108
+ const tagId = node.tagId;
113
109
  if (tagId && tagId >= 7 && tagId <= 12) {
114
110
  if (eventType === 0) {
115
111
  collectingHeaderText = true;
@@ -128,12 +124,6 @@ function* htmlToMarkdownSplitChunksStream(html, options = {}) {
128
124
  currentHeaderTagId = null;
129
125
  }
130
126
  }
131
- if (tagId === 23 && (element.depthMap[34] || 0) > 0) {
132
- if (eventType === 0) {
133
- const lang = getCodeLanguage(element);
134
- if (lang && !currentChunkCodeLanguage) currentChunkCodeLanguage = lang;
135
- }
136
- }
137
127
  if (tagId === 13 && eventType === 0) yield* flushChunk();
138
128
  }
139
129
  if (collectingHeaderText && node.type === 2) currentHeaderText += node.value;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.5.5",
4
+ "version": "1.5.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",