@helping-ai-workflow/md2doc 2.1.0 → 2.2.0

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.
Files changed (2) hide show
  1. package/lib/md2doc.js +46 -5
  2. package/package.json +2 -2
package/lib/md2doc.js CHANGED
@@ -88,7 +88,7 @@ const waveDromTag = inlineScriptTag(localWaveDromJs)
88
88
 
89
89
  const mermaidScriptTag = inlineScriptTag(localMermaidJs)
90
90
  || `<script type="module">
91
- import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
91
+ import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
92
92
  mermaid.initialize({ startOnLoad: true, theme: 'default' });
93
93
  </script>`;
94
94
 
@@ -309,7 +309,11 @@ ${itemsHtml}
309
309
  return `\n<script type="WaveDrom">\n${code}\n</script>\n`;
310
310
  }
311
311
  if (lang === 'mermaid') {
312
- return `\n<div class="mermaid">\n${code}\n</div>\n`;
312
+ // Escape so the browser delivers the literal source to mermaid. Raw
313
+ // injection lets the HTML parser consume entities and tags first —
314
+ // an author's &lt;IP&gt; became an <IP> element mermaid sanitized
315
+ // away — diverging from GitHub's escaped-code-block semantics.
316
+ return `\n<div class="mermaid">\n${escapeHtml(code)}\n</div>\n`;
313
317
  }
314
318
  if (lang === 'dot' || lang === 'graphviz') {
315
319
  const r = spawnSync('dot', ['-Tsvg'], { input: code, encoding: 'utf8', timeout: 10000 });
@@ -411,15 +415,52 @@ ${itemsHtml}
411
415
 
412
416
  marked.setOptions({ gfm: true, breaks: false, renderer });
413
417
 
414
- // Pre-process non-standard inline syntax before marked parses
418
+ // Code-aware subscript / superscript as marked inline extensions.
419
+ // The old raw-text pre-pass (mdPre.replace(/~([^~]+)~/...)) ran BEFORE marked
420
+ // tokenised, so it rewrote ~NOT / ^XOR operators inside fenced, indented and
421
+ // inline code into <sub>/<sup> (96 such mangles in one RTL spec). As inline
422
+ // extensions marked tokenises code first, so these never fire inside code.
423
+ // The tokenizer also requires a single whitespace-free token (~x~ / ^x^), so
424
+ // spaced operator expressions (~a & ~b, a ^ b) and lone operators (2^24, ~rst)
425
+ // stay literal even in prose — only a genuine subscript/superscript converts.
426
+ marked.use({
427
+ extensions: [
428
+ {
429
+ name: 'subscript',
430
+ level: 'inline',
431
+ start(src) { const i = src.indexOf('~'); return i < 0 ? undefined : i; },
432
+ tokenizer(src) {
433
+ const m = /^~(?=\S)([^~\s\n]+)~/.exec(src);
434
+ if (m) {
435
+ return { type: 'subscript', raw: m[0], text: m[1],
436
+ tokens: this.lexer.inlineTokens(m[1]) };
437
+ }
438
+ },
439
+ renderer(token) { return `<sub>${this.parser.parseInline(token.tokens)}</sub>`; },
440
+ },
441
+ {
442
+ name: 'superscript',
443
+ level: 'inline',
444
+ start(src) { const i = src.indexOf('^'); return i < 0 ? undefined : i; },
445
+ tokenizer(src) {
446
+ const m = /^\^(?=\S)([^^\s\n]+)\^/.exec(src);
447
+ if (m) {
448
+ return { type: 'superscript', raw: m[0], text: m[1],
449
+ tokens: this.lexer.inlineTokens(m[1]) };
450
+ }
451
+ },
452
+ renderer(token) { return `<sup>${this.parser.parseInline(token.tokens)}</sup>`; },
453
+ },
454
+ ],
455
+ });
456
+
457
+ // Pre-process the remaining non-standard inline syntax before marked parses
415
458
  const escAttr = (s) => String(s)
416
459
  .replace(/&/g, '&amp;')
417
460
  .replace(/</g, '&lt;')
418
461
  .replace(/>/g, '&gt;')
419
462
  .replace(/"/g, '&quot;');
420
463
  const mdPre = md
421
- .replace(/\^([^^]+)\^/g, '<sup>$1</sup>') // ^a^ → <sup>a</sup>
422
- .replace(/~([^~]+)~/g, '<sub>$1</sub>') // ~a~ → <sub>a</sub>
423
464
  .replace(/\[\[([^\]\n]+)\]\]/g, (_, inner) => { // [[ref-id, §sub]] → clickable citation
424
465
  const body = inner.trim();
425
466
  const slug = body.split(',', 1)[0].trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helping-ai-workflow/md2doc",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Markdown → HTML / PDF renderer with WaveDrom, Mermaid, and Graphviz support",
5
5
  "keywords": [
6
6
  "markdown",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "scripts": {
33
33
  "preinstall": "node scripts/preinstall.js",
34
- "test": "node test/md2doc.test.js && node test/cli.test.js"
34
+ "test": "node test/md2doc.test.js && node test/cli.test.js && node test/code-operator.test.js"
35
35
  },
36
36
  "repository": {
37
37
  "type": "git",