@mdream/js 1.5.7 → 1.5.10

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.
@@ -84,9 +84,16 @@ function createCompoundSelector(selectors) {
84
84
  toString: () => selectors.map((s) => s.toString()).join("")
85
85
  };
86
86
  }
87
- function parseSelector(selector) {
88
- selector = selector.trim();
89
- if (!selector) throw new Error("Empty selector");
87
+ function createSelectorList(selectors) {
88
+ return {
89
+ matches(element) {
90
+ for (let i = 0; i < selectors.length; i++) if (selectors[i].matches(element)) return true;
91
+ return false;
92
+ },
93
+ toString: () => selectors.map((s) => s.toString()).join(", ")
94
+ };
95
+ }
96
+ function parseCompoundSelector(selector) {
90
97
  const selectorParts = [];
91
98
  let current = "";
92
99
  let inAttribute = false;
@@ -112,6 +119,41 @@ function parseSelector(selector) {
112
119
  if (selectorParts.length === 1) return selectorParts[0];
113
120
  return createCompoundSelector(selectorParts);
114
121
  }
122
+ function parseSelector(selector) {
123
+ selector = selector.trim();
124
+ if (!selector) throw new Error("Empty selector");
125
+ const selectors = [];
126
+ let start = 0;
127
+ let inAttribute = false;
128
+ let quote = 0;
129
+ let escaped = false;
130
+ for (let i = 0; i < selector.length; i++) {
131
+ const char = selector.charCodeAt(i);
132
+ if (quote) {
133
+ if (escaped) escaped = false;
134
+ else if (char === 92) escaped = true;
135
+ else if (char === quote) quote = 0;
136
+ continue;
137
+ }
138
+ if (inAttribute) {
139
+ if (char === 34 || char === 39) quote = char;
140
+ else if (char === 93) inAttribute = false;
141
+ continue;
142
+ }
143
+ if (char === 91) inAttribute = true;
144
+ else if (char === 44) {
145
+ const part = selector.slice(start, i).trim();
146
+ if (!part) return createTagSelector(selector);
147
+ selectors.push(parseCompoundSelector(part));
148
+ start = i + 1;
149
+ }
150
+ }
151
+ if (!selectors.length) return parseCompoundSelector(selector);
152
+ const last = selector.slice(start).trim();
153
+ if (!last) return createTagSelector(selector);
154
+ selectors.push(parseCompoundSelector(last));
155
+ return createSelectorList(selectors);
156
+ }
115
157
  function extractionPlugin(selectors) {
116
158
  const matcherCallbacks = Object.entries(selectors).map(([selector, callback]) => ({
117
159
  matcher: parseSelector(selector),
@@ -120,11 +162,17 @@ function extractionPlugin(selectors) {
120
162
  const trackedElements = /* @__PURE__ */ new Map();
121
163
  return createPlugin({
122
164
  onNodeEnter(element) {
123
- matcherCallbacks.forEach(({ matcher, callback }) => {
124
- if (matcher.matches(element)) trackedElements.set(element, {
125
- textContent: "",
126
- callback
127
- });
165
+ let callbacks;
166
+ for (let i = 0; i < matcherCallbacks.length; i++) {
167
+ const { matcher, callback } = matcherCallbacks[i];
168
+ if (matcher.matches(element)) {
169
+ callbacks ||= [];
170
+ callbacks.push(callback);
171
+ }
172
+ }
173
+ if (callbacks) trackedElements.set(element, {
174
+ textContent: "",
175
+ callbacks
128
176
  });
129
177
  },
130
178
  processTextNode(textNode) {
@@ -138,11 +186,14 @@ function extractionPlugin(selectors) {
138
186
  onNodeExit(element, state) {
139
187
  const tracked = trackedElements.get(element);
140
188
  if (tracked) {
141
- const extractedElement = {
142
- ...element,
143
- textContent: tracked.textContent.trim()
144
- };
145
- tracked.callback(extractedElement, state);
189
+ for (let i = 0; i < tracked.callbacks.length; i++) {
190
+ const extractedElement = {
191
+ ...element,
192
+ attributes: { ...element.attributes },
193
+ textContent: tracked.textContent.trim()
194
+ };
195
+ tracked.callbacks[i](extractedElement, state);
196
+ }
146
197
  trackedElements.delete(element);
147
198
  }
148
199
  }
@@ -158,14 +209,18 @@ function extractionCollectorPlugin(config) {
158
209
  const trackedElements = /* @__PURE__ */ new Map();
159
210
  const plugin = createPlugin({
160
211
  onNodeEnter(element) {
212
+ let selectors;
161
213
  for (let i = 0; i < matchers.length; i++) {
162
214
  const m = matchers[i];
163
- if (m.matcher.matches(element)) trackedElements.set(element, {
164
- textContent: "",
165
- selector: m.selector,
166
- callback: m.callback
167
- });
215
+ if (m.matcher.matches(element)) {
216
+ selectors ||= [];
217
+ selectors.push(m.selector);
218
+ }
168
219
  }
220
+ if (selectors) trackedElements.set(element, {
221
+ textContent: "",
222
+ selectors
223
+ });
169
224
  },
170
225
  processTextNode(textNode) {
171
226
  let currentParent = textNode.parent;
@@ -178,13 +233,15 @@ function extractionCollectorPlugin(config) {
178
233
  onNodeExit(element) {
179
234
  const tracked = trackedElements.get(element);
180
235
  if (tracked) {
181
- const extracted = {
182
- selector: tracked.selector,
183
- tagName: element.name,
184
- textContent: tracked.textContent.trim(),
185
- attributes: { ...element.attributes }
186
- };
187
- results.push(extracted);
236
+ for (let i = 0; i < tracked.selectors.length; i++) {
237
+ const extracted = {
238
+ selector: tracked.selectors[i],
239
+ tagName: element.name,
240
+ textContent: tracked.textContent.trim(),
241
+ attributes: { ...element.attributes }
242
+ };
243
+ results.push(extracted);
244
+ }
188
245
  trackedElements.delete(element);
189
246
  }
190
247
  }
@@ -227,6 +284,7 @@ function filterPlugin(options = {}) {
227
284
  const excludeSelectors = options.exclude?.map((selector) => compileSelector(selector)) || [];
228
285
  const processChildren = options.processChildren !== false;
229
286
  const skippedSubtrees = /* @__PURE__ */ new WeakSet();
287
+ const skippedByInclude = /* @__PURE__ */ new WeakSet();
230
288
  function excludesSubtree(element) {
231
289
  if (skippedSubtrees.has(element)) return true;
232
290
  if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element) || excludeSelectors.some((selector) => selector.matches(element))) {
@@ -240,8 +298,10 @@ function filterPlugin(options = {}) {
240
298
  beforeNodeProcess(event) {
241
299
  const { node } = event;
242
300
  if (node.type === 2) {
243
- const parent = node.parent;
301
+ const textNode = node;
302
+ const parent = textNode.parent;
244
303
  if (parent && skippedSubtrees.has(parent)) return { skip: true };
304
+ if (parent && skippedByInclude.has(parent)) textNode.excludedFromMarkdown = true;
245
305
  return;
246
306
  }
247
307
  if (node.type !== 1) return;
@@ -254,6 +314,7 @@ function filterPlugin(options = {}) {
254
314
  if (!processChildren) break;
255
315
  currentElement = currentElement.parent;
256
316
  }
317
+ skippedByInclude.add(element);
257
318
  return { skip: true };
258
319
  }
259
320
  }
@@ -259,6 +259,8 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
259
259
  tableRenderedTable?: boolean;
260
260
  tableCurrentRowCells?: number;
261
261
  tableColumnAlignments?: string[];
262
+ /** See MarkdownState for semantics. */
263
+ tableHeaderCells?: number;
262
264
  /** Resolved plugin instances for efficient iteration */
263
265
  resolvedPlugins?: TransformPlugin[];
264
266
  /** Content buffer for markdown output */
@@ -280,9 +282,11 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
280
282
  */
281
283
  preFencePending?: boolean;
282
284
  preFenceLang?: string;
283
- preOwnFence?: boolean;
285
+ preFenceOpen?: boolean;
284
286
  /** Number of default blockquotes currently buffered for line prefixing. */
285
287
  bufferedBlockquoteDepth?: number;
288
+ /** Content-column prefix deferred after a list item rule. */
289
+ listRulePending?: string;
286
290
  /** Whether output should omit Markdown/HTML markup */
287
291
  plainText?: boolean;
288
292
  }
@@ -330,9 +334,6 @@ type GfmAction = {
330
334
  _tag: 'CodeFenceEnter';
331
335
  language: string;
332
336
  output: string;
333
- } | {
334
- _tag: 'CodeFenceExit';
335
- output: string;
336
337
  };
337
338
  type TagHandlerResult = string | GfmAction | undefined | void;
338
339
  /**
package/dist/parse.d.mts CHANGED
@@ -5,12 +5,12 @@ import { ElementNode, Node, NodeEvent, TagHandler, TransformPlugin } from "./_ch
5
5
  * pending (a later chunk might continue them); at true EOF they must be
6
6
  * committed so trailing content is not dropped (e.g. `<p>a<p>b`).
7
7
  *
8
- * `leftover` is the residual returned by the final `parseHtmlStream`. Pure
9
- * trailing text (no leading `<`) is emitted; a residual that is an incomplete
10
- * start tag (leading `<`) is dropped, matching the browser tokenizer's
11
- * EOF-in-tag behaviour. The text-buffer flags set while the trailing text was
12
- * scanned persist on `state`, so `processTextBuffer` commits it as if the next
13
- * tag had triggered the flush.
8
+ * `leftover` is the residual returned by the final `parseHtmlStream`. Trailing
9
+ * text is emitted; a residual that is an incomplete start tag is dropped,
10
+ * matching the browser tokenizer's EOF-in-tag behaviour. A leading `<` alone
11
+ * does not make it a tag: `<3` is text. The text-buffer flags set while the
12
+ * trailing text was scanned persist on `state`, so `processTextBuffer` commits
13
+ * it as if the next tag had triggered the flush.
14
14
  */
15
15
  declare function finalizeParse(leftover: string, state: ParseState, handleEvent: (event: NodeEvent) => void): void;
16
16
  interface ParseOptions {
@@ -3,8 +3,9 @@ import { createPlugin } from "./_chunks/index.mjs";
3
3
  interface ExtractedElement extends ElementNode {
4
4
  textContent: string;
5
5
  }
6
+ type ExtractionCallback = (element: ExtractedElement, state: MdreamRuntimeState) => void;
6
7
  /** Extract matching elements through the composable JavaScript plugin interface. */
7
- declare function extractionPlugin(selectors: Record<string, (element: ExtractedElement, state: MdreamRuntimeState) => void>): TransformPlugin;
8
+ declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): TransformPlugin;
8
9
  /**
9
10
  * Extraction collector for `plugins.extraction` config.
10
11
  * Collects results during processing; callbacks are called post-conversion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.5.7",
4
+ "version": "1.5.10",
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",