mathpix-markdown-it 2.0.34 → 2.0.36

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 (39) hide show
  1. package/README.md +123 -1
  2. package/doc/changelog.md +42 -0
  3. package/es5/browser/add-speech.js +1 -0
  4. package/es5/browser/auto-render.js +33 -0
  5. package/es5/bundle.js +1 -1
  6. package/es5/index.js +1 -1
  7. package/lib/browser/add-speech.d.ts +17 -0
  8. package/lib/browser/add-speech.js +97 -0
  9. package/lib/browser/add-speech.js.map +1 -0
  10. package/lib/browser/auto-render.d.ts +20 -0
  11. package/lib/browser/auto-render.js +319 -0
  12. package/lib/browser/auto-render.js.map +1 -0
  13. package/lib/browser/utils.d.ts +4 -0
  14. package/lib/browser/utils.js +11 -0
  15. package/lib/browser/utils.js.map +1 -0
  16. package/lib/markdown/common/convert-math-to-html.d.ts +4 -1
  17. package/lib/markdown/common/convert-math-to-html.js +174 -93
  18. package/lib/markdown/common/convert-math-to-html.js.map +1 -1
  19. package/lib/markdown/common/render-table-cell-content.js +14 -8
  20. package/lib/markdown/common/render-table-cell-content.js.map +1 -1
  21. package/lib/markdown/highlight/highlight-math-token.js +5 -3
  22. package/lib/markdown/highlight/highlight-math-token.js.map +1 -1
  23. package/lib/mathjax/index.d.ts +6 -0
  24. package/lib/mathjax/index.js +103 -33
  25. package/lib/mathjax/index.js.map +1 -1
  26. package/lib/mathpix-markdown-model/index.d.ts +1 -0
  27. package/lib/mathpix-markdown-model/index.js +1 -0
  28. package/lib/mathpix-markdown-model/index.js.map +1 -1
  29. package/lib/sre/index.d.ts +8 -0
  30. package/lib/sre/index.js +38 -20
  31. package/lib/sre/index.js.map +1 -1
  32. package/lib/sre/sre-browser.d.ts +16 -0
  33. package/lib/sre/sre-browser.js +26 -1
  34. package/lib/sre/sre-browser.js.map +1 -1
  35. package/lib/styles/index.js +1 -1
  36. package/lib/styles/index.js.map +1 -1
  37. package/package.json +2 -2
  38. package/pr-specs/2026-01-html-math-output-options.md +529 -0
  39. package/pr-specs/2026-02-formd-delegate-image-rendering-in-table-cells.md +126 -0
@@ -0,0 +1,126 @@
1
+ # PR: Delegate image rendering in table cells when `forMD` is set
2
+
3
+ Status: Active
4
+ Owner: @OlgaRedozubova
5
+
6
+ ---
7
+
8
+ ## Context
9
+
10
+ `renderTableCellContent()` hardcodes `![alt](src)` markdown for `image` and `includegraphics` tokens, bypassing any custom render rules the caller has registered on the markdown-it renderer. The function already calls `slf.renderInline([child])` for every child token and stores the result in `rendered`, but for images this result is discarded.
11
+
12
+ This means callers that register custom image render rules (e.g., to resolve CDN URLs to local paths, apply sizing, or add custom formatting) have no way to control how images inside table cells are rendered in the markdown output.
13
+
14
+ ## Goal
15
+
16
+ When `options.forMD` is true, `renderTableCellContent` should use the renderer's output for `image`/`includegraphics` tokens instead of hardcoding `![alt](src)`. This allows callers (like mmd-converter) to resolve image paths, apply custom formatting, and handle pipe escaping through their own render rules.
17
+
18
+ ## Non-Goals
19
+
20
+ - Changing HTML, TSV, CSV, or DOCX/PPTX rendering of table images
21
+ - Changing behavior when `forMD` is not set (default path is unchanged)
22
+ - Adding image resolution logic to mathpix-markdown-it itself (that stays in mmd-converter)
23
+
24
+ ## Current Behavior
25
+
26
+ In `renderTableCellContent()`, every child token is rendered via `slf.renderInline([child])` and the result is stored in `rendered`. However, for `image`/`includegraphics` tokens, the `mdCell` output ignores `rendered` and constructs markdown manually:
27
+
28
+ ```typescript
29
+ case 'image':
30
+ case 'includegraphics': {
31
+ const src = child.attrGet('src');
32
+ mdCell += `![${child.attrGet('alt')}](${src})`.replace(/\|/g, '\\|');
33
+ continue;
34
+ }
35
+ ```
36
+
37
+ Custom render rules registered on the renderer instance are never used for table cell markdown.
38
+
39
+ ## Desired Behavior
40
+
41
+ When `options.forMD` is true:
42
+
43
+ 1. Set `child.meta.isTableCell = true` before calling `slf.renderInline([child])` — this lets render rules know the token is inside a table cell (e.g., to escape pipe characters in alt text).
44
+
45
+ 2. For `image`/`includegraphics` tokens, use the `rendered` output (from `slf.renderInline`) instead of hardcoded markdown:
46
+
47
+ ```typescript
48
+ case 'image':
49
+ case 'includegraphics': {
50
+ const src = child.attrGet('src');
51
+ tsvCell += src;
52
+ csvCell += src;
53
+ mdCell += options?.forMD
54
+ ? rendered
55
+ : `![${child.attrGet('alt') ?? ''}](${src})`.replace(/\|/g, '\\|');
56
+ continue;
57
+ }
58
+ ```
59
+
60
+ When `forMD` is false/undefined, behavior is identical to before.
61
+
62
+ ## Files Changed
63
+
64
+ | File | Change |
65
+ |------|--------|
66
+ | `src/markdown/common/render-table-cell-content.ts` | Add `isTableCell` meta tagging when `forMD` is set; use `rendered` for image tokens when `forMD` is set |
67
+ | `tests/_data/_table-markdown/_data.js` | Add test case (id: 50) for `\includegraphics` inside tabular |
68
+
69
+ ## Why This Approach
70
+
71
+ ### Why use the existing `rendered` variable instead of a new code path?
72
+
73
+ `renderTableCellContent` already calls `slf.renderInline([child])` for every child token and stores the result in `rendered`. For most token types, `rendered` is used for the `content` (HTML) output. For images, it was being discarded in favor of hardcoded markdown. Using `rendered` for `mdCell` when `forMD` is set is the minimal change — no new rendering logic, just using what's already computed.
74
+
75
+ ### Why gate on `forMD` instead of always delegating?
76
+
77
+ The default (`forMD: false`) path produces standard `![alt](src)` markdown that works for all existing consumers (HTML rendering, DOCX, PPTX). Changing the default would risk regressions in those paths. `forMD` is only set by mmd-converter's markdown export pipeline, which is exactly where custom render rules need to participate.
78
+
79
+ ### Why set `isTableCell` on token meta?
80
+
81
+ Render rules need to know when a token is inside a table cell so they can escape pipe characters (`|` → `\|`) in alt text. Without this, a pipe in alt text would break the markdown table structure. The meta flag is a clean way to communicate this without adding parameters to the render rule signature.
82
+
83
+ ## Constraints
84
+
85
+ - Default behavior (when `forMD` is not set) must not change
86
+ - TSV and CSV output must not change (they still use raw `src`)
87
+ - HTML output (`content`) must not change (it already uses `rendered`)
88
+ - DOCX/PPTX output (`tableSmoothed`) must not change
89
+ - All existing tests must pass
90
+
91
+ ## Testing
92
+
93
+ New test case (id: 50) in `_table-markdown/_data.js`:
94
+
95
+ ```javascript
96
+ {
97
+ id: 50,
98
+ latex: '\\begin{tabular}{|l|l|}\\n\\hline\\n\\textbf{Case} & \\textbf{Image} \\\\\\n\\hline\\n' +
99
+ 'LaTeX image without options (should be resolved) &\\n' +
100
+ '\\includegraphics{https://mathpix-ocr-examples.s3.amazonaws.com/cases_printed_1.jpg}\\\\\\n' +
101
+ '\\hline\\n\\end{tabular}',
102
+ table_markdown: '| **Case** | **Image** |\\n| :--- | :--- |\\n' +
103
+ '| LaTeX image without options (should be resolved) | ' +
104
+ '![](https://mathpix-ocr-examples.s3.amazonaws.com/cases_printed_1.jpg) |'
105
+ }
106
+ ```
107
+
108
+ Note: this test verifies the default (non-`forMD`) behavior produces standard markdown. The `forMD` rendering is tested in mmd-converter's `06-tabular-with-images` test case, which exercises the full pipeline with `renderImageRule` and `renderIncludeGraphics`.
109
+
110
+ ```bash
111
+ npm run build
112
+ npm test
113
+ ```
114
+
115
+ ## Risk / Rollback
116
+
117
+ **Risk**: Low
118
+ - Gated behind `forMD` option — only mmd-converter's markdown export path is affected
119
+ - Default rendering path is unchanged
120
+ - Two-line change in the source file
121
+
122
+ **Rollback**: Revert PR or pin to previous version
123
+
124
+ ## Related
125
+
126
+ - Issue: [#18320](https://github.com/Mathpix/monorepo/pull/18320)