mathpix-markdown-it 2.0.35 → 2.0.37

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 (61) hide show
  1. package/README.md +146 -4
  2. package/doc/changelog.md +64 -0
  3. package/es5/browser/add-speech.js +1 -0
  4. package/es5/browser/auto-render.js +33 -0
  5. package/es5/bundle.js +5 -5
  6. package/es5/index.js +5 -5
  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/contex-menu/styles.js +2 -1
  17. package/lib/contex-menu/styles.js.map +1 -1
  18. package/lib/copy-to-clipboard/clipboard-copy-styles.js +2 -1
  19. package/lib/copy-to-clipboard/clipboard-copy-styles.js.map +1 -1
  20. package/lib/markdown/common/convert-math-to-html.d.ts +4 -1
  21. package/lib/markdown/common/convert-math-to-html.js +174 -93
  22. package/lib/markdown/common/convert-math-to-html.js.map +1 -1
  23. package/lib/markdown/highlight/highlight-math-token.js +5 -3
  24. package/lib/markdown/highlight/highlight-math-token.js.map +1 -1
  25. package/lib/mathjax/index.d.ts +6 -0
  26. package/lib/mathjax/index.js +103 -33
  27. package/lib/mathjax/index.js.map +1 -1
  28. package/lib/mathpix-markdown-model/index.d.ts +35 -5
  29. package/lib/mathpix-markdown-model/index.js +106 -50
  30. package/lib/mathpix-markdown-model/index.js.map +1 -1
  31. package/lib/sre/index.d.ts +8 -0
  32. package/lib/sre/index.js +38 -20
  33. package/lib/sre/index.js.map +1 -1
  34. package/lib/sre/sre-browser.d.ts +16 -0
  35. package/lib/sre/sre-browser.js +26 -1
  36. package/lib/sre/sre-browser.js.map +1 -1
  37. package/lib/styles/colors.d.ts +63 -0
  38. package/lib/styles/colors.js +68 -0
  39. package/lib/styles/colors.js.map +1 -0
  40. package/lib/styles/helpers.js +22 -0
  41. package/lib/styles/helpers.js.map +1 -0
  42. package/lib/styles/index.d.ts +4 -3
  43. package/lib/styles/index.js +45 -7
  44. package/lib/styles/index.js.map +1 -1
  45. package/lib/styles/styles-code.d.ts +1 -1
  46. package/lib/styles/styles-code.js +6 -1
  47. package/lib/styles/styles-code.js.map +1 -1
  48. package/lib/styles/styles-container.js +2 -1
  49. package/lib/styles/styles-container.js.map +1 -1
  50. package/lib/styles/styles-lists.d.ts +1 -1
  51. package/lib/styles/styles-lists.js +1 -1
  52. package/lib/styles/styles-lists.js.map +1 -1
  53. package/lib/styles/styles-tabular.d.ts +1 -1
  54. package/lib/styles/styles-tabular.js +5 -2
  55. package/lib/styles/styles-tabular.js.map +1 -1
  56. package/package.json +2 -2
  57. package/pr-specs/2026-01-html-math-output-options.md +529 -0
  58. package/pr-specs/2026-03-mmd-css-scoping.md +267 -0
  59. package/lib/styles/halpers.js +0 -13
  60. package/lib/styles/halpers.js.map +0 -1
  61. /package/lib/styles/{halpers.d.ts → helpers.d.ts} +0 -0
package/README.md CHANGED
@@ -691,6 +691,127 @@ import {
691
691
  <img width="370" alt="Screen Shot 2022-05-03 at 17 21 50" src="https://user-images.githubusercontent.com/32493105/166471623-fd3f6a5b-84e4-4d43-afcd-0384e83eb2df.png">
692
692
 
693
693
 
694
+ ## Output Format
695
+
696
+ The `output_format` option in `TOutputMath` controls which math format is placed in the HTML output. This is useful for optimizing file size or delegating rendering to the client.
697
+
698
+ | Value | Description |
699
+ |-------|-------------|
700
+ | `'svg'` (default) | Pre-rendered SVG with hidden format elements. Works offline, no client-side rendering needed. |
701
+ | `'mathml'` | Native `<math>` elements only. Smaller file size, requires client-side rendering via `auto-render.js`. |
702
+ | `'latex'` | Raw LaTeX with original delimiters. Smaller file size, requires client-side rendering via `auto-render.js`. |
703
+
704
+ ### Example usage
705
+
706
+ ```js
707
+ const options = {
708
+ outMath: {
709
+ output_format: 'mathml', // or 'latex'
710
+ include_mathml: true,
711
+ include_latex: true,
712
+ }
713
+ };
714
+ const html = MathpixMarkdownModel.markdownToHTML('$x^2$', options);
715
+ ```
716
+
717
+ When using `'mathml'` or `'latex'`, the server outputs minimal HTML containing only the raw format. Use the browser rendering script (`auto-render.js`) to transform this into the full structure with SVG and hidden formats.
718
+
719
+
720
+ ## Browser Rendering Script (auto-render.js)
721
+
722
+ For `output_format: 'mathml'` or `output_format: 'latex'`, use the browser bundle to render math on the client side.
723
+
724
+ ### Loading the script
725
+
726
+ ```html
727
+ <script src="https://cdn.jsdelivr.net/npm/mathpix-markdown-it@latest/es5/browser/auto-render.js"></script>
728
+ ```
729
+
730
+ ### Usage
731
+
732
+ ```js
733
+ // Render all math elements in a container
734
+ window.MathpixRender.renderMathInElement(document.getElementById('content'), {
735
+ outMath: {
736
+ output_format: 'svg',
737
+ include_svg: true,
738
+ include_mathml: true,
739
+ include_latex: true,
740
+ include_asciimath: true,
741
+ },
742
+ accessibility: {
743
+ assistive_mml: true,
744
+ include_speech: true,
745
+ }
746
+ });
747
+ ```
748
+
749
+ ### Configuration options
750
+
751
+ | Option | Type | Default | Description |
752
+ |--------|------|---------|-------------|
753
+ | `outMath` | `TOutputMath` | `{ output_format: 'svg', include_svg: true }` | Controls which hidden formats to generate. Set `include_mathml`, `include_latex`, `include_asciimath`, etc. to `true` to generate hidden format elements for context menu compatibility. |
754
+ | `accessibility.assistive_mml` | `boolean` | `true` | Add `<mjx-assistive-mml>` element for screen readers |
755
+ | `accessibility.include_speech` | `boolean` | `false` | Add `aria-label` with speech text (requires `assistive_mml: true`) |
756
+
757
+ ### Accessibility behavior
758
+
759
+ The browser bundle uses a simplified accessibility configuration (different from the server-side [TAccessibility](#taccessibility) interface):
760
+
761
+ | Configuration | Result |
762
+ |---------------|--------|
763
+ | `{ assistive_mml: true, include_speech: true }` | `aria-label` attribute with speech text + `<mjx-assistive-mml>` element |
764
+ | `{ assistive_mml: true }` | `aria-labelledby` pointing to `<mjx-assistive-mml>` ID |
765
+ | No accessibility config | No accessibility attributes added |
766
+
767
+ The script automatically skips elements that have already been rendered.
768
+
769
+ **Note:** For server-side rendering accessibility, use the [TAccessibility](#taccessibility) interface with `assistiveMml` and `sre` options instead.
770
+
771
+
772
+ ## Browser Speech Script (add-speech.js)
773
+
774
+ Use this script to add speech accessibility to math that was already rendered server-side as SVG with `assistiveMml: true` but without SRE speech. It loads SRE dynamically and adds `aria-label` attributes to existing `mjx-container` elements.
775
+
776
+ This is useful when you want fast server-side rendering without the SRE dependency, but still want speech accessibility on the client.
777
+
778
+ ### Prerequisites
779
+
780
+ The rendered HTML must include `<mjx-assistive-mml>` elements (i.e., the server used `accessibility: { assistiveMml: true }`).
781
+
782
+ ### Loading the script
783
+
784
+ ```html
785
+ <script src="https://cdn.jsdelivr.net/npm/mathpix-markdown-it@latest/es5/browser/add-speech.js"></script>
786
+ ```
787
+
788
+ By default, the script automatically processes `document.body` on `DOMContentLoaded`.
789
+
790
+ ### Usage
791
+
792
+ ```js
793
+ // Add speech to all rendered math in a specific container
794
+ window.MathpixSpeech.addSpeechToRenderedMath(document.getElementById('content'));
795
+ ```
796
+
797
+ ### Configuration
798
+
799
+ Set `window.MathpixSpeechConfig` before the script loads to customize behavior:
800
+
801
+ ```js
802
+ window.MathpixSpeechConfig = {
803
+ container: document.getElementById('math-content') // defaults to document.body
804
+ };
805
+ ```
806
+
807
+ ### What it does
808
+
809
+ For each `mjx-container` element that does not already have an `aria-label`:
810
+ 1. Extracts MathML from the `<mjx-assistive-mml>` child element
811
+ 2. Generates speech text using SRE (Speech Rule Engine)
812
+ 3. Sets `aria-label`, `role="math"`, and `tabindex="0"` on the container
813
+
814
+
694
815
  # Documentation
695
816
 
696
817
  ## React components
@@ -733,10 +854,12 @@ The `MathpixMarkdown` React element accepts the following props:
733
854
  | | returns | description | |
734
855
  |----------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------|---------|
735
856
  | **Style methods:** | | | |
857
+ | buildStyles(opts?: [StyleBundleOpts](https://github.com/Mathpix/mathpix-markdown-it#stylebundleopts)) | string | Single CSS builder. All style assembly methods delegate here. Modules toggled via opts. | |
736
858
  | loadMathJax() | boolean | Adds a style element into the head of the document and returns true. In case of an error, returns false. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-markdownToHTML-method)|
737
- | getMathpixStyleOnly() | string | returns styles as a string. | |
738
- | getMathpixStyle(true) | string | returns styles as a string including styles for container. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-render-method)|
739
- | getMathpixFontsStyle() | boolean | returns fonts styles as a string. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-render-method)|
859
+ | getMathpixStyleOnly(useColors?) | string | Styles for embedded widget (no container/preview). | |
860
+ | getMathpixStyle(stylePreview?, showToc?, tocContainerName?, useColors?, isPptx?) | string | Full page styles including container. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-render-method)|
861
+ | getMathpixFontsStyle() | string | Returns fonts styles as a string. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-render-method)|
862
+ | getMaxWidthStyle(maxWidth?, isHideScroll?) | string | Returns CSS for math/tabular max-width constraints and optional scrollbar hiding. | |
740
863
  | **Render methods:** | | | |
741
864
  | markdownToHTML(str, options: [TMarkdownItOptions](https://github.com/Mathpix/mathpix-markdown-it#tmarkdownitoptions)) | string | Renders input text to html element as a string. |[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-markdownToHTML-method)|
742
865
  | render(str, options: [optionsMathpixMarkdown](https://github.com/Mathpix/mathpix-markdown-it#optionsmathpixmarkdown)) | string | Renders input text to HTML element as a string and wraps it in a container. Should be used to render the entire document.|[example](https://github.com/Mathpix/mathpix-markdown-it/tree/master/examples/react-app/use-render-method)|
@@ -746,6 +869,24 @@ The `MathpixMarkdown` React element accepts the following props:
746
869
 
747
870
 
748
871
 
872
+ ### StyleBundleOpts
873
+
874
+ | | type&nbsp;*`default`* | description |
875
+ |--------------------------|-------------------------------|------------------------------------------------------------|
876
+ | `setTextAlignJustify` | boolean&nbsp;*`false`* | Enables `text-align: justify` on content blocks |
877
+ | `useColors` | boolean&nbsp;*`true`* | When `false`, omits color declarations from CSS output |
878
+ | `maxWidth` | string&nbsp;*`''`* | Sets max-width for math/tabular containers (e.g. `'800px'`)|
879
+ | `isPptx` | boolean&nbsp;*`false`* | Adjusts layout for PowerPoint export |
880
+ | `resetBody` | boolean&nbsp;*`false`* | Includes body margin/line-height reset |
881
+ | `container` | boolean&nbsp;*`false`* | Includes base element styles (headings, links, tables etc) |
882
+ | `mathjax` | boolean&nbsp;*`false`* | Includes MathJax stylesheet |
883
+ | `code` | boolean&nbsp;*`true`* | Includes syntax-highlighting styles |
884
+ | `preview` | boolean&nbsp;*`false`* | Includes `#preview` wrapper styles |
885
+ | `toc` | boolean&nbsp;*`false`* | Includes table-of-contents styles |
886
+ | `tocContainerName` | string&nbsp;*`'toc'`* | ID of the TOC container element |
887
+ | `menu` | boolean&nbsp;*`false`* | Includes context-menu and clipboard styles |
888
+
889
+
749
890
  ### TMarkdownItOptions
750
891
 
751
892
  | | type&nbsp;*`default`* | description |
@@ -818,6 +959,7 @@ The `MathpixMarkdown` React element accepts the following props:
818
959
 
819
960
  | | type&nbsp;*`default`* | description |
820
961
  |--------------------------|------------------------------|-------------------------------------------------------------------------------------------------------------------|
962
+ | `output_format` | `'svg' \| 'mathml' \| 'latex'`&nbsp;*`'svg'`* | Controls which math format is placed in HTML output. See [Output Format](#output-format) section. |
821
963
  | `include_mathml` | boolean&nbsp;*`false`* | outputs mathml `<mathml style="display: none"><math>...</math></mathml>` |
822
964
  | `include_mathml_word` | boolean&nbsp;*`false`* | outputs mathml_word `<mathmlword style="display: none"><math>...</math></mathmlword>` |
823
965
  | `include_asciimath` | boolean&nbsp;*`false`* | outputs asciimath `<asciimath style="display: none">...</asciimath>` |
@@ -964,7 +1106,7 @@ const { loadSreAsync } = require('mathpix-markdown-it/lib/sre/sre-node');
964
1106
 
965
1107
  ```
966
1108
 
967
- Then just pass the resulting value to `accessibility.spe`
1109
+ Then just pass the resulting value to `accessibility.sre`
968
1110
  ```js
969
1111
  accessibility: {
970
1112
  assistiveMml: true, // assistive-mml will be added to mjx-container
package/doc/changelog.md CHANGED
@@ -1,5 +1,69 @@
1
+ # March 2026
2
+
3
+ ## [2.0.37] - CSS scoping and style module cleanup
4
+
5
+ - CSS Scoping:
6
+ - All MMD class selectors now have `#preview-content`/`#setText` scoped variants for specificity boost.
7
+ - Bare selectors preserved as fallback for `markdownToHTML()` (no wrapper).
8
+
9
+ - Style Architecture:
10
+ - New `buildStyles(opts: StyleBundleOpts)` single CSS builder — all assembly methods delegate here.
11
+ - `MathpixStyle` restructured into 10 composable sub-functions.
12
+ - Color constants extracted into `src/styles/colors.ts`.
13
+ - `halpers.ts` renamed to `helpers.ts`.
14
+
15
+ - Improvements:
16
+ - `.tabular` now renders consistently regardless of context (standalone vs nested inside a list). Previously, list context could affect table width and font size via cascade. Fixed with explicit `margin: 0 0 1em`, `font-size: inherit`, and other defensive defaults.
17
+ - `useColors=false` now correctly omits blockquote border, table border, and mark background colors.
18
+ - `getMathpixStyle(useColors=false)` now also omits `ContainerStyle` colors (body text, headings, links, captions). Previously `ContainerStyle()` was always called with default colors.
19
+
20
+ - Bug Fixes:
21
+ - `div.svg-container` child combinator consistency (`>` for both `#preview-content` and `#setText`).
22
+ - `loadMathJax` updates existing `#Mathpix-styles` element instead of skipping.
23
+
24
+ - Breaking Changes:
25
+ - `scaleEquation` parameter removed from `loadMathJax`, `getMathpixStyleOnly`, `getMathpixStyle`, and `getMathpixMarkdownStyles`. It was never used in CSS output. If you were passing it positionally, shift your arguments. Use `buildStyles(opts)` for a named-parameter alternative.
26
+
27
+ - Dead Code Removed:
28
+ - `.empty` selector (never generated), `.preview-right` selector (used as id, not class).
29
+
30
+ - Docs:
31
+ - Added implementation details in `pr-specs/2026-03-mmd-css-scoping.md`.
32
+
1
33
  # February 2026
2
34
 
35
+ ## [2.0.36] - 16 February 2026
36
+
37
+ - Math Output Format:
38
+ - Added `output_format` option to `TOutputMath` to control which math format is placed in HTML output.
39
+ - `'svg'` (default): Pre-rendered SVG with hidden formats, works offline.
40
+ - `'mathml'`: Native `<math>` elements only, smaller file size, requires client-side rendering.
41
+ - `'latex'`: Raw LaTeX with original delimiters, smaller file size, requires client-side rendering.
42
+
43
+ - Browser Rendering Script (`auto-render.js`):
44
+ - New browser bundle for client-side math rendering at `es5/browser/auto-render.js`.
45
+ - Renders MathML or LaTeX content to SVG.
46
+ - Generates hidden format elements for context menu compatibility.
47
+ - Configurable accessibility support via `MathpixAccessibilityConfig`:
48
+ - `assistive_mml`: Add `<mjx-assistive-mml>` for screen readers.
49
+ - `include_speech`: Add `aria-label` with speech text.
50
+
51
+ - Browser Speech Script (`add-speech.js`):
52
+ - New browser bundle for adding speech to already-rendered SVG at `es5/browser/add-speech.js`.
53
+ - Use when SVG was rendered with `assistiveMml: true` but without `sre` (speech).
54
+ - Loads SRE dynamically and adds `aria-label`, `role="math"`, `tabindex` to `mjx-container` elements.
55
+ - Requires `mjx-assistive-mml` to be present in the rendered output.
56
+ - Exposes `window.MathpixSpeech.addSpeechToRenderedMath(container?)`.
57
+
58
+ - Accessibility:
59
+ - `mjx-assistive-mml` is no longer marked with `aria-hidden="true"` when accessibility options are enabled. Previously, the assistive MathML element was hidden from screen readers even when the user explicitly requested accessibility via `assistiveMml: true` or `sre`. Now, if any accessibility option is set, the MathML content is exposed to assistive technology — either via `aria-labelledby` (pointing to the assistive MML) or via `aria-label` (SRE speech text). This affects both server-side rendering (`addAriaToMathHTML`) and the new browser bundles.
60
+
61
+ - Fixes:
62
+ - Fixed centering issue for equations with numbering inside `.math-block[data-width="full"]`.
63
+
64
+ - Docs:
65
+ - Added implementation details in `pr-specs/2026-01-html-math-output-options.md`.
66
+
3
67
  ## [2.0.35] - 13 February 2026
4
68
 
5
69
  - Tabular: