diff --git a/README.md b/README.md index b057e26..28618c5 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ Also online at [p10node.github.io/office-engine](https://p10node.github.io/offic - **Microsoft Visio (view only)**: `.vsdx` drawings (also `.vsdm`, templates and stencils) open read-only, a slide per page: shapes with their geometry (lines, arcs, ellipses, Béziers, NURBS), masters, groups, connectors with line ends, fills, gradients, themes, text, pictures, background pages and layers; *Download as* PowerPoint saves the converted file - **CSV, text, Markdown, HTML, SVG**: `.csv` / `.tsv` open as a workbook (streamed: a million rows in about 2 s; past 12 million cells the rows are read from the file as they are shown, so there is no row limit), `.txt` / `.md` / `.html` as a document, `.svg` / `.svgz` as a picture (`.gz` files are unpacked first), from any `src` (file, Blob, URL); format from the extension, MIME type or content; UTF-8 / UTF-16 / Windows-1252; editable. Markdown, HTML and SVG are saved back in their own format (CSV and text as `.xlsx` / `.docx`) - **Diagrams as code**: `.mmd` (Mermaid), `.puml` (PlantUML), `.dot` / `.gv` (Graphviz) open as a document with the drawing; ` ```mermaid ` / ` ```plantuml ` / ` ```dot ` blocks in Markdown and `
` in HTML are drawn too. Drawn by a built-in layout engine in the worker: no server, no network, no dependency
+- **Math in Markdown**: `$…$` and `$$…$$` TeX formulas are drawn as equations (the browser's own MathML: selectable, read by screen readers, printed); the document stays editable around them and saves each formula back exactly as it was written. Currency (`$5 and $10`), `\$` and code are left alone; a formula that cannot be read shows its source. *Download as* Word gives real Word equations
 - **Code side by side**: Markdown, HTML, SVG and diagram files show their source next to the document (**Code** button); edit it and the document is rebuilt as you type; edit the document and the Markdown / HTML is built again from it
 - **File menu**: new, open (also DOC / XLS / PPT / RTF / OpenDocument / Pages / Keynote / Numbers / Visio / CSV / text / Markdown / HTML / SVG / diagrams), import CSV, rename, download as PDF / HTML / text / CSV, document details, print
 - **Comments**: threads with replies, resolve, edit / delete, @mentions; on DOCX text, XLSX cells and PPTX slides, stored in the file the way Word, Excel and PowerPoint store them
@@ -104,7 +105,7 @@ The renderers also ship on their own, to show diagrams and Markdown in any page
 | [`@p10node/mermaid`](packages/mermaid)   | Mermaid → SVG                                                       | 26 KB       |
 | [`@p10node/plantuml`](packages/plantuml) | PlantUML → SVG (no Java)                                            | 29 KB       |
 | [`@p10node/graphviz`](packages/graphviz) | Graphviz DOT → SVG (no WebAssembly)                                 | 16 KB       |
-| [`@p10node/markdown`](packages/markdown) | Markdown → safe HTML, diagram code blocks drawn                     | 39 KB       |
+| [`@p10node/markdown`](packages/markdown) | Markdown → safe HTML, diagram code blocks drawn, TeX math as MathML | 53 KB       |
 
 ```ts
 import { render } from '@p10node/mermaid'
diff --git a/docs/api.md b/docs/api.md
index 6f8f656..eba2e78 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -252,7 +252,7 @@ const { svg, width, height } = render('sequenceDiagram\n  Ann->>Ben: hi') // lan
 renderPage() // 
, … of the page become drawings
 
 import { render as markdown } from '@p10node/markdown'
-el.innerHTML = markdown(text) // safe HTML; ```mermaid / ```plantuml / ```dot drawn
+el.innerHTML = markdown(text) // safe HTML; ```mermaid / ```plantuml / ```dot drawn, $…$ / $$…$$ math as MathML
 ```
 
 ## CSP
diff --git a/docs/development.md b/docs/development.md
index b20388e..db6c19f 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -34,6 +34,7 @@ src/core     parsers, DOM-free (runs in worker / Bun)
   legacy     .doc / .xls / .ppt (compound file reader + converters), .rtf, OpenDocument → DOCX / XLSX / PPTX
   iwork      Pages / Keynote / Numbers → DOCX / PPTX / XLSX
   visio      Visio drawings (.vsdx) → PPTX
+  math       TeX math: `$…$` / `$$…$$` in Markdown, TeX → MathML (views, web pages) and → OMML (Word file)
 src/react    view components
 src/embed.ts mount() + 
 src/collab   real-time collaboration (hub, session, per-format adapters)
diff --git a/docs/features.md b/docs/features.md
index c7ca46b..24d9e9a 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -130,7 +130,7 @@ A `src` (file, Blob, buffer or URL) that is not an Office package opens too, con
 |-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | `.csv`, `.tsv` / `.tab`       | workbook, one sheet named after the file; the rows stream in as the file is read (a million rows open like a large workbook; past 12 million cells the rows are read from the file by pages as they are shown); numbers and booleans typed, codes like `007` kept as text |
 | `.txt`, `.text`, `.log`       | document, one paragraph per line                                                                                                                                                                                                                                          |
-| `.md`, `.markdown`            | document: headings, lists, bold / italic / strike, links, code, quotes, tables (as tab-separated lines)                                                                                                                                                                   |
+| `.md`, `.markdown`            | document: headings, lists, bold / italic / strike, links, code, quotes, tables (as tab-separated lines), TeX math (`$…$`, `$$…$$`) as equations (see [Math in Markdown](#math-in-markdown))                                                                               |
 | `.html`, `.htm`, `.xhtml`     | document: headings, paragraphs, lists, inline formatting and links; scripts and styles are dropped                                                                                                                                                                        |
 | `.svg`                        | the picture alone (no page), scaled down to fit the view, at its own size (`width` / `height` in any unit, else the `viewBox`) otherwise; the file is kept as it is, given the SVG namespace when it lacks one; *Download as* Word gives a document holding the picture   |
 | `.svgz`, `.csv.gz`, `.md.gz`… | the file inside: a gzip-compressed plain file is unpacked whole in the worker (native `DecompressionStream`), the name inside telling the format                                                                                                                          |
@@ -144,7 +144,28 @@ A `src` (file, Blob, buffer or URL) that is not an Office package opens too, con
 - **Encoding**: the `encoding` open parameter when given (a `TextDecoder` label, e.g. `windows-1258`), else the byte order mark (UTF-8, UTF-16), the declared `charset`, UTF-8, else Windows-1258 when the bytes read as Vietnamese in it (tone marks after vowels: Excel's CSV on Vietnamese Windows; the text is put in NFC), else Windows-1252 (Excel's CSV on other Windows)
 - **Columns**: a sheet holds 16,384 columns (up to XFD); the columns of a wider CSV after it are not loaded and the viewer says so (a message; `warning` event from the worker)
 - The bar shows a `CSV` / `TSV` / `TXT` / `MD` / `HTML` / `SVG` badge
-- **Saving**: a Markdown, HTML or SVG file is saved **as itself** (`.md` / `.html` / `.svg`, also what `onSave` receives). Markdown and HTML are built again from the document when it was edited there: headings, lists (nested), bold / italic / underline / strike, links, code, colors, tables, pictures (embedded as data URLs; a diagram block becomes its drawing); the text reads with its tracked changes accepted. An unedited file is saved as it is. CSV / TSV and text are saved as a workbook / document (`.xlsx` / `.docx`); *Download as* Word / CSV / text / Markdown gives the other formats
+- **Saving**: a Markdown, HTML or SVG file is saved **as itself** (`.md` / `.html` / `.svg`, also what `onSave` receives). Markdown and HTML are built again from the document when it was edited there: headings, lists (nested), bold / italic / underline / strike, links, code, colors, tables, pictures (embedded as data URLs; a diagram block becomes its drawing), TeX formulas as they were written; the text reads with its tracked changes accepted. An unedited file is saved as it is. CSV / TSV and text are saved as a workbook / document (`.xlsx` / `.docx`); *Download as* Word / CSV / text / Markdown gives the other formats
+
+### Math in Markdown
+
+TeX formulas in a Markdown file are drawn as equations, by the browser from MathML (no font or script is loaded): selectable, read by screen readers, printed and exported like the text around them. The converter is the library's own (TeX → MathML, in the worker and the view), with no dependency.
+
+| Written                 | Becomes                                                                                                                                    |
+|-------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
+| `$…$`                   | an inline formula, when a non-space follows the opening `$` and the closing `$` has a non-space before it and no digit after it (pandoc)   |
+| `$$…$$`                 | a display formula (centered, on its own line); it may span lines, but not a blank one                                                      |
+| `\$`                    | a dollar sign; `It costs $5 and $10` has no formula either                                                                                 |
+| `` `$x$` ``, code block | code: nothing inside a code span, a fenced or an indented code block is math, and a `$` whose formula would end inside a code span is text |
+| `\(…\)`, `\[…\]`        | not math: in Markdown these are escaped brackets                                                                                           |
+
+- **TeX read**: Greek letters and the usual symbols, operators, relations, arrows and dots; sub / superscripts and primes; big operators (`\sum`, `\prod`, `\int`, `\oint`, `\bigcup`…) with their limits under / over them in display style and beside them inline (`\limits`, `\nolimits`); `\frac`, `\dfrac`, `\tfrac`, `\binom`, `\over`, `\choose`; `\sqrt`, `\sqrt[n]`; `\left … \middle … \right` and `\big` … `\Bigg`; accents (`\hat`, `\bar`, `\vec`, `\tilde`, `\dot`, `\ddot`, `\widehat`, `\overline`, `\underline`, `\overbrace`, `\underbrace`, `\overset`, `\underset`, `\xrightarrow`…); `\text` (with `$…$` inside), `\mathrm`, `\mathbf`, `\mathit`, `\mathbb`, `\mathcal`, `\mathfrak`, `\mathsf`, `\mathtt`, `\boldsymbol` (as the Unicode math alphabets: 𝐱, ℝ, 𝔤…), `\rm`, `\bf`…; `\operatorname`, the named functions (`\sin`, `\log`, `\lim`, `\max`…); spaces (`\,`, `\quad`, `\hspace`…); `\color`, `\textcolor`; `\not`; `\displaystyle`…; `\phantom`, `\boxed`, `\pmod`, `\tag`; the environments `matrix`, `pmatrix`, `bmatrix`, `Bmatrix`, `vmatrix`, `Vmatrix`, `smallmatrix`, `cases`, `dcases`, `aligned`, `align`, `alignat`, `gathered`, `gather`, `split`, `array`, `subarray`, `equation`, `eqnarray`, `\substack`, and `\\` lines at the top of a display formula
+- **Falls back**: a command it does not know (`\newcommand`, `\href`, `\ce`…) shows as written, in red monospace, and the rest of the formula is drawn; a formula it cannot read (an unclosed brace, `\left` without `\right`, an unknown environment, a double superscript, more than 20,000 characters or groups nested more than 40 deep) shows its whole source that way, the reason as its tooltip. Nothing else of the page changes, and the source is kept as it was
+- **Editing**: a formula is one object of the text, like a picture: the caret steps over it, Delete removes it, Undo brings it back, and copy / paste keeps it a formula (the clipboard gets MathML holding the TeX, and the TeX in the plain text; MathML with a TeX annotation from other pages, KaTeX's for one, is pasted as a formula too). The formula itself is edited in the **Code** pane (no visual equation editor yet)
+- **Saving**: the document keeps each formula's TeX and delimiters, so the Markdown written for an edited paragraph has the formula exactly as it was (spaces and line breaks of a `$$` block included); the rest of the file is kept as it is. In text written again, a `$` is escaped only when it would otherwise be read as math
+- **Word file** (*Download as* Word document): each formula is a real Office Math equation (OMML: Word shows and edits it; display formulas are equation paragraphs), carrying its TeX in two attributes of the library's own namespace, which the file declares ignorable so Word reads it and drops them on save. Opened here again, the equations come back as the same formulas. A color with a name Word does not know is left out of the Word equation, and a formula that cannot be read is its source as text there
+- **Exports**: *Download as* web page and print / PDF show the equations (MathML, the TeX as an annotation); plain text and Markdown give the TeX between its delimiters
+- **Not yet**: `\(…\)` / `\[…\]` and GitHub's ` ```math ` blocks and `` $`…`$ `` (shown as written), macros (`\newcommand`, `\def`), `\href`, chemistry (`\ce`); a `$$` block over several lines inside a list item or a quote (put it on one line there); equations of Word files (OMML) are still shown as their linear text, not drawn
+- **Browsers**: Chromium, Firefox and Safari draw MathML. Chromium follows MathML Core: table column alignment comes from the viewer's style sheet, `\boxed` / `\cancel` draw no box or line, `\textbf` inside `\text` is not bold. The look of the letters is the system's math font (Cambria Math on Windows, STIX Two Math on macOS)
 
 ### Diagrams: Mermaid, PlantUML, Graphviz
 
diff --git a/e2e/markdown-math.spec.ts b/e2e/markdown-math.spec.ts
new file mode 100644
index 0000000..a62afa2
--- /dev/null
+++ b/e2e/markdown-math.spec.ts
@@ -0,0 +1,203 @@
+/** TeX math in Markdown (#33): drawn as MathML, edited around as an object, saved back as written, printed and exported */
+import { describe, expect, test } from 'bun:test'
+import { docxParas, useBrowser, viewer, type Viewer } from './harness'
+
+useBrowser()
+
+const MD = `# Notes
+
+Euler’s identity: $e^{i\\pi} + 1 = 0$ is famous.
+
+$$
+\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}
+$$
+
+It costs $5 and \\$10, see \`$x$\`.
+
+Broken: $\\frac{a}{$ and $\\foo x$ end.
+`
+
+/** opens `md` as notes.md through File → Open (a viewer that can edit and save) */
+async function openMd(md = MD): Promise {
+  const v = await viewer({ src: 'samples/sample.xlsx', onSave: true, editable: true })
+  await v.ctx.grantPermissions(['clipboard-read', 'clipboard-write'])
+  await v.chooseFile(() => v.menu('Open…'), { name: 'notes.md', mimeType: 'text/markdown', buffer: Buffer.from(md) })
+  await v.page.waitForFunction(() => document.querySelector('.de-bar .de-title')?.textContent === 'notes.md')
+  await v.page.locator('.de-page-inner math').first().waitFor()
+  await v.page.waitForTimeout(400)
+  return v
+}
+
+/** the TeX of the formulas on the pages, in order */
+const texts = (v: Viewer) => v.page.locator('.de-pages annotation').allTextContents()
+
+describe('Markdown math in the document', () => {
+  test('inline and display formulas are MathML; currency, escapes and code stay text; broken ones show their source', async () => {
+    const v = await openMd()
+    const { page } = v
+    if (process.env.MATH_SHOTS) await page.locator('.de-page').first().screenshot({ path: `${process.env.MATH_SHOTS}/markdown-math.png` })
+    expect(await page.locator('.de-pages math:not([display])').count()).toBe(3)
+    expect(await page.locator('.de-pages math[display="block"]').count()).toBe(1)
+    expect(await texts(v)).toEqual(['e^{i\\pi} + 1 = 0', '\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n', '\\frac{a}{', '\\foo x'])
+    // drawn: the fraction with its parts, the display formula a block of its own, taller than a line
+    expect(await page.locator('.de-pages math[display="block"] mfrac msqrt msup').count()).toBe(1)
+    const box = (await page.locator('.de-pages math[display="block"]').boundingBox())!
+    expect(box.height).toBeGreaterThan(30)
+    expect(await page.locator('.de-pages .de-math[data-display]').evaluate((e) => getComputedStyle(e).display)).toBe('block')
+    // an equation is one object of the text: not editable inside
+    expect(await page.locator('.de-pages .de-math').first().getAttribute('data-obj')).toBe('')
+    const text = await v.pagesText()
+    expect(text).toContain('It costs $5 and $10, see $x$.')
+    expect(text).not.toContain('\\frac{-b')
+    // the broken ones: their source, marked, with the reason as the tooltip; the page goes on
+    expect(await page.locator('.de-pages .de-math[data-error]').count()).toBe(2)
+    expect(await page.locator('.de-pages .de-math[data-error]').first().getAttribute('title')).toBe('Missing }')
+    expect(await page.locator('.de-pages merror').allTextContents()).toEqual(['\\frac{a}{', '\\foo'])
+    expect(text).toContain('end.')
+    // opened is not unsaved
+    expect(await page.locator('.de-save-dirty').count()).toBe(0)
+    expect(v.errors).toEqual([])
+  })
+
+  test('typing next to an equation and saving gives back the formulas as written', async () => {
+    const v = await openMd()
+    await v.edit()
+    await v.selectText('Euler’s identity', 'end')
+    await v.page.keyboard.type(' (famous)')
+    await v.selectText(' is famous', 'end')
+    await v.page.keyboard.type('!')
+    await v.page.waitForTimeout(300)
+    const saved = await v.save()
+    expect(saved.name).toBe('notes.md')
+    expect(saved.bytes.toString('utf8')).toBe(MD.replace('Euler’s identity: $e^{i\\pi} + 1 = 0$ is famous.', 'Euler’s identity (famous): $e^{i\\pi} + 1 = 0$ is famous!.'))
+    // the Word file: real equations holding their TeX
+    const word = await v.downloadAs('Word document')
+    const ps = await docxParas(word.bytes)
+    const eq = ps[1].runs.find((r) => r.t === 'ref')
+    expect(eq).toMatchObject({ t: 'ref', math: { tex: 'e^{i\\pi} + 1 = 0', delim: '$' } })
+    expect(v.errors).toEqual([])
+  })
+
+  test('the equation is one character for the caret: stepped over, deleted, undone, copied and pasted', async () => {
+    const v = await openMd()
+    const { page } = v
+    await v.edit()
+    const inline = () => page.locator('.de-pages math:not([display])').count()
+    // the caret before it, a step to the right is after it
+    await v.selectText('identity: ', 'end')
+    await page.keyboard.press('ArrowRight')
+    await page.keyboard.type('X')
+    await page.waitForTimeout(200)
+    expect(await page.locator('.de-pages .de-p', { hasText: 'Euler' }).innerText()).toContain('X is famous')
+    await page.keyboard.press('Control+z')
+    await page.waitForTimeout(200)
+    // deleted as one, and back with Undo
+    await v.selectText('identity: ', 'end')
+    await page.keyboard.press('Delete')
+    await page.waitForTimeout(200)
+    expect(await inline()).toBe(2)
+    expect(await texts(v)).not.toContain('e^{i\\pi} + 1 = 0')
+    await page.keyboard.press('Control+z')
+    await page.waitForTimeout(300)
+    expect(await inline()).toBe(3)
+    expect(await texts(v)).toContain('e^{i\\pi} + 1 = 0')
+    // copied with the text around it, pasted in another paragraph: a formula again
+    await page.evaluate(() => {
+      const root = document.querySelector('.de-pages')!
+      const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
+      let a: Text | null = null
+      let b: Text | null = null
+      for (let n = walker.nextNode(); n; n = walker.nextNode()) {
+        if (!a && n.textContent!.includes('Euler’s identity')) a = n as Text
+        if (!b && n.textContent!.includes(' is famous')) b = n as Text
+      }
+      root.focus()
+      getSelection()!.setBaseAndExtent(a!, 0, b!, b!.textContent!.indexOf(' is famous'))
+    })
+    await page.keyboard.press('Control+c')
+    await v.selectText('It costs', 'start')
+    await page.keyboard.press('Control+v')
+    await page.waitForTimeout(500)
+    expect(await inline()).toBe(4)
+    expect((await texts(v)).filter((t) => t === 'e^{i\\pi} + 1 = 0')).toHaveLength(2)
+    const saved = (await v.save()).bytes.toString('utf8')
+    // (the paragraph is written again: its dollar signs are escaped only where they would be read as math)
+    expect(saved).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$It costs $5 and $10, see `$x$`.')
+    expect(saved).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$ is famous.')
+    // (Save took the focus)
+    await v.selectText('It costs', 'start')
+    await page.keyboard.press('Control+z')
+    await page.waitForTimeout(300)
+    expect(await inline()).toBe(3)
+    expect(v.errors).toEqual([])
+  })
+
+  test('the source pane: a formula typed in the Markdown is drawn', async () => {
+    const v = await openMd()
+    const { page } = v
+    await page.getByTestId('de-code-toggle').click()
+    const input = page.getByTestId('de-source-input')
+    await input.waitFor()
+    expect(await input.inputValue()).toBe(MD)
+    await input.click()
+    await page.keyboard.press('Control+End')
+    await page.keyboard.type('\nRoot: $\\sqrt{2}$\n')
+    await page.waitForFunction(() => [...document.querySelectorAll('.de-pages annotation')].some((a) => a.textContent === '\\sqrt{2}'))
+    expect(await page.locator('.de-pages msqrt').count()).toBe(2)
+    expect(v.errors).toEqual([])
+  })
+
+  test('print and the web page export carry the equations; text and Markdown exports their TeX', async () => {
+    const v = await openMd()
+    const { page } = v
+    // headless print is a no-op: what reaches the print frame
+    await page.evaluate(() => {
+      ;(window as any).__printed = []
+      new MutationObserver((ms) => {
+        for (const m of ms)
+          for (const n of m.addedNodes) {
+            if ((n as HTMLElement).tagName !== 'IFRAME') continue
+            const w = (n as HTMLIFrameElement).contentWindow!
+            w.print = () => {
+              const d = w.document
+              ;(window as any).__printed.push({
+                inline: d.querySelectorAll('math:not([display])').length,
+                display: d.querySelectorAll('math[display="block"]').length,
+                tex: [...d.querySelectorAll('annotation')].map((a) => a.textContent),
+                height: d.querySelector('math[display="block"]')?.getBoundingClientRect().height ?? 0,
+              })
+            }
+            w.document.execCommand = () => false
+          }
+      }).observe(document.body, { childList: true })
+    })
+    await v.menu('Print')
+    await page.waitForFunction(() => (window as any).__printed?.length > 0, null, { timeout: 30_000 })
+    const printed = await page.evaluate(() => (window as any).__printed.at(-1))
+    expect(printed).toMatchObject({ inline: 3, display: 1 })
+    expect(printed.tex[0]).toBe('e^{i\\pi} + 1 = 0')
+    expect(printed.height).toBeGreaterThan(30)
+    const web = await v.downloadAs('Web page')
+    expect(web.name).toBe('notes.html')
+    expect(web.text).toContain('e^{i\\pi} + 1 = 0')
+    expect(web.text).toMatch(/]*>/)
+    // the export's style sheet has the math rules (Chromium draws table columns from CSS)
+    expect(web.text).toContain('.de-math merror')
+    const txt = await v.downloadAs('Plain text')
+    expect(txt.text).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$ is famous.')
+    const md = await v.downloadAs('Markdown')
+    expect(md.text).toContain('$$\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n$$')
+    expect(v.errors).toEqual([])
+  })
+
+  test('TeX that tries to be markup stays text', async () => {
+    const v = await openMd('A $$ and $\\href{javascript:alert(2)}{x}$ and $\\color{red;background:url(https://example.com/x)}{y}$.\n')
+    const { page } = v
+    expect(await page.evaluate(() => (window as any).__hit)).toBeUndefined()
+    expect(await page.locator('.de-pages img, .de-pages a[href], .de-pages [onerror]').count()).toBe(0)
+    expect(await page.locator('.de-pages math [mathcolor]').count()).toBe(0)
+    // (the first one does not read as TeX: its source, as text)
+    expect(await page.locator('.de-pages merror').allTextContents()).toEqual(['', '\\href'])
+    expect(v.errors).toEqual([])
+  })
+})
diff --git a/packages/markdown/README.md b/packages/markdown/README.md
index fef20e6..88cc113 100644
--- a/packages/markdown/README.md
+++ b/packages/markdown/README.md
@@ -1,6 +1,6 @@
 # @p10node/markdown
 
-Markdown → HTML that is safe to show, with ` ```mermaid `, ` ```plantuml ` and ` ```dot ` code blocks drawn as SVG. No DOM needed, no server, no dependency.
+Markdown → HTML that is safe to show, with ` ```mermaid `, ` ```plantuml ` and ` ```dot ` code blocks drawn as SVG and `$…$` / `$$…$$` TeX math as MathML. No DOM needed, no server, no dependency.
 
 Part of [office-engine](https://github.com/p10node/office-engine). Diagrams alone: [`@p10node/diagram`](../diagram).
 
@@ -17,13 +17,14 @@ import { render, renderTo } from '@p10node/markdown'
 
 const html = render(markdownText)                       // string
 renderTo(document.getElementById('doc')!, markdownText) // into an element
-render(text, { diagrams: false, headingIds: false })
+render(text, { diagrams: false, headingIds: false, math: false })
 ```
 
 | Option       | Default | Notes                                                                            |
 |--------------|---------|----------------------------------------------------------------------------------|
 | `diagrams`   | `true`  | Draw ` ```mermaid `, ` ```plantuml ` / ` ```puml `, ` ```dot ` / ` ```graphviz ` |
 | `headingIds` | `true`  | `id` on headings from their text (`# Getting started` → `#getting-started`)      |
+| `math`       | `true`  | `$…$` and `$$…$$` TeX formulas as MathML                                         |
 
 ## What is read
 
@@ -31,10 +32,20 @@ render(text, { diagrams: false, headingIds: false })
 - Inline: emphasis, strong, code spans, links and pictures (inline and reference), autolinks, backslash escapes, hard line breaks
 - GitHub: tables (column alignment), task lists, strikethrough, bare `https://` / `www.` links
 - Code blocks get `class="language-…"`; diagrams `
`; a diagram that cannot be drawn shows why and its code (`
`) +- TeX math, read as pandoc reads it: `$…$` inline (a non-space after the opening `$`, a non-space before the closing one and no digit after it, so `$5 and $10` stays text), `$$…$$` display (over several lines too). `\$` is a dollar sign; nothing in code is math. Each formula becomes `` (`display="block"` for `$$`) with the drawing in MathML and the TeX in ``, which the browser draws by itself (no script, no font to load). What is read: Greek letters and symbols, operators, relations, arrows, scripts and primes, big operators with their limits, fractions, binomials, roots, `\left … \right`, accents, `\text`, the font commands (`\mathbb`, `\mathbf`, `\mathcal`…), spaces, `\color`, `\operatorname`, the named functions, and the `matrix` / `pmatrix` / `bmatrix` / `cases` / `aligned` / `gathered` / `array` environments. A command it does not know is shown as written in an ``, the rest drawn; a formula it cannot read (an unclosed brace…) is its source in an `` + +Chromium follows MathML Core, which leaves out the column alignment of tables (`aligned`, `cases`, `array`): add these rules to your page to have it there too. + +```css +mtd[columnalign=left] { text-align: left } +mtd[columnalign=right] { text-align: right } +mtable[columnspacing="0em 2em"] > mtr > mtd { padding-left: 0; padding-right: 0 } +mtable[columnspacing="0em 2em"] > mtr > mtd[columnalign=right]:not(:first-child) { padding-left: 2em } +``` ## Safety -The HTML can go straight into `innerHTML`: all text is escaped and **HTML written in the Markdown is shown as text**, links keep web, mail, phone and relative targets (`javascript:` and the like become plain text), and pictures load web, relative or `data:image/…` sources only. +The HTML can go straight into `innerHTML`: all text is escaped and **HTML written in the Markdown is shown as text**, links keep web, mail, phone and relative targets (`javascript:` and the like become plain text), and pictures load web, relative or `data:image/…` sources only. TeX is text too: the MathML has only MathML elements, with no link and no style, and a `\color` is kept only when it is a color name or `#rgb` value. ## License diff --git a/packages/markdown/src/index.ts b/packages/markdown/src/index.ts index 305d009..d61b149 100644 --- a/packages/markdown/src/index.ts +++ b/packages/markdown/src/index.ts @@ -1,6 +1,7 @@ /** * @p10node/markdown: Markdown → safe HTML (CommonMark blocks, GitHub tables, task lists, strikethrough, bare links), - * with ```mermaid / ```plantuml / ```dot code blocks drawn as SVG. No DOM needed, no dependency. + * with ```mermaid / ```plantuml / ```dot code blocks drawn as SVG and `$…$` / `$$…$$` TeX math as MathML. No DOM + * needed, no dependency. */ import { markdownToHtml } from './markdown' @@ -9,6 +10,8 @@ export interface MarkdownOptions { diagrams?: boolean /** `id` attributes on headings (from their text) so `#links` reach them. Default true. */ headingIds?: boolean + /** `$…$` and `$$…$$` TeX math as MathML (the browser draws it; the TeX is kept in an annotation). Default true. */ + math?: boolean } /** diff --git a/packages/markdown/src/markdown.ts b/packages/markdown/src/markdown.ts index dc59c16..30fabb5 100644 --- a/packages/markdown/src/markdown.ts +++ b/packages/markdown/src/markdown.ts @@ -2,9 +2,11 @@ * Markdown → HTML for showing in a page: CommonMark blocks (headings, paragraphs, nested lists, quotes, code, * rules) and GitHub's tables, task lists, strikethrough and bare links. The HTML is safe to set as innerHTML: * text is escaped, raw HTML in the source is shown as text, and links / images keep only harmless URLs. - * ```mermaid / ```plantuml / ```dot code blocks become SVG drawings. + * ```mermaid / ```plantuml / ```dot code blocks become SVG drawings, `$…$` / `$$…$$` TeX math MathML. */ import { DIAGRAM_NAME, FENCE_LANG, renderDiagrams } from '../../../src/core/diagram' +import { displayEnd, findMath } from '../../../src/core/math/scan' +import { texMathml } from '../../../src/core/math/tex' import { safeHref } from '../../../src/core/url' export interface MarkdownOptions { @@ -12,12 +14,15 @@ export interface MarkdownOptions { diagrams?: boolean /** `id` attributes on headings (from their text) so `#links` reach them. Default true. */ headingIds?: boolean + /** `$…$` and `$$…$$` TeX math as MathML (the TeX kept in an annotation). Default true. */ + math?: boolean } interface Ctx { refs: Map diagrams: boolean ids: boolean + math: boolean slugs: Map } @@ -37,7 +42,7 @@ const label = (s: string) => s.trim().replace(/\s+/g, ' ').toLowerCase() /** Markdown text → HTML. */ export function markdownToHtml(md: string, opts: MarkdownOptions = {}): string { const lines = md.replace(/^/, '').replace(/\r\n?/g, '\n').replace(/\t/g, ' ').split('\n') - const ctx: Ctx = { refs: new Map(), diagrams: opts.diagrams !== false, ids: opts.headingIds !== false, slugs: new Map() } + const ctx: Ctx = { refs: new Map(), diagrams: opts.diagrams !== false, ids: opts.headingIds !== false, math: opts.math !== false, slugs: new Map() } // link reference definitions (outside code blocks) are read first and leave no text const kept: string[] = [] let fence: string | null = null @@ -100,6 +105,13 @@ function blocks(lines: string[], ctx: Ctx, tight: boolean): string { out.push(`
${esc(body.join('\n'))}\n
`) continue } + // a $$ formula over several lines: they are the paragraph's, whatever they start with + const dm = ctx.math ? displayEnd(lines, i) : -1 + if (dm > i) { + para.push(...lines.slice(i, dm + 1)) + i = dm + 1 + continue + } if ((m = ATX.exec(line))) { flush() out.push(heading(m[1].length, m[2] ?? '', ctx)) @@ -244,6 +256,7 @@ function heading(level: number, text: string, ctx: Ctx): string { if (!ctx.ids) return `${html}` const base = html + .replace(//g, '') .replace(/<[^>]*>/g, '') .replace(/&\w+;|&#\d+;/g, '') .toLowerCase() @@ -302,6 +315,19 @@ function inline(src: string, ctx: Ctx): string { return hold(s ? `${a}` : a) } let s = src.replace(/[\u0000\u0001]/g, '�') + // TeX math first: between its $ / $$ is TeX, not Markdown (code spans are skipped there, escaped dollars too) + if (ctx.math) { + const spans = findMath(s) + if (spans.length) { + let t = '' + let at = 0 + for (const m of spans) { + t += s.slice(at, m.start) + hold(texMathml(m.tex, m.delim === '$$')) + at = m.end + } + s = t + s.slice(at) + } + } // code spans first: nothing inside them is markup s = s.replace(/(`+)(?!`)([\s\S]*?[^`])\1(?!`)/g, (_, _t: string, c: string) => { const t = c.replace(/\n/g, ' ') diff --git a/src/core/convert.ts b/src/core/convert.ts index 950dd7c..a03a67a 100644 --- a/src/core/convert.ts +++ b/src/core/convert.ts @@ -5,7 +5,9 @@ import { officeHex } from './color' import { dict } from './dict' import { HIGHLIGHT } from './docx/styles' -import type { RunSet } from './docx/types' +import type { RunSet, TexMath } from './docx/types' +import { M_NS, MC_NS, mathOmml, OE_NS } from './math/omml' +import { displayEnd, findMath } from './math/scan' import { escAttr, escText } from './raw' import { safeHref } from './url' import { writeZip } from './zipwrite' @@ -186,6 +188,8 @@ export interface ImportRun { text: string set?: RunSet href?: string + /** a TeX formula (written as an equation; `text` is not used) */ + math?: TexMath } /** a picture of its own paragraph: an SVG drawing (diagrams) */ @@ -318,14 +322,37 @@ function mdInline(s: string): string { return s.replace(/\u0000(\d+)\u0000/g, (_, i: string) => `${code[Number(i)]}`).replace(/\u0001(\d+)\u0001/g, (_, i: string) => lit[Number(i)]) } -/** Small CommonMark subset → HTML: headings, paragraphs, lists, quotes, code blocks, rules, tables. */ +/** + * Inline Markdown of source text (the lines of a paragraph are joined), its `$…$` / `$$…$$` formulas (see findMath) as + * `` elements holding the TeX as written; `math` false: none (an indented code block). + */ +function mathInline(src: string, math = true): string { + const spans = math ? findMath(src) : [] + const held: string[] = [] + let text = src + if (spans.length) { + text = '' + let at = 0 + for (const m of spans) { + const html = `${escHtml(m.tex)}` + text += src.slice(at, m.start).replace(/\u0002/g, '�') + `\u0002${held.push(html) - 1}\u0002` + at = m.end + } + text += src.slice(at).replace(/\u0002/g, '�') + } + const html = mdInline(escHtml(text.split('\n').map((l) => l.trim()).join(' '))) + return held.length ? html.replace(/\u0002(\d+)\u0002/g, (_, k: string) => held[Number(k)]) : html +} + +/** Small CommonMark subset → HTML: headings, paragraphs, lists, quotes, code blocks, rules, tables, TeX math. */ export function markdownToHtml(md: string): string { const lines = md.replace(/^/, '').split(/\r\n|\r|\n/) const out: string[] = [] let para: string[] = [] let list: 'ul' | 'ol' | null = null const flush = () => { - if (para.length) out.push(`

${mdInline(escHtml(para.join(' ')))}

`) + // the lines as written (a $$ formula keeps its line breaks); a paragraph indented as code has no math + if (para.length) out.push(`

${mathInline(para.join('\n'), !/^(?: {4}|\t)/.test(para[0]))}

`) para = [] } const closeList = () => { @@ -348,6 +375,13 @@ export function markdownToHtml(md: string): string { closeList() continue } + // a $$ formula over several lines: they are the paragraph's, whatever they start with + const dm = list ? -1 : displayEnd(lines, i) + if (dm > i) { + para.push(...lines.slice(i, dm + 1)) + i = dm + continue + } const h = /^\s{0,3}(#{1,6})\s+(.*)$/.exec(line) if (h) { // closing #s and the spaces around them off, by hand: a lazy `(.*?)\s*#*\s*$` is quadratic on a long run of spaces @@ -356,7 +390,7 @@ export function markdownToHtml(md: string): string { h[2] = h[2].slice(0, e).trimEnd() flush() closeList() - out.push(`${mdInline(escHtml(h[2]))}`) + out.push(`${mathInline(h[2])}`) continue } if (/^\s{0,3}([-*_])(\s*\1){2,}\s*$/.test(line)) { @@ -368,7 +402,7 @@ export function markdownToHtml(md: string): string { if (line.includes('|') && /^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?\s*$/.test(lines[i + 1] ?? '')) { flush() closeList() - const cells = (l: string) => l.trim().replace(/^\||\|$/g, '').split('|').map((c) => mdInline(escHtml(c.trim()))) + const cells = (l: string) => l.trim().replace(/^\||\|$/g, '').split('|').map((c) => mathInline(c.trim())) const rows = [`${cells(line).map((c) => `${c}`).join('')}`] i++ while (i + 1 < lines.length && lines[i + 1].includes('|') && lines[i + 1].trim()) rows.push(`${cells(lines[++i]).map((c) => `${c}`).join('')}`) @@ -384,23 +418,23 @@ export function markdownToHtml(md: string): string { out.push(`<${kind}>`) list = kind } - out.push(`
  • ${mdInline(escHtml(li[3].replace(/^\[[ xX]\]\s+/, (m) => (m[1] === ' ' ? '☐ ' : '☑ '))))}
  • `) + out.push(`
  • ${mathInline(li[3].replace(/^\[[ xX]\]\s+/, (m) => (m[1] === ' ' ? '☐ ' : '☑ ')))}
  • `) continue } const q = /^\s*>\s?(.*)$/.exec(line) if (q) { flush() closeList() - out.push(`
    ${mdInline(escHtml(q[1]))}
    `) + out.push(`
    ${mathInline(q[1])}
    `) continue } if (list && /^\s{2,}\S/.test(line)) { // lazy continuation of the previous item - out[out.length - 1] = out[out.length - 1].replace(/<\/li>$/, ` ${mdInline(escHtml(line.trim()))}`) + out[out.length - 1] = out[out.length - 1].replace(/<\/li>$/, () => ` ${mathInline(line.trim())}`) continue } closeList() - para.push(line.trim()) + para.push(line) } flush() closeList() @@ -429,6 +463,7 @@ function rPrXml(set: RunSet | undefined): string { } function runXml(r: ImportRun): string { + if (r.math) return mathOmml(r.math) let body = '' for (const piece of r.text.split(/(\t|\n)/)) { if (!piece) continue @@ -538,6 +573,8 @@ export function parasToDocx(paras: ImportPara[], opts: DocxOptions = {}): Promis body += `${pPr ? `${pPr}` : ''}${runs}` } if (!body) body = '' + // equations (TeX math): Office Math, with the TeX in attributes Word is told it may ignore + const math = paras.some((p) => p.runs.some((r) => r.math)) ? ` xmlns:m="${M_NS}" xmlns:mc="${MC_NS}" xmlns:oe="${OE_NS}" mc:Ignorable="oe"` : '' const sect = `${opts.landscape ? '' : ''}` const styles = `${XML}` + @@ -563,7 +600,7 @@ export function parasToDocx(paras: ImportPara[], opts: DocxOptions = {}): Promis ], ['_rels/.rels', rootRels('word/document.xml')], ['word/_rels/document.xml.rels', rels], - ['word/document.xml', `${XML}${body}${sect}`], + ['word/document.xml', `${XML}${body}${sect}`], ['word/styles.xml', styles], ['word/numbering.xml', numberingXml(nums)], ...images.map((svg, k) => [`word/media/diagram${k + 1}.svg`, svg] as [string, string]), diff --git a/src/core/docx/parser.ts b/src/core/docx/parser.ts index 1126e86..b1026d6 100644 --- a/src/core/docx/parser.ts +++ b/src/core/docx/parser.ts @@ -6,6 +6,7 @@ import { emuToPt } from '../units' import { desc, descAll, kid, kids, num, DOCX_UNDERSTOOD, parseXml, parseXmlStream, path, TreeBuilder, type SaxHandler, type XEl } from '../xml' import { formatNumber, Numbering } from './numbering' import { ommlText } from './omml' +import { mathOf } from '../math/omml' import { readDocxComments, type DocxComments } from './comments' import { borderCss, merge, readMargins, readPPr, readRPr, readTableBorders, shading, StyleSheet, type NumPr, @@ -436,8 +437,10 @@ export class Converter { case 'oMathPara': { const text = ommlText(c) const s = { ...base, i: true, font: '"Cambria Math", "STIX Two Math", serif' } + // a formula of a Markdown file (ours): its TeX comes along, to be drawn and saved as it was written + const math = mathOf(c.a) // an equation is one object: it is written back as it was, not as the flat text shown here - if (this.cur) sink.push({ t: 'ref', text, s, ...pick(this.srcOf(c), 'src'), ...(this.tc ? { tc: this.tc } : {}) }) + if (this.cur) sink.push({ t: 'ref', text, s, ...pick(this.srcOf(c), 'src'), ...(this.tc ? { tc: this.tc } : {}), ...(math ? { math } : {}) }) else if (text) sink.push({ t: 'text', text, s }) break } diff --git a/src/core/docx/spec.ts b/src/core/docx/spec.ts index acc8b16..62d4d65 100644 --- a/src/core/docx/spec.ts +++ b/src/core/docx/spec.ts @@ -10,7 +10,9 @@ export function inlineSpec(x: Inline): SpecRun { if (x.src) return { obj: 'copy', src: x.src, size: x.resized ? [x.w, x.h] : undefined, alt: x.altSet ? (x.alt ?? '') : undefined, rid: x.repl ? x.rid : undefined, cm: x.cm?.length ? x.cm : undefined, ...(x.tc ? { tc: x.tc } : {}) } return x.rid ? { obj: 'image', rid: x.rid, w: x.w, h: x.h, alt: x.alt, cm: x.cm?.length ? x.cm : undefined } : { text: '' } default: - return x.src ? { obj: 'copy', src: x.src, ...(x.tc ? { tc: x.tc } : {}) } : { text: '' } + if (x.src) return { obj: 'copy', src: x.src, ...(x.tc ? { tc: x.tc } : {}) } + // a pasted formula: made from its TeX + return x.t === 'ref' && x.math ? { obj: 'math', math: x.math, ...(x.tc ? { tc: x.tc } : {}) } : { text: '' } } } diff --git a/src/core/docx/types.ts b/src/core/docx/types.ts index 4abf61f..d524461 100644 --- a/src/core/docx/types.ts +++ b/src/core/docx/types.ts @@ -119,7 +119,17 @@ export type Inline = | { t: 'field'; kind: 'PAGE' | 'NUMPAGES'; s: RunStyle; src?: SrcRef; tc?: TrackMark } | { t: 'box'; w: number; h: number; blocks: Block[]; fill?: string; border?: string; src?: SrcRef; tc?: TrackMark } | { t: 'shape'; w: number; h: number; label?: string; src?: SrcRef; tc?: TrackMark } - | { t: 'ref'; text: string; s: RunStyle; src?: SrcRef; tc?: TrackMark } + /** + * kept whole and written back as it is (an equation, a field result, ruby…); `text` is what it reads as. + * `math`: a TeX formula (Markdown `$…$` / `$$…$$`), drawn from its TeX and saved back as it was written + */ + | { t: 'ref'; text: string; s: RunStyle; src?: SrcRef; tc?: TrackMark; math?: TexMath } + +/** a TeX formula and the delimiters it was written with */ +export interface TexMath { + tex: string + delim: '$' | '$$' +} export interface ListMarker { text: string @@ -184,6 +194,8 @@ export type SpecRun = | { obj: 'copy'; src: SrcRef; size?: [number, number]; alt?: string; rid?: string; cm?: string[]; tc?: TrackMark } | { obj: 'image'; rid: string; w: number; h: number; name?: string; alt?: string; cm?: string[] } | { obj: 'field'; kind: 'PAGE' | 'NUMPAGES'; set?: RunSet; cm?: string[] } + /** a TeX formula with no source run (pasted): written as a new equation */ + | { obj: 'math'; math: TexMath; cm?: string[]; tc?: TrackMark } export interface ParaSpec { pid: number diff --git a/src/core/docx/writer.ts b/src/core/docx/writer.ts index f3df9cd..85bafed 100644 --- a/src/core/docx/writer.ts +++ b/src/core/docx/writer.ts @@ -22,6 +22,7 @@ import type { TableOp, TableSpec, PageSet, PageSetResult, PageSetup, HFKind, HFLine, HFSet, HFSetResult, TrackMark, } from './types' import { dict } from '../dict' +import { MC_NS, mathOmml, OE_NS } from '../math/omml' export const DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' const NUMBERING_REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering' @@ -624,6 +625,14 @@ export async function genParagraph(doc: DocxDocument, spec: ParaSpec): Promise<{ } else if (run.obj === 'image') { body += imageRunXml(W, run.rid, run.w, run.h, run.name, run.alt) map.push({ cm: run.cm }) + } else if (run.obj === 'math') { + // (from the clipboard or a peer: checked) + const m = run.math + if (m && typeof m.tex === 'string' && (m.delim === '$' || m.delim === '$$')) { + const xml = mathOmml({ tex: m.tex, delim: m.delim }, W, true) + body += run.tc ? trackXml(doc, W, run.tc, xml) : xml + map.push({ cm: run.cm }) + } } else { const src = origRun(doc, run.src) if (src) { @@ -1284,6 +1293,26 @@ export async function saveDocx(doc: DocxDocument): Promise { const page: PageSetup = { ...doc.page, ...set } body = rewriteSects(body!, (sect) => applyPageSet(sect, set, page)) } + // an equation made from TeX (pasted): its TeX attributes are in a namespace Word must be told to ignore + if (body?.includes(' oe:tex=')) body = withMathNs(body) if (body !== undefined) doc.pkg.setPart(doc.mainPath, body) return doc.pkg.save(DOCX_MIME) } + +/** the root of the main part declares the TeX namespace, ignorable (Markup Compatibility), as Word wants it */ +export function withMathNs(xml: string): string { + const m = /<(?:[\w.-]+:)?document\b[^>]*>/.exec(xml) + if (!m) return xml + let tag = m[0] + const add = (attr: string) => (tag = tag.replace(/\s*\/?>$/, (end) => ` ${attr}${end}`)) + if (!/\sxmlns:oe=/.test(tag)) add(`xmlns:oe="${OE_NS}"`) + let mc = new RegExp(`\\sxmlns:([\\w.-]+)="${MC_NS.replace(/[.]/g, '\\.')}"`).exec(tag)?.[1] + if (!mc) { + mc = /\sxmlns:mc=/.test(tag) ? 'mce' : 'mc' + add(`xmlns:${mc}="${MC_NS}"`) + } + const ign = new RegExp(`\\s${mc}:Ignorable="([^"]*)"`).exec(tag) + if (!ign) add(`${mc}:Ignorable="oe"`) + else if (!ign[1].split(/\s+/).includes('oe')) tag = tag.replace(ign[0], ` ${mc}:Ignorable="${(ign[1] + ' oe').trim()}"`) + return xml.slice(0, m.index) + tag + xml.slice(m.index + m[0].length) +} diff --git a/src/core/html.ts b/src/core/html.ts index b915279..8f1f20a 100644 --- a/src/core/html.ts +++ b/src/core/html.ts @@ -2,7 +2,7 @@ * HTML → paragraphs of formatted runs: clipboard HTML (main thread, parsed by the browser's DOMParser) and HTML files * (also in the worker, where a small DOM-free parser reads them). */ -import type { Inline, RunSet, RunStyle } from './docx/types' +import type { Inline, RunSet, RunStyle, TexMath } from './docx/types' import { cssFont } from './fonts' import { safeHref } from './url' import { dict } from './dict' @@ -161,6 +161,20 @@ function textOf(n: HNode): string { return n.nodeType === 3 ? (n.textContent ?? '') : Array.from(n.childNodes).map(textOf).join('') } +const TEX_ENCODING = /^(?:application\/x-tex|application\/x-latex|text\/x-tex|text\/x-latex|tex|latex)$/i + +/** the TeX of a MathML element, from its TeX annotation; null when it has none */ +function texAnnotation(n: HNode): string | null { + for (const k of Array.from(n.childNodes)) { + if (k.nodeType !== 1) continue + const e = k as HElement + if (e.tagName.toUpperCase() === 'ANNOTATION' && TEX_ENCODING.test((e.getAttribute('encoding') ?? '').trim())) return textOf(e) + const inner = texAnnotation(e) + if (inner !== null) return inner + } + return null +} + /** whether the marker text Word writes in front of a list paragraph ("1.", "a)", "(i)") numbers, as opposed to a bullet ("·", "o", "§") */ const NUMBERED = /^\(?[\dA-Za-z]{1,4}[.)]$/ @@ -209,6 +223,15 @@ function convert(root: HNode, base: Ctx, rich: boolean, tables: boolean): Pasted ;(cur ?? para()).runs.push(toRun('\n', c)) continue } + // MathML that says its TeX (a Markdown formula, ours copied, KaTeX / Wikipedia): the formula; other MathML: its text + if (el.tagName.toUpperCase() === 'MATH') { + const tex = texAnnotation(el) + if (tex !== null) { + const math: TexMath = { tex, delim: el.getAttribute('display') === 'block' ? '$$' : '$' } + ;(cur ?? para()).runs.push({ t: 'ref', text: tex.trim(), s: c.sz ? { sz: c.sz } : {}, math }) + continue + } + } if (el.tagName === 'IMG' && rich) { const src = (el.getAttribute('src') ?? '').trim() if (DATA_IMG.test(src) && src.length <= MAX_IMG) { diff --git a/src/core/math/omml.ts b/src/core/math/omml.ts new file mode 100644 index 0000000..237b11a --- /dev/null +++ b/src/core/math/omml.ts @@ -0,0 +1,232 @@ +/** + * TeX math → Office Math (OMML), for the Word file a Markdown document is: Word shows a real equation. The TeX and its + * delimiters go along in two attributes of our own namespace (declared ignorable, so Word reads the file and drops + * them), which is how the document gives the formula back as it was written. + */ +import { dict } from '../dict' +import { escAttr, escText } from '../raw' +import type { TexMath } from './scan' +import { textOf, texToMathml, type MNode } from './tex' + +export const M_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/math' +export const MC_NS = 'http://schemas.openxmlformats.org/markup-compatibility/2006' +/** the namespace of the TeX attributes (`oe:tex`, `oe:delim`) on an equation */ +export const OE_NS = 'urn:p10node:office-engine:math' + +/** accents → the combining mark OMML puts over the base */ +const ACC: Record = dict({ + '^': '\u0302', '\u02c7': '\u030c', '~': '\u0303', '\u00b4': '\u0301', '`': '\u0300', '\u02d9': '\u0307', '\u00a8': '\u0308', + '\u20db': '\u20db', '\u20dc': '\u20dc', '\u02d8': '\u0306', '\u00af': '\u0305', '\u2192': '\u20d7', '\u2190': '\u20d6', '\u2194': '\u20e1', '\u02da': '\u030a', +}) +/** relations and punctuation: where the operand of a big operator ends */ +const STOP = new Set([...'=<>≤≥≠≈≡∼≃≅≪≫∈∉⊂⊃⊆⊇→←↔⇒⇐⇔⟶⟹⟺↦∝,;:|']) +/** colors Word takes by name, as hex (other names are left out) */ +const NAMED: Record = dict({ + black: '000000', white: 'FFFFFF', red: 'FF0000', green: '008000', blue: '0000FF', yellow: 'FFFF00', cyan: '00FFFF', magenta: 'FF00FF', + gray: '808080', grey: '808080', orange: 'FFA500', purple: '800080', brown: 'A52A2A', pink: 'FFC0CB', violet: 'EE82EE', teal: '008080', + olive: '808000', lime: '00FF00', navy: '000080', maroon: '800000', silver: 'C0C0C0', aqua: '00FFFF', fuchsia: 'FF00FF', darkgray: 'A9A9A9', + lightgray: 'D3D3D3', darkgreen: '006400', darkblue: '00008B', darkred: '8B0000', +}) + +function hexOf(c: string | undefined): string | undefined { + if (!c) return undefined + if (/^#[0-9a-f]{6}$/i.test(c)) return c.slice(1).toUpperCase() + if (/^#[0-9a-f]{3}$/i.test(c)) return [...c.slice(1)].map((x) => x + x).join('').toUpperCase() + return NAMED[c.toLowerCase()] +} + +class Writer { + constructor(private w: string) {} + + /** a run of math text: `plain` upright (sty p), `text` ordinary text (m:nor) */ + run(t: string, how: { plain?: boolean; text?: boolean }, color?: string): string { + if (!t) return '' + const pr = how.text ? '' : how.plain ? '' : '' + const w = this.w + const rpr = how.text ? (color ? `<${w}rPr><${w}color ${w}val="${color}"/>` : '') : `<${w}rPr><${w}rFonts ${w}ascii="Cambria Math" ${w}hAnsi="Cambria Math"/>${color ? `<${w}color ${w}val="${color}"/>` : ''}` + return `${pr}${rpr}${escText(t)}` + } + + /** the content of an argument (m:e, m:num…) */ + arg(tag: string, n: MNode | undefined, color?: string): string { + const body = n ? this.node(n, color) : '' + return body ? `${body}` : `` + } + + /** children of a row, a big operator taking what follows it (up to a relation) as its operand */ + row(kids: MNode[], color?: string): string { + let out = '' + for (let i = 0; i < kids.length; i++) { + const k = kids[i] + const big = bigOf(k) + if (big) { + let j = i + 1 + while (j < kids.length && !isStop(kids[j])) j++ + out += this.nary(big, kids.slice(i + 1, j), color) + i = j - 1 + continue + } + out += this.node(k, color) + } + return out + } + + private nary(b: Big, operand: MNode[], color?: string): string { + const chr = b.op.x ?? '∫' + const pr = + `` + + (b.sub ? '' : '') + + (b.sup ? '' : '') + + '' + return `${pr}${this.arg('sub', b.sub, color)}${this.arg('sup', b.sup, color)}${this.row(operand, color)}` + } + + node(n: MNode, color?: string): string { + const k = n.k ?? [] + switch (n.t) { + case 'math': + case 'semantics': + return k[0] ? this.node(k[0], color) : '' + case 'mi': { + const t = n.x ?? '' + return this.run(t, { plain: n.a?.mathvariant === 'normal' || [...t].length > 1 }, color) + } + case 'mn': + return this.run(n.x ?? '', {}, color) + case 'mo': + return this.run((n.x ?? '').replace(/[\u2061\u200b]/g, ''), {}, color) + case 'mtext': + case 'merror': + return this.run(textOf(n), { text: true }, color) + case 'mspace': { + const w = parseFloat(n.a?.width ?? '0') + const sp = w >= 2 ? '\u2003\u2003' : w >= 1 ? '\u2003' : w >= 0.5 ? '\u2002' : w >= 0.27 ? '\u2004' : w >= 0.2 ? '\u2005' : w >= 0.15 ? '\u2009' : '' + return this.run(sp, {}, color) + } + case 'mstyle': { + const c = hexOf(n.a?.mathcolor) ?? color + return this.row(k, c) + } + case 'mrow': + case 'mpadded': { + const f = n.n?.fence + if (!f) return this.row(k, color) + // \left( … \middle| … \right): one delimiter object, the parts between \middle as its arguments + const inner = k.filter((x) => !(x.t === 'mo' && x.a?.fence === 'true')) + const parts: MNode[][] = [[]] + let sep = '' + for (const x of inner) { + if (x.n?.sep) { + sep ||= (x.x ?? '').replace('\u200b', '') + parts.push([]) + } else parts[parts.length - 1].push(x) + } + const pr = `${sep ? `` : ''}` + return `${pr}${parts.map((p) => `${this.row(p, color)}`).join('')}` + } + case 'mfrac': { + const pr = n.a?.linethickness === '0' ? '' : '' + return `${pr}${this.arg('num', k[0], color)}${this.arg('den', k[1], color)}` + } + case 'msqrt': + return `${this.arg('e', k[0], color)}` + case 'mroot': + return `${this.arg('deg', k[1], color)}${this.arg('e', k[0], color)}` + case 'msub': + case 'msup': + case 'msubsup': + case 'munder': + case 'mover': + case 'munderover': { + const big = bigOf(n) + if (big) return this.nary(big, [], color) + const [b, x, y] = k + if (n.t === 'msub') return `${this.arg('e', b, color)}${this.arg('sub', x, color)}` + if (n.t === 'msup') return `${this.arg('e', b, color)}${this.arg('sup', x, color)}` + if (n.t === 'msubsup') return `${this.arg('e', b, color)}${this.arg('sub', x, color)}${this.arg('sup', y, color)}` + const mark = x?.t === 'mo' ? (x.x ?? '') : '' + if (n.t === 'mover' && (mark === '‾' || mark === '¯') && n.a?.accent === 'true' && (x.a?.stretchy === 'true' || mark === '‾')) + return `${this.arg('e', b, color)}` + if (n.t === 'munder' && mark === '_') return `${this.arg('e', b, color)}` + if (mark === '⏞' || mark === '⏟' || mark === '⏠' || mark === '⏡') { + const top = n.t === 'mover' + return `${this.arg('e', b, color)}` + } + if (n.t === 'mover' && n.a?.accent === 'true' && ACC[mark]) return `${this.arg('e', b, color)}` + if (n.t === 'munder') return `${this.arg('e', b, color)}${this.arg('lim', x, color)}` + if (n.t === 'mover') return `${this.arg('e', b, color)}${this.arg('lim', x, color)}` + return `${this.arg('e', b, color)}${this.arg('lim', x, color)}${this.arg('lim', y, color)}` + } + case 'mtable': { + const rows = k.filter((r) => r.t === 'mtr') + const cols = Math.max(1, ...rows.map((r) => (r.k ?? []).length)) + const aligns = Array.from({ length: cols }, (_, c) => rows[0]?.k?.[c]?.a?.columnalign ?? 'center') + let mcs = '' + for (let c = 0; c < cols; ) { + let e = c + while (e < cols && aligns[e] === aligns[c]) e++ + mcs += `` + c = e + } + const body = rows.map((r) => `${Array.from({ length: cols }, (_, c) => this.arg('e', r.k?.[c]?.k?.[0], color)).join('')}`).join('') + return `${mcs}${body}` + } + case 'mphantom': + return `${this.row(k, color)}` + case 'menclose': + return `${this.row(k, color)}` + case 'annotation': + return '' + default: + return this.row(k, color) + } + } +} + +interface Big { + op: MNode + sub?: MNode + sup?: MNode + under: boolean +} + +/** a big operator (∑, ∫…), with its limits */ +function bigOf(n: MNode): Big | undefined { + if (n.t === 'mo' && n.n?.op && n.n.op !== 'limits') return { op: n, under: n.n.op === 'display' } + const b = n.k?.[0] + if (!b || b.t !== 'mo' || !b.n?.op || b.n.op === 'limits') return undefined + switch (n.t) { + case 'msub': return { op: b, sub: n.k![1], under: false } + case 'msup': return { op: b, sup: n.k![1], under: false } + case 'msubsup': return { op: b, sub: n.k![1], sup: n.k![2], under: false } + case 'munder': return { op: b, sub: n.k![1], under: true } + case 'mover': return { op: b, sup: n.k![1], under: true } + case 'munderover': return { op: b, sub: n.k![1], sup: n.k![2], under: true } + } + return undefined +} + +const isStop = (n: MNode) => n.t === 'mo' && STOP.has(n.x ?? '') + +/** the attribute value of a text: line breaks and tabs as character references, so they come back as they were */ +const attrText = (s: string) => escAttr(s).replace(/\r/g, ' ').replace(/\n/g, ' ').replace(/\t/g, ' ') + +/** + * The OMML of a formula: `m:oMath` (inline) or `m:oMathPara` (display) holding the equation, with the TeX and its + * delimiters as `oe:tex` / `oe:delim`. `w` is the WordprocessingML prefix of the document ("w:"); `ns`: the + * namespaces are declared on the element (where the document root may not declare them). + */ +export function mathOmml(m: TexMath, w = 'w:', ns = false): string { + const display = m.delim === '$$' + const body = new Writer(w).node(texToMathml(m.tex, display).node) || ' ' + const decl = ns ? ` xmlns:m="${M_NS}" xmlns:oe="${OE_NS}"` : '' + const attrs = `${decl} oe:tex="${attrText(m.tex)}" oe:delim="${display ? '$$' : '$'}"` + return display ? `${body}` : `${body}` +} + +/** the TeX an equation element of ours carries (its attributes, read by the XML reader without their prefix) */ +export function mathOf(a: Record): TexMath | undefined { + const tex = a.tex + const delim = a.delim + return typeof tex === 'string' && (delim === '$' || delim === '$$') ? { tex, delim } : undefined +} diff --git a/src/core/math/scan.ts b/src/core/math/scan.ts new file mode 100644 index 0000000..6e3c4f5 --- /dev/null +++ b/src/core/math/scan.ts @@ -0,0 +1,123 @@ +/** + * TeX math in Markdown text, as pandoc reads it (`tex_math_dollars`): `$…$` inline, `$$…$$` display. + * + * - `$$` opens display math, closed by the next `$$`; it may span lines (not a blank one: the caller gives one paragraph) + * - `$` opens inline math when a non-space follows it; the next `$` closes it when a non-space is before it and no digit + * after it, else this `$` is a dollar sign (`costs $5 and $10` has no math) + * - `\$` is a dollar sign, and a backslash escape is skipped inside math too (`$a\$b$`) + * - nothing inside a code span (`` `$x$` ``) is math, and a `$` whose closing `$` would be past the start of a code + * span is a dollar sign (the code span wins, as on GitHub) + * - `\(…\)` and `\[…\]` are not math: in Markdown they are escaped brackets + */ +import type { TexMath } from '../docx/types' + +export type { TexMath } + +/** a formula in a text: the TeX between the delimiters as written (spaces and line breaks of a `$$` block included) */ +export interface MathSpan extends TexMath { + /** where the opening delimiter starts and the closing one ends */ + start: number + end: number +} + +const isSpace = (c: string | undefined) => c === ' ' || c === '\t' || c === '\n' || c === '\r' || c === '\f' || c === '\v' +const isDigit = (c: string | undefined) => c !== undefined && c >= '0' && c <= '9' + +/** where a run of `n` backticks closes a code span opened at `from`, or -1 */ +function codeEnd(s: string, from: number, n: number): number { + for (let i = s.indexOf('`', from); i >= 0; i = s.indexOf('`', i)) { + let j = i + while (s[j] === '`') j++ + if (j - i === n) return j + i = j + } + return -1 +} + +/** where a code span starting at `i` (a run of backticks) ends, or -1 when the run is only backticks */ +function codeSpan(s: string, i: number): { run: number; end: number } { + let j = i + while (s[j] === '`') j++ + return { run: j, end: codeEnd(s, j, j - i) } +} + +/** + * Where the formula opened at `from` closes: the index of its closing delimiter (`$`, or `$$` when `display`), or -1. + * Backslash escapes are skipped; a code span before the delimiter means there is no formula (the code wins). + */ +function closer(s: string, from: number, display: boolean): number { + for (let j = from; j < s.length; ) { + const c = s[j] + if (c === '\\') j += 2 + else if (c === '`') { + const code = codeSpan(s, j) + if (code.end >= 0) return -1 + j = code.run + } else if (c === '$' && (!display || s[j + 1] === '$')) return j + else j++ + } + return -1 +} + +/** the math spans of a text (a paragraph, a heading, a table cell…), in order */ +export function findMath(s: string): MathSpan[] { + const out: MathSpan[] = [] + if (!s.includes('$')) return out + const n = s.length + for (let i = 0; i < n; ) { + const c = s[i] + if (c === '\\') { + i += 2 + continue + } + if (c === '`') { + const code = codeSpan(s, i) + i = code.end < 0 ? code.run : code.end + continue + } + if (c !== '$') { + i++ + continue + } + if (s[i + 1] === '$') { + // display: up to the next $$, not empty + const e = closer(s, i + 2, true) + if (e >= 0 && s.slice(i + 2, e).trim()) out.push({ start: i, end: e + 2, tex: s.slice(i + 2, e), delim: '$$' }) + // (an empty one is text, both of its $$) + i = e >= 0 ? e + 2 : i + 2 + continue + } + if (i + 1 < n && !isSpace(s[i + 1])) { + // inline: the first $ after it closes it, or this one is a dollar sign + const j = closer(s, i + 1, false) + if (j >= 0 && !isSpace(s[j - 1]) && !isDigit(s[j + 1])) { + out.push({ start: i, end: j + 1, tex: s.slice(i + 1, j), delim: '$' }) + i = j + 1 + continue + } + } + i++ + } + return out +} + +/** + * The Markdown of a formula: its TeX between its delimiters, as it was written. TeX that `$…$` cannot hold (spaces at + * its ends, a `$` of its own, from a paste) gets `$$`. + */ +export function mathSource(m: TexMath): string { + if (m.delim === '$' && (!m.tex || /^\s|\s$/.test(m.tex) || /(^|[^\\])(\\\\)*\$/.test(m.tex) || /(^|[^\\])(\\\\)*\\$/.test(m.tex))) return `$$${m.tex}$$` + return m.delim + m.tex + m.delim +} + +/** `$$` lines that open a display formula the lines after them close: the block's lines are one paragraph, whatever they start with */ +export function displayEnd(lines: string[], i: number): number { + const line = lines[i] + const at = line.indexOf('$$') + if (at < 0 || !/^ {0,3}\$\$/.test(line) || line.indexOf('$$', at + 2) >= 0) return -1 + for (let j = i + 1; j < lines.length && j < i + 1000; j++) { + if (!lines[j].trim()) return -1 + if (lines[j].includes('$$')) return j + } + return -1 +} diff --git a/src/core/math/symbols.ts b/src/core/math/symbols.ts new file mode 100644 index 0000000..50c1dbe --- /dev/null +++ b/src/core/math/symbols.ts @@ -0,0 +1,161 @@ +/** + * The TeX commands the math converter knows that stand for one character (letters, symbols, operators, arrows, + * delimiters) or a function name, and the Unicode letters of the math alphabets (bold, script, double-struck…). + */ +import { dict } from '../dict' + +/** "name char name char…" → { name: char } */ +const table = (s: string): Record => { + const out: Record = dict({}) + const parts = s.trim().split(/\s+/) + for (let k = 0; k + 1 < parts.length; k += 2) out[parts[k]] = parts[k + 1] + return out +} + +/** letters and other ordinary symbols: an `mi` */ +export const ORD = table(` + alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ varkappa ϰ + lambda λ mu μ nu ν xi ξ omicron ο pi π varpi ϖ rho ρ varrho ϱ sigma σ varsigma ς tau τ upsilon υ phi ϕ varphi φ + chi χ psi ψ omega ω digamma ϝ + infty ∞ partial ∂ nabla ∇ forall ∀ exists ∃ nexists ∄ emptyset ∅ varnothing ∅ hbar ℏ hslash ℏ ell ℓ Re ℜ Im ℑ + aleph ℵ beth ℶ gimel ℷ daleth ℸ wp ℘ angle ∠ measuredangle ∡ sphericalangle ∢ triangle △ triangledown ▽ + square □ Box □ blacksquare ■ Diamond ◊ lozenge ◊ blacklozenge ⧫ bigstar ★ clubsuit ♣ diamondsuit ♢ heartsuit ♡ + spadesuit ♠ flat ♭ natural ♮ sharp ♯ backprime ‵ top ⊤ bot ⊥ degree ° checkmark ✓ mho ℧ eth ð imath ı jmath ȷ + complement ∁ Finv Ⅎ Game ⅁ circledS Ⓢ S § P ¶ copyright © pounds £ yen ¥ euro € dag † ddag ‡ + vartriangle △ blacktriangle ▴ blacktriangledown ▾ diagup ╱ diagdown ╲ +`) + +/** capital Greek letters: upright, as TeX sets them */ +export const UPRIGHT = table(` + Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Upsilon Υ Phi Φ Psi Ψ Omega Ω + Alpha Α Beta Β Epsilon Ε Zeta Ζ Eta Η Iota Ι Kappa Κ Mu Μ Nu Ν Omicron Ο Rho Ρ Tau Τ Chi Χ +`) + +/** binary operators, relations, arrows, punctuation: an `mo` */ +export const OP = table(` + pm ± mp ∓ times × div ÷ cdot ⋅ ast ∗ star ⋆ circ ∘ bullet ∙ oplus ⊕ ominus ⊖ otimes ⊗ oslash ⊘ odot ⊙ cap ∩ cup ∪ + sqcap ⊓ sqcup ⊔ uplus ⊎ wedge ∧ land ∧ vee ∨ lor ∨ setminus ∖ smallsetminus ∖ wr ≀ amalg ⨿ diamond ⋄ + bigtriangleup △ bigtriangledown ▽ triangleleft ◃ triangleright ▹ lhd ⊲ rhd ⊳ unlhd ⊴ unrhd ⊵ cdotp ⋅ centerdot ⋅ + intercal ⊺ ltimes ⋉ rtimes ⋊ leftthreetimes ⋋ rightthreetimes ⋌ dotplus ∔ divideontimes ⋇ barwedge ⌅ veebar ⊻ + curlywedge ⋏ curlyvee ⋎ boxplus ⊞ boxminus ⊟ boxtimes ⊠ boxdot ⊡ circledast ⊛ circledcirc ⊚ circleddash ⊝ + Cap ⋒ Cup ⋓ gtrdot ⋗ lessdot ⋖ dagger † ddagger ‡ neg ¬ lnot ¬ prime ′ surd √ ldotp . colon : + leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ equiv ≡ approx ≈ approxeq ≊ sim ∼ simeq ≃ cong ≅ propto ∝ ll ≪ gg ≫ lll ⋘ ggg ⋙ + prec ≺ succ ≻ preceq ⪯ succeq ⪰ subset ⊂ supset ⊃ subseteq ⊆ supseteq ⊇ subsetneq ⊊ supsetneq ⊋ sqsubset ⊏ + sqsupset ⊐ sqsubseteq ⊑ sqsupseteq ⊒ in ∈ ni ∋ owns ∋ notin ∉ vdash ⊢ dashv ⊣ models ⊨ vDash ⊨ Vdash ⊩ Vvdash ⊪ + perp ⊥ parallel ∥ nparallel ∦ mid ∣ nmid ∤ asymp ≍ bowtie ⋈ Join ⋈ smile ⌣ frown ⌢ doteq ≐ doteqdot ≑ Doteq ≑ + leqslant ⩽ geqslant ⩾ leqq ≦ geqq ≧ lesssim ≲ gtrsim ≳ lessapprox ⪅ gtrapprox ⪆ lessgtr ≶ gtrless ≷ nless ≮ ngtr ≯ + nleq ≰ ngeq ≱ nsim ≁ ncong ≇ nsubseteq ⊈ nsupseteq ⊉ triangleq ≜ coloneqq ≔ eqqcolon ≕ thicksim ∼ thickapprox ≈ + backsim ∽ backsimeq ⋍ eqsim ≂ risingdotseq ≓ fallingdotseq ≒ circeq ≗ bumpeq ≏ Bumpeq ≎ therefore ∴ because ∵ + varpropto ∝ between ≬ pitchfork ⋔ vartriangleleft ⊲ vartriangleright ⊳ trianglelefteq ⊴ trianglerighteq ⊵ + lneq ⪇ gneq ⪈ lneqq ≨ gneqq ≩ precsim ≾ succsim ≿ nprec ⊀ nsucc ⊁ nvdash ⊬ nvDash ⊭ Subset ⋐ Supset ⋑ + subseteqq ⫅ supseteqq ⫆ eqcirc ≖ gtreqless ⋛ lesseqgtr ⋚ shortmid ∣ shortparallel ∥ smallsmile ⌣ smallfrown ⌢ + to → rightarrow → leftarrow ← gets ← leftrightarrow ↔ Rightarrow ⇒ Leftarrow ⇐ Leftrightarrow ⇔ implies ⟹ + impliedby ⟸ iff ⟺ longrightarrow ⟶ longleftarrow ⟵ longleftrightarrow ⟷ Longrightarrow ⟹ Longleftarrow ⟸ + Longleftrightarrow ⟺ mapsto ↦ longmapsto ⟼ hookrightarrow ↪ hookleftarrow ↩ uparrow ↑ downarrow ↓ updownarrow ↕ + Uparrow ⇑ Downarrow ⇓ Updownarrow ⇕ nearrow ↗ searrow ↘ swarrow ↙ nwarrow ↖ rightharpoonup ⇀ rightharpoondown ⇁ + leftharpoonup ↼ leftharpoondown ↽ rightleftharpoons ⇌ leftrightharpoons ⇋ upharpoonleft ↿ upharpoonright ↾ + downharpoonleft ⇃ downharpoonright ⇂ rightrightarrows ⇉ leftleftarrows ⇇ rightleftarrows ⇄ leftrightarrows ⇆ + twoheadrightarrow ↠ twoheadleftarrow ↞ rightarrowtail ↣ leftarrowtail ↢ looparrowright ↬ looparrowleft ↫ + curvearrowright ↷ curvearrowleft ↶ circlearrowright ↻ circlearrowleft ↺ Rrightarrow ⇛ Lleftarrow ⇚ + rightsquigarrow ⇝ leadsto ⇝ leftrightsquigarrow ↭ nrightarrow ↛ nleftarrow ↚ nRightarrow ⇏ nLeftarrow ⇍ + nleftrightarrow ↮ nLeftrightarrow ⇎ dashrightarrow ⇢ dashleftarrow ⇠ Lsh ↰ Rsh ↱ multimap ⊸ restriction ↾ + upuparrows ⇈ downdownarrows ⇊ + ldots … dots … cdots ⋯ vdots ⋮ ddots ⋱ dotsb ⋯ dotsc … dotsi ⋯ dotsm ⋯ dotso … iddots ⋰ mathellipsis … +`) + +/** big operators (`n` kind: limits under / over in display style, or always beside for the integrals) */ +export const BIG = table(` + sum ∑ prod ∏ coprod ∐ bigcup ⋃ bigcap ⋂ bigsqcup ⨆ bigvee ⋁ bigwedge ⋀ bigoplus ⨁ bigotimes ⨂ bigodot ⨀ + biguplus ⨄ int ∫ iint ∬ iiint ∭ iiiint ⨌ oint ∮ oiint ∯ oiiint ∰ intop ∫ smallint ∫ +`) +export const INTEGRAL = new Set(['∫', '∬', '∭', '⨌', '∮', '∯', '∰']) +/** big operator characters typed as themselves */ +export const BIG_CHARS = new Set(Object.values(BIG)) + +/** named functions, upright; the second set takes its limits under it in display style */ +export const FUNC = new Set('arccos arcsin arctan arg cos cosh cot coth csc deg dim exp hom ker lg ln log sec sin sinh tan tanh sgn sech csch arccot arcsec arccsc'.split(' ')) +export const FUNC_LIMITS = new Set('det gcd inf lim liminf limsup max min Pr sup injlim projlim'.split(' ')) +const FUNC_TEXT: Record = dict({ liminf: 'lim inf', limsup: 'lim sup', injlim: 'inj lim', projlim: 'proj lim' }) +export const funcText = (name: string) => FUNC_TEXT[name] ?? name + +/** delimiters after \left, \right, \big…, and on their own */ +export const DELIM = table(` + langle ⟨ rangle ⟩ lvert | rvert | vert | lVert ‖ rVert ‖ Vert ‖ lfloor ⌊ rfloor ⌋ lceil ⌈ rceil ⌉ lbrace { rbrace } + lbrack [ rbrack ] lgroup ⟮ rgroup ⟯ lmoustache ⎰ rmoustache ⎱ ulcorner ┌ urcorner ┐ llcorner └ lrcorner ┘ + backslash \\ uparrow ↑ downarrow ↓ updownarrow ↕ Uparrow ⇑ Downarrow ⇓ Updownarrow ⇕ { { } } | ‖ +`) +/** characters that are delimiters after \left / \right */ +export const DELIM_CHARS = new Set(['(', ')', '[', ']', '|', '/', '<', '>']) + +/** spaces, in em */ +export const SPACE: Record = dict({ + ',': 0.1667, ':': 0.2222, '>': 0.2222, ';': 0.2778, '!': -0.1667, ' ': 0.3333, quad: 1, qquad: 2, enspace: 0.5, enskip: 0.5, + thinspace: 0.1667, medspace: 0.2222, thickspace: 0.2778, negthinspace: -0.1667, negmedspace: -0.2222, negthickspace: -0.2778, space: 0.3333, +}) + +/** accents: the mark, whether it grows with what is under it, and whether it goes under */ +export const ACCENT: Record = dict({ + hat: { c: '^', accent: true }, widehat: { c: '^', wide: true, accent: true }, check: { c: 'ˇ', accent: true }, widecheck: { c: 'ˇ', wide: true, accent: true }, + tilde: { c: '~', accent: true }, widetilde: { c: '~', wide: true, accent: true }, acute: { c: '´', accent: true }, grave: { c: '`', accent: true }, + dot: { c: '˙', accent: true }, ddot: { c: '¨', accent: true }, dddot: { c: '⃛', accent: true }, ddddot: { c: '⃜', accent: true }, breve: { c: '˘', accent: true }, + bar: { c: '¯', accent: true }, vec: { c: '→', accent: true }, mathring: { c: '˚', accent: true }, + overline: { c: '‾', wide: true, accent: true }, underline: { c: '_', wide: true, under: true }, + overrightarrow: { c: '→', wide: true }, overleftarrow: { c: '←', wide: true }, overleftrightarrow: { c: '↔', wide: true }, + underrightarrow: { c: '→', wide: true, under: true }, underleftarrow: { c: '←', wide: true, under: true }, underleftrightarrow: { c: '↔', wide: true, under: true }, + overbrace: { c: '⏞', wide: true }, underbrace: { c: '⏟', wide: true, under: true }, overgroup: { c: '⏠', wide: true }, undergroup: { c: '⏡', wide: true, under: true }, + utilde: { c: '~', wide: true, under: true }, +}) + +/** relation (and arrow) characters a `\not` turns into their negated form; others get a long solidus overlay */ +export const NEGATED: Record = dict({ + '=': '≠', '<': '≮', '>': '≯', '≤': '≰', '≥': '≱', '∈': '∉', '∋': '∌', '⊂': '⊄', '⊃': '⊅', '⊆': '⊈', '⊇': '⊉', '≡': '≢', '∼': '≁', '≈': '≉', + '≅': '≇', '∣': '∤', '∥': '∦', '→': '↛', '←': '↚', '↔': '↮', '⇒': '⇏', '⇐': '⇍', '⇔': '⇎', '≺': '⊀', '≻': '⊁', '⊢': '⊬', '⊨': '⊭', '∃': '∄', + '≃': '≄', '≍': '≭', '⊑': '⋢', '⊒': '⋣', '⊲': '⋪', '⊳': '⋫', '⊴': '⋬', '⊵': '⋭', +}) + +/** characters typed as themselves that are operators (the rest of the non-letters are too, but these are common) */ +export const OP_CHAR: Record = dict({ '-': '−', '*': '∗', "'": '′' }) + +// ---------------------------------------------------------------- math alphabets + +export type Font = + | 'normal' | 'bold' | 'italic' | 'bold-italic' | 'double-struck' | 'script' | 'bold-script' | 'fraktur' | 'bold-fraktur' + | 'sans-serif' | 'bold-sans-serif' | 'sans-serif-italic' | 'sans-serif-bold-italic' | 'monospace' + +const LATIN: Partial> = { + bold: 0x1d400, italic: 0x1d434, 'bold-italic': 0x1d468, script: 0x1d49c, 'bold-script': 0x1d4d0, fraktur: 0x1d504, 'double-struck': 0x1d538, + 'bold-fraktur': 0x1d56c, 'sans-serif': 0x1d5a0, 'bold-sans-serif': 0x1d5d4, 'sans-serif-italic': 0x1d608, 'sans-serif-bold-italic': 0x1d63c, monospace: 0x1d670, +} +const DIGIT: Partial> = { bold: 0x1d7ce, 'double-struck': 0x1d7d8, 'sans-serif': 0x1d7e2, 'bold-sans-serif': 0x1d7ec, monospace: 0x1d7f6 } +const GREEK: Partial> = { bold: 0x1d6a8, italic: 0x1d6e2, 'bold-italic': 0x1d71c, 'bold-sans-serif': 0x1d756, 'sans-serif-bold-italic': 0x1d790 } +/** letters of the math alphabets that Unicode had already (the Letterlike Symbols block): their slots are empty */ +const HOLES: Record = { + 0x1d455: 0x210e, 0x1d49d: 0x212c, 0x1d4a0: 0x2130, 0x1d4a1: 0x2131, 0x1d4a3: 0x210b, 0x1d4a4: 0x2110, 0x1d4a7: 0x2112, 0x1d4a8: 0x2133, + 0x1d4ad: 0x211b, 0x1d4ba: 0x212f, 0x1d4bc: 0x210a, 0x1d4c4: 0x2134, 0x1d506: 0x212d, 0x1d50b: 0x210c, 0x1d50c: 0x2111, 0x1d515: 0x211c, + 0x1d51d: 0x2128, 0x1d53a: 0x2102, 0x1d53f: 0x210d, 0x1d545: 0x2115, 0x1d547: 0x2119, 0x1d548: 0x211a, 0x1d549: 0x211d, 0x1d551: 0x2124, +} +const GREEK_TAIL: Record = { 0x3f4: 17, 0x2207: 25, 0x2202: 51, 0x3f5: 52, 0x3d1: 53, 0x3f0: 54, 0x3d5: 55, 0x3f1: 56, 0x3d6: 57 } + +/** where a Greek letter (or ∇, ∂ and the variant forms) stands in a math alphabet's block of 58 */ +function greekSlot(cp: number): number | undefined { + if (cp >= 0x391 && cp <= 0x3a9 && cp !== 0x3a2) return cp - 0x391 + if (cp >= 0x3b1 && cp <= 0x3c9) return 26 + cp - 0x3b1 + return GREEK_TAIL[cp] +} + +/** a letter or digit in a math alphabet (𝐱, ℝ, 𝔤…); itself when the alphabet has no such character */ +export function styled(ch: string, font: Font): string { + const cp = ch.codePointAt(0)! + let base: number | undefined + let at = -1 + if (cp >= 65 && cp <= 90) (base = LATIN[font]), (at = cp - 65) + else if (cp >= 97 && cp <= 122) (base = LATIN[font]), (at = cp - 97 + 26) + else if (cp >= 48 && cp <= 57) (base = DIGIT[font]), (at = cp - 48) + else { + const slot = greekSlot(cp) + if (slot !== undefined) (base = GREEK[font]), (at = slot) + } + if (base === undefined || at < 0) return ch + const out = base + at + return String.fromCodePoint(HOLES[out] ?? out) +} diff --git a/src/core/math/tex.ts b/src/core/math/tex.ts new file mode 100644 index 0000000..f3f75c8 --- /dev/null +++ b/src/core/math/tex.ts @@ -0,0 +1,826 @@ +/** + * TeX math → MathML: what people write between `$…$` / `$$…$$` in Markdown, as a tree the views draw with the + * browser's own MathML (and as a string for web pages). No DOM, no dependency. + * + * Nothing throws out of here. A command it does not know shows as its source in an `merror`, the rest is drawn; input + * it cannot make sense of (an unclosed brace, `\left` without `\right`, an unknown environment, too long or nested too + * deeply) gives the whole source in an `merror`. The TeX always travels along, in an `annotation`. + */ +import { dict } from '../dict' +import { ACCENT, BIG, BIG_CHARS, DELIM, DELIM_CHARS, FUNC, FUNC_LIMITS, funcText, INTEGRAL, NEGATED, OP, OP_CHAR, ORD, SPACE, styled, UPRIGHT, type Font } from './symbols' + +/** a MathML element (`x`: the text of a token element) */ +export interface MNode { + t: string + a?: Record + k?: MNode[] + x?: string + /** what the OMML writer needs to know and MathML does not say: never written out */ + n?: MNote +} + +export interface MNote { + /** a big operator: limits under / over in display style ('display'), always ('limits'), or beside ('side') */ + op?: 'display' | 'limits' | 'side' + /** a function name (sin, lim…): followed by an invisible function application */ + fn?: boolean + /** an mrow between \left and \right: the two delimiters ('' for none) */ + fence?: [string, string] + /** a \middle delimiter inside such an mrow */ + sep?: boolean +} + +/** the source is longer: shown as it is */ +export const MAX_TEX = 20_000 +/** groups inside one another, at most */ +const MAX_DEPTH = 40 +/** elements made, at most */ +const MAX_NODES = 50_000 + +class TexError extends Error {} + +const el = (t: string, k?: MNode[], a?: Record, n?: MNote): MNode => ({ t, ...(k ? { k } : {}), ...(a ? { a } : {}), ...(n ? { n } : {}) }) +const tok = (t: string, x: string, a?: Record, n?: MNote): MNode => ({ t, x, ...(a ? { a } : {}), ...(n ? { n } : {}) }) +const mo = (x: string, a?: Record, n?: MNote) => tok('mo', x, a, n) +/** text: a space at either end would be dropped, it is a no-break space */ +const mtext = (x: string, a?: Record) => tok('mtext', x.replace(/^\s|\s$/g, '\u00a0'), a) +/** several nodes as one (one alone is itself) */ +const row = (k: MNode[]): MNode => (k.length === 1 ? k[0] : el('mrow', k)) +const space = (em: number) => el('mspace', undefined, { width: `${+em.toFixed(4)}em` }) +/** the invisible function application after sin, lim… */ +const FN_APPLY = '\u2061' + +/** the mathcolor of a color name or #rgb / #rrggbb (also \color[HTML]{…} and [rgb]{r,g,b}), else undefined */ +function colorOf(spec: string, model?: string): string | undefined { + const v = spec.trim() + if (model === 'HTML' && /^[0-9a-f]{6}$/i.test(v)) return '#' + v.toLowerCase() + if (model === 'rgb' || model === 'RGB') { + const parts = v.split(',').map((x) => Number(x.trim())) + if (parts.length !== 3 || parts.some((x) => !Number.isFinite(x) || x < 0)) return undefined + const k = model === 'rgb' ? 255 : 1 + return '#' + parts.map((x) => Math.min(255, Math.round(x * k)).toString(16).padStart(2, '0')).join('') + } + if (/^[a-z]{1,20}$/i.test(v)) return v.toLowerCase() + if (/^#(?:[0-9a-f]{3}){1,2}$/i.test(v)) return v.toLowerCase() + return undefined +} + +/** a length (\hspace{1em}, \kern3mu) in em; undefined when it is not one */ +function lengthEm(spec: string): number | undefined { + const m = /^\s*(-?\d*\.?\d+)\s*(em|ex|mu|pt|px|pc|bp|cm|mm|in)?\s*$/.exec(spec) + if (!m) return undefined + const v = Number(m[1]) + const per: Record = { em: 1, ex: 0.43, mu: 1 / 18, pt: 0.1, px: 1 / 16, pc: 1.2, bp: 0.1, cm: 2.85, mm: 0.285, in: 7.23 } + return Math.max(-10, Math.min(100, v * (per[m[2] ?? 'em'] ?? 1))) +} + +/** environments: delimiters around, column alignment (repeated), display style inside, columns as pairs (aligned) */ +interface Env { + open?: string + close?: string + align?: string[] + display?: boolean + pairs?: boolean + small?: boolean + /** a column spec as the first argument (array) */ + spec?: boolean + /** a number as the first argument (alignat) */ + count?: boolean +} +const ENV: Record = dict({ + matrix: {}, pmatrix: { open: '(', close: ')' }, bmatrix: { open: '[', close: ']' }, Bmatrix: { open: '{', close: '}' }, + vmatrix: { open: '|', close: '|' }, Vmatrix: { open: '‖', close: '‖' }, smallmatrix: { small: true }, + 'matrix*': {}, 'pmatrix*': { open: '(', close: ')' }, 'bmatrix*': { open: '[', close: ']' }, 'Bmatrix*': { open: '{', close: '}' }, + 'vmatrix*': { open: '|', close: '|' }, 'Vmatrix*': { open: '‖', close: '‖' }, + cases: { open: '{', align: ['left'] }, dcases: { open: '{', align: ['left'], display: true }, rcases: { close: '}', align: ['left'] }, + aligned: { pairs: true, display: true }, align: { pairs: true, display: true }, 'align*': { pairs: true, display: true }, + alignat: { pairs: true, display: true, count: true }, 'alignat*': { pairs: true, display: true, count: true }, alignedat: { pairs: true, display: true, count: true }, + split: { pairs: true, display: true }, flalign: { pairs: true, display: true }, 'flalign*': { pairs: true, display: true }, + gathered: { display: true }, gather: { display: true }, 'gather*': { display: true }, multline: { display: true }, 'multline*': { display: true }, + equation: { display: true }, 'equation*': { display: true }, + eqnarray: { align: ['right', 'center', 'left'], display: true }, 'eqnarray*': { align: ['right', 'center', 'left'], display: true }, + array: { spec: true }, subarray: { spec: true, small: true }, +}) + +const FONT_CMD: Record = dict({ + mathrm: 'normal', mathup: 'normal', mathbf: 'bold', mathit: 'italic', mathbb: 'double-struck', mathcal: 'script', mathscr: 'script', + mathfrak: 'fraktur', mathsf: 'sans-serif', mathtt: 'monospace', boldsymbol: 'bold-italic', bm: 'bold-italic', mathbfit: 'bold-italic', + mathsfit: 'sans-serif-italic', Bbb: 'double-struck', frak: 'fraktur', +}) +const FONT_SWITCH: Record = dict({ rm: 'normal', bf: 'bold', it: 'italic', sf: 'sans-serif', tt: 'monospace', cal: 'script' }) +const TEXT_CMD: Record = dict({ text: undefined, textrm: undefined, textnormal: undefined, mbox: undefined, hbox: undefined, textup: undefined, textbf: 'bold', textit: 'italic', textsf: 'sans-serif', texttt: 'monospace', emph: 'italic' }) +const STYLE_SWITCH: Record> = dict({ + displaystyle: { displaystyle: 'true', scriptlevel: '0' }, textstyle: { displaystyle: 'false', scriptlevel: '0' }, + scriptstyle: { displaystyle: 'false', scriptlevel: '1' }, scriptscriptstyle: { displaystyle: 'false', scriptlevel: '2' }, +}) +/** \big… delimiter sizes */ +const BIG_SIZE: Record = dict({ big: '1.2em', Big: '1.623em', bigg: '2.047em', Bigg: '2.470em' }) +const ENCLOSE: Record = dict({ boxed: 'box', fbox: 'box', cancel: 'updiagonalstrike', bcancel: 'downdiagonalstrike', xcancel: 'updiagonalstrike downdiagonalstrike' }) +/** escapes that are their character */ +const ESCAPED: Record = dict({ '%': '%', $: '$', '#': '#', '&': '&', _: '_', '{': '{', '}': '}' }) + +const isLetter = (c: string) => /^\p{L}$/u.test(c) +const isDigit = (c: string | undefined) => c !== undefined && c >= '0' && c <= '9' + +class Parser { + private i = 0 + private depth = 0 + private made = 0 + private font: Font | null = null + /** display style where the parser is (limits of big operators go under / over) */ + private dstyle: boolean + /** \tag{…} */ + tag?: string + /** commands not known, for the error message */ + readonly unknown: string[] = [] + + constructor( + private s: string, + display: boolean, + ) { + this.dstyle = display + } + + private node(n: T): T { + if (++this.made > MAX_NODES) throw new TexError('Too many elements') + return n + } + + // ---------------------------------------------------------------- tokens + + /** spaces and % comments */ + private ws() { + const s = this.s + while (this.i < s.length) { + const c = s[this.i] + if (c === ' ' || c === '\t' || c === '\n' || c === '\r' || c === '\f') this.i++ + else if (c === '%') while (this.i < s.length && s[this.i] !== '\n') this.i++ + else break + } + } + + /** the next token, not taken: a command with its backslash ("\frac", "\,", "\\"), or one character */ + peek(): string | null { + this.ws() + const s = this.s + if (this.i >= s.length) return null + if (s[this.i] === '\\') { + let j = this.i + 1 + if (j >= s.length) throw new TexError('A backslash ends the text') + if (/[a-zA-Z]/.test(s[j])) while (j < s.length && /[a-zA-Z]/.test(s[j])) j++ + else j += s.codePointAt(j)! > 0xffff ? 2 : 1 + return s.slice(this.i, j) + } + return String.fromCodePoint(s.codePointAt(this.i)!) + } + + take(): string | null { + const t = this.peek() + if (t !== null) this.i += t.length + return t + } + + private expect(t: string) { + const got = this.take() + if (got !== t) throw new TexError(got === null ? `Missing ${t}` : `Expected ${t}, found ${got}`) + } + + /** a `*` right after a command name (\operatorname*) */ + private star(): boolean { + if (this.s[this.i] !== '*') return false + this.i++ + return true + } + + /** the text of a {…} argument as it is (braces inside kept), or of the next token */ + private raw(): string { + this.ws() + const s = this.s + if (s[this.i] !== '{') { + const t = this.take() + if (t === null) throw new TexError('Missing argument') + return t + } + let depth = 0 + const start = this.i + 1 + for (let j = this.i; j < s.length; j++) { + const c = s[j] + if (c === '\\') j++ + else if (c === '{') depth++ + else if (c === '}' && --depth === 0) { + this.i = j + 1 + return s.slice(start, j) + } + } + throw new TexError('Missing }') + } + + /** an optional [...] argument as text */ + private rawOpt(): string | undefined { + this.ws() + if (this.s[this.i] !== '[') return undefined + const e = this.s.indexOf(']', this.i) + if (e < 0) throw new TexError('Missing ]') + const v = this.s.slice(this.i + 1, e) + this.i = e + 1 + return v + } + + // ---------------------------------------------------------------- structure + + /** the whole formula: rows when it has \\ or & at the top */ + top(): MNode { + const rows = this.rows(() => false) + if (this.i < this.s.length) throw new TexError(`Unexpected ${this.peek()}`) + let body: MNode + if (rows.length === 1 && rows[0].length === 1) body = rows[0][0] + else body = this.table(rows, { pairs: rows.some((r) => r.length > 1), display: this.dstyle }) + if (this.tag !== undefined) body = el('mrow', [body, space(2), mtext(this.tag)]) + return body + } + + /** items up to `stop` (or the end of the group / text), the switches (\displaystyle, \color…) applied to what follows them */ + private expr(stop: (t: string) => boolean): MNode[] { + const out: MNode[] = [] + for (;;) { + const t = this.peek() + if (t === null || stop(t)) return out + switch (t) { + case '}': + throw new TexError('Unexpected }') + case '&': + throw new TexError('Misplaced &') + case '\\\\': + case '\\cr': + throw new TexError('Misplaced \\\\') + case '\\end': + throw new TexError('\\end without \\begin') + case '\\right': + throw new TexError('\\right without \\left') + case '\\over': + case '\\atop': + case '\\choose': { + this.take() + const den = this.expr(stop) + const f = this.node(el('mfrac', [row(out), row(den)], t === '\\over' ? undefined : { linethickness: '0' })) + return [t === '\\choose' ? el('mrow', [mo('('), f, mo(')')]) : f] + } + } + const name = t.slice(1) + if (t[0] === '\\' && STYLE_SWITCH[name]) { + this.take() + const saved = this.dstyle + this.dstyle = name === 'displaystyle' + const rest = this.expr(stop) + this.dstyle = saved + out.push(this.node(el('mstyle', rest, { ...STYLE_SWITCH[name] }))) + return out + } + if (t[0] === '\\' && FONT_SWITCH[name]) { + this.take() + const saved = this.font + this.font = FONT_SWITCH[name] + out.push(...this.expr(stop)) + this.font = saved + return out + } + if (t === '\\color') { + this.take() + const model = this.rawOpt() + const c = colorOf(this.raw(), model) + const rest = this.expr(stop) + out.push(c ? this.node(el('mstyle', rest, { mathcolor: c })) : row(rest)) + return out + } + const n = this.scripted() + if (n) out.push(n) + } + } + + /** cells and rows (& and \\) up to `end` */ + private rows(end: (t: string) => boolean): MNode[][] { + const rows: MNode[][] = [] + let cells: MNode[] = [] + for (;;) { + while (this.peek() === '\\hline' || this.peek() === '\\hdashline') this.take() + cells.push(row(this.expr((t) => t === '&' || t === '\\\\' || t === '\\cr' || end(t)))) + const t = this.peek() + if (t === '&') { + this.take() + continue + } + if (t === '\\\\' || t === '\\cr') { + this.take() + this.rawOpt() + rows.push(cells) + cells = [] + continue + } + rows.push(cells) + break + } + // a last \\ does not start a row + const last = rows[rows.length - 1] + if (rows.length > 1 && last.length === 1 && last[0].t === 'mrow' && !last[0].k?.length) rows.pop() + return rows + } + + private table(rows: MNode[][], env: Env, spec?: string[]): MNode { + const cols = Math.max(...rows.map((r) => r.length)) + const alignOf = (c: number) => (spec ? spec[c] ?? 'center' : env.pairs ? (c % 2 ? 'left' : 'right') : env.align ? env.align[c % env.align.length] : 'center') + const trs = rows.map((r) => + this.node( + el( + 'mtr', + Array.from({ length: cols }, (_, c) => { + let cell = r[c] ?? el('mrow', []) + // the right half of an aligned pair starts with a relation spaced as one (TeX puts {} there) + if (env.pairs && c % 2 && r[c]) cell = el('mrow', [el('mi', []), ...(cell.t === 'mrow' && !cell.n ? cell.k ?? [] : [cell])]) + const align = alignOf(c) + return this.node(el('mtd', [cell], align !== 'center' ? { columnalign: align } : undefined)) + }), + ), + ), + ) + const a: Record = {} + if (env.display) a.displaystyle = 'true' + if (env.small) a.scriptlevel = '1' + const aligns = Array.from({ length: cols }, (_, c) => alignOf(c)) + if (aligns.some((x) => x !== 'center')) a.columnalign = aligns.join(' ') + if (env.pairs) a.columnspacing = '0em 2em' + else if (env.align?.[0] === 'left' && cols > 1) a.columnspacing = '1em' + if (env.small) a.rowspacing = '0.1em' + return this.node(el('mtable', trs, Object.keys(a).length ? a : undefined)) + } + + // ---------------------------------------------------------------- atoms + + /** an atom with its scripts, limits and primes */ + private scripted(): MNode | null { + const t = this.peek() + const base = t === '^' || t === '_' || t === "'" ? null : this.atom() + let sub: MNode | undefined + let sup: MNode | undefined + let primes = 0 + let limits: boolean | undefined + for (;;) { + const s = this.peek() + if (s === '\\limits' || s === '\\nolimits') { + this.take() + limits = s === '\\limits' + } else if (s === "'") { + this.take() + primes++ + } else if (s === '^' || s === '_') { + this.take() + if ((s === '^' ? sup : sub) !== undefined) throw new TexError(s === '^' ? 'Double superscript' : 'Double subscript') + const saved = this.dstyle + this.dstyle = false + const a = this.arg() + this.dstyle = saved + if (s === '^') sup = a + else sub = a + } else break + } + if (primes) { + const p = mo(primes <= 4 ? ['′', '″', '‴', '⁗'][primes - 1] : '′'.repeat(primes)) + sup = sup ? el('mrow', [p, sup]) : p + } + if (!sub && !sup) return base && base.n?.fn ? this.apply(base) : base + const b = base ?? el('mrow', []) + const op = b.n?.op + const under = op === 'limits' || (op !== undefined && limits !== false && (limits === true || (op === 'display' && this.dstyle))) + if (under && limits && b.t === 'mo') b.a = { ...b.a, movablelimits: 'false' } + let n: MNode + if (under) n = sub && sup ? el('munderover', [b, sub, sup]) : sub ? el('munder', [b, sub]) : el('mover', [b, sup!]) + else n = sub && sup ? el('msubsup', [b, sub, sup]) : sub ? el('msub', [b, sub]) : el('msup', [b, sup!]) + this.node(n) + return b.n?.fn ? this.apply(n) : n + } + + /** a function name and the invisible function application after it, spaced as TeX spaces it from what follows (not from a bracket) */ + private apply(fn: MNode): MNode { + const next = this.peek() + const tight = next === null || next === '(' || next === '[' || next === '|' || next === '\\left' || next === '}' || next === '&' || next === '\\\\' || next === '\\{' || next === '\\langle' + return el('mrow', [fn, mo(FN_APPLY, tight ? undefined : { rspace: '0.1667em' })]) + } + + /** an argument: a {group} or one token */ + private arg(): MNode { + const t = this.peek() + if (t === null || t === '}' || t === '&' || t === '^' || t === '_' || t === '\\\\' || t === '\\end' || t === '\\right') throw new TexError('Missing argument') + // one digit is one token: \frac12, x^23 (the 3 is not in the script) + if (isDigit(t)) { + this.take() + const f = this.font + return this.node(tok('mn', f && f !== 'normal' && f !== 'italic' ? styled(t, f) : t)) + } + return this.atom() ?? el('mrow', []) + } + + private atom(): MNode | null { + if (++this.depth > MAX_DEPTH) throw new TexError('Nested too deeply') + try { + const t = this.take()! + if (t === '{') { + const k = this.expr((x) => x === '}') + this.expect('}') + return this.node(k.length === 1 ? k[0] : el('mrow', k)) + } + if (t[0] === '\\' && t.length > 1) return this.command(t.slice(1)) + return this.char(t) + } finally { + this.depth-- + } + } + + /** a letter in the current font */ + private letter(c: string, upright = false): MNode { + const f = this.font + if (f === 'normal' || (!f && upright)) return this.node(tok('mi', c, { mathvariant: 'normal' })) + return this.node(tok('mi', f ? styled(c, f) : c)) + } + + private char(c: string): MNode { + const s = this.s + if (isDigit(c) || (c === '.' && isDigit(s[this.i]))) { + let num = c + while (this.i < s.length && (isDigit(s[this.i]) || (s[this.i] === '.' && isDigit(s[this.i + 1])))) num += s[this.i++] + const f = this.font + return this.node(tok('mn', f && f !== 'normal' && f !== 'italic' ? [...num].map((d) => styled(d, f)).join('') : num)) + } + if (isLetter(c)) { + // \mathrm{max}: one word + if (this.font === 'normal') { + let word = c + while (this.i < s.length && /[a-zA-Z]/.test(s[this.i])) word += s[this.i++] + if (word.length > 1) return this.node(tok('mi', word)) + } + return this.letter(c) + } + switch (c) { + case '~': + return this.node(mtext('\u00a0')) + case '#': + case '$': + throw new TexError(`Unexpected ${c}`) + case '(': + case ')': + case '[': + case ']': + case '|': + return this.node(mo(c, { stretchy: 'false' })) + } + if (BIG_CHARS.has(c)) return this.node(mo(c, undefined, { op: INTEGRAL.has(c) ? 'side' : 'display' })) + return this.node(mo(OP_CHAR[c] ?? c)) + } + + /** a delimiter after \left, \right, \big… ('' for `.`) */ + private delim(): string { + const t = this.take() + if (t === null) throw new TexError('Missing delimiter') + if (t === '.') return '' + if (t[0] === '\\' && t.length > 1) { + const d = DELIM[t.slice(1)] + if (d !== undefined) return d + } else if (DELIM_CHARS.has(t)) return t === '<' ? '⟨' : t === '>' ? '⟩' : t + throw new TexError(`Not a delimiter: ${t}`) + } + + private command(name: string): MNode | null { + const ord = ORD[name] + if (ord !== undefined) return this.letter(ord) + const up = UPRIGHT[name] + if (up !== undefined) return this.letter(up, true) + if (/^var[A-Z]/.test(name) && UPRIGHT[name.slice(3)] !== undefined) return this.node(tok('mi', styled(UPRIGHT[name.slice(3)], 'italic'))) + const op = OP[name] + if (op !== undefined) return this.node(mo(op)) + const big = BIG[name] + if (big !== undefined) return this.node(mo(big, undefined, { op: INTEGRAL.has(big) ? 'side' : 'display' })) + if (FUNC.has(name) || FUNC_LIMITS.has(name)) return this.node(tok('mi', funcText(name), undefined, { fn: true, ...(FUNC_LIMITS.has(name) ? { op: 'display' } : {}) })) + const sp = SPACE[name] + if (sp !== undefined) return this.node(space(sp)) + if (ESCAPED[name] !== undefined) return this.node(name === '_' || name === '#' || name === '%' || name === '&' ? tok('mi', ESCAPED[name]) : mo(ESCAPED[name], { stretchy: 'false' })) + const d = DELIM[name] + if (d !== undefined) return this.node(mo(d, { stretchy: 'false' })) + const acc = ACCENT[name] + if (acc) { + const body = this.arg() + const mark = mo(acc.c, acc.wide ? { stretchy: 'true' } : { stretchy: 'false' }) + const n = acc.under ? el('munder', [body, mark], acc.accent ? { accentunder: 'true' } : undefined) : el('mover', [body, mark], acc.accent ? { accent: 'true' } : undefined) + // a brace takes its label as a limit: \overbrace{x}^{n} + if (name === 'overbrace' || name === 'underbrace' || name === 'overgroup' || name === 'undergroup') n.n = { op: 'limits' } + return this.node(n) + } + const font = FONT_CMD[name] + if (font) { + const saved = this.font + this.font = font + try { + return this.arg() + } finally { + this.font = saved + } + } + if (name in TEXT_CMD) return this.text(this.raw(), TEXT_CMD[name]) + const size = BIG_SIZE[name.replace(/[lrm]$/, '')] + if (size) { + const c = this.delim() + return this.node(mo(c || '\u200b', { minsize: size, maxsize: size, stretchy: 'true', symmetric: 'true' })) + } + switch (name) { + case 'frac': + case 'dfrac': + case 'tfrac': + case 'cfrac': + case 'binom': + case 'dbinom': + case 'tbinom': { + const saved = this.dstyle + this.dstyle = false + const num = this.arg() + const den = this.arg() + this.dstyle = saved + const binom = name.endsWith('binom') + let f: MNode = this.node(el('mfrac', [num, den], binom ? { linethickness: '0' } : undefined)) + if (binom) f = el('mrow', [mo('('), f, mo(')')]) + if (name[0] === 'd' || name === 'cfrac') return this.node(el('mstyle', [f], { displaystyle: 'true', scriptlevel: '0' })) + if (name[0] === 't') return this.node(el('mstyle', [f], { displaystyle: 'false', scriptlevel: '0' })) + return f + } + case 'sqrt': { + this.ws() + let index: MNode | undefined + if (this.s[this.i] === '[') { + this.i++ + index = row(this.expr((t) => t === ']')) + this.expect(']') + } + const body = this.arg() + return this.node(index ? el('mroot', [body, index]) : el('msqrt', [body])) + } + case 'left': { + const open = this.delim() + const body = this.expr((t) => t === '\\right') + this.expect('\\right') + const close = this.delim() + const fence = (c: string) => mo(c, { fence: 'true', stretchy: 'true', symmetric: 'true' }) + return this.node(el('mrow', [...(open ? [fence(open)] : []), ...body, ...(close ? [fence(close)] : [])], undefined, { fence: [open, close] })) + } + case 'middle': + return this.node(mo(this.delim() || '\u200b', { stretchy: 'true', symmetric: 'true' }, { sep: true })) + case 'operatorname': + case 'operatornamewithlimits': { + const limits = this.star() || name === 'operatornamewithlimits' + const saved = this.font + this.font = 'normal' + const body = this.arg() + this.font = saved + const text = textOf(body) + const n = /^[\p{L}\p{N} ]+$/u.test(text) ? tok('mi', text, text.length === 1 ? { mathvariant: 'normal' } : undefined) : body + n.n = { fn: true, ...(limits ? { op: 'display' } : {}) } + return this.node(n) + } + case 'mathop': { + const body = this.arg() + const n = body.t === 'mo' || body.t === 'mi' ? { ...body } : el('mrow', [body]) + n.n = { ...n.n, op: 'display' } + return this.node(n) + } + case 'mathbin': + case 'mathrel': + case 'mathord': + case 'mathpunct': + case 'mathopen': + case 'mathclose': + case 'mathinner': + return this.arg() + case 'begin': + return this.env() + case 'textcolor': { + const model = this.rawOpt() + const c = colorOf(this.raw(), model) + const body = this.arg() + return c ? this.node(el('mstyle', [body], { mathcolor: c })) : body + } + case 'colorbox': + case 'fcolorbox': { + if (name === 'fcolorbox') this.raw() + this.raw() + return this.text(this.raw()) + } + case 'not': { + const next = this.peek() + if (next === null || next === '}') return this.node(mo('/')) + const n = this.atom() + if (n && (n.t === 'mo' || n.t === 'mi') && n.x) { + const neg = NEGATED[n.x] + return this.node({ ...n, t: 'mo', x: neg ?? n.x + '\u0338' }) + } + return n + } + case 'bmod': + case 'mod': + return this.node(el('mrow', [space(name === 'mod' ? 0.6667 : 0.2222), tok('mi', 'mod'), space(name === 'mod' ? 0.3333 : 0.2222)])) + case 'pmod': + case 'pod': { + const body = this.arg() + return this.node(el('mrow', [space(0.6667), mo('('), ...(name === 'pmod' ? [tok('mi', 'mod'), space(0.3333)] : []), body, mo(')')])) + } + case 'phantom': + return this.node(el('mphantom', [this.arg()])) + case 'hphantom': + return this.node(el('mpadded', [el('mphantom', [this.arg()])], { height: '0', depth: '0' })) + case 'vphantom': + return this.node(el('mpadded', [el('mphantom', [this.arg()])], { width: '0' })) + case 'smash': + this.rawOpt() + return this.node(el('mpadded', [this.arg()], { height: '0', depth: '0' })) + case 'overset': + case 'underset': + case 'stackrel': { + const saved = this.dstyle + this.dstyle = false + const over = this.arg() + this.dstyle = saved + const body = this.arg() + return this.node(el(name === 'underset' ? 'munder' : 'mover', [body, over])) + } + case 'xrightarrow': + case 'xleftarrow': + case 'xLeftarrow': + case 'xRightarrow': + case 'xleftrightarrow': + case 'xLeftrightarrow': + case 'xmapsto': { + this.ws() + let below: MNode | undefined + if (this.s[this.i] === '[') { + this.i++ + below = row(this.expr((t) => t === ']')) + this.expect(']') + } + const above = this.arg() + const arrow = mo({ xrightarrow: '→', xleftarrow: '←', xLeftarrow: '⇐', xRightarrow: '⇒', xleftrightarrow: '↔', xLeftrightarrow: '⇔', xmapsto: '↦' }[name]!, { stretchy: 'true', minsize: '2em' }) + return this.node(below ? el('munderover', [arrow, below, above]) : el('mover', [arrow, above])) + } + case 'substack': { + this.ws() + if (this.s[this.i] !== '{') throw new TexError('Missing argument') + this.i++ + const rows = this.rows((t) => t === '}') + this.expect('}') + return this.table(rows, { small: true }) + } + case 'tag': { + const star = this.star() + const v = this.raw() + this.tag = star ? v : `(${v})` + return null + } + case 'label': + this.raw() + return null + case 'nonumber': + case 'notag': + case 'limits': + case 'nolimits': + case 'relax': + case 'strut': + case 'mathstrut': + return null + case 'hspace': + case 'mspace': + case 'hskip': + case 'kern': + case 'mkern': + case 'mskip': { + this.star() + this.ws() + let spec: string + if (this.s[this.i] === '{') spec = this.raw() + else { + const m = /^\s*-?\d*\.?\d+\s*[a-z]{2}/.exec(this.s.slice(this.i, this.i + 40)) + if (!m) throw new TexError(`Missing length after \\${name}`) + spec = m[0] + this.i += m[0].length + } + const em = lengthEm(spec) + if (em === undefined) throw new TexError(`Not a length: ${spec}`) + return this.node(space(em)) + } + } + const enc = ENCLOSE[name] + if (enc) return this.node(el('menclose', [this.arg()], { notation: enc })) + // not known: shown as written, the rest goes on + this.unknown.push('\\' + name) + return this.node(el('merror', [mtext('\\' + name)])) + } + + /** \text{…}: words, with $…$ math inside */ + private text(src: string, variant?: string): MNode { + const parts: MNode[] = [] + let buf = '' + const flush = () => { + if (buf) parts.push(this.node(mtext(buf, variant ? { mathvariant: variant } : undefined))) + buf = '' + } + for (let j = 0; j < src.length; j++) { + const c = src[j] + if (c === '\\') { + const m = /^\\([a-zA-Z]+|.)/s.exec(src.slice(j, j + 40)) + const cmd = m?.[1] ?? '' + j += cmd.length + if (ESCAPED[cmd] !== undefined) buf += ESCAPED[cmd] + else if (cmd === ' ' || cmd === ',' || cmd === ';' || cmd === ':') buf += cmd === ' ' ? ' ' : '\u2009' + else if (cmd === 'quad' || cmd === 'qquad') buf += cmd === 'quad' ? '\u2003' : '\u2003\u2003' + else if (cmd === 'textbackslash') buf += '\\' + else if (cmd === '\\') buf += ' ' + // \textbf{…} and the like inside: their text + } else if (c === '$') { + const e = src.indexOf('$', j + 1) + if (e < 0) throw new TexError('Unclosed $ in \\text') + flush() + const inner = new Parser(src.slice(j + 1, e), false) + inner.depth = this.depth + 1 + if (inner.depth > MAX_DEPTH) throw new TexError('Nested too deeply') + parts.push(inner.top()) + this.made += inner.made + j = e + } else if (c === '~') buf += '\u00a0' + else if (c !== '{' && c !== '}') buf += c + } + flush() + return parts.length === 1 ? parts[0] : this.node(el('mrow', parts)) + } + + private env(): MNode { + const name = this.raw().trim() + const env = ENV[name] + if (!env) throw new TexError(`Unknown environment ${name}`) + let spec: string[] | undefined + if (env.spec) { + spec = [] + const src = this.raw().replace(/@\{[^}]*\}|\*\{[^}]*\}\{[^}]*\}|[|:]/g, '') + for (const c of src.replace(/[pmb]\{[^}]*\}/g, 'l')) if (c === 'l' || c === 'c' || c === 'r') spec.push(c === 'l' ? 'left' : c === 'r' ? 'right' : 'center') + } else if (env.count) this.raw() + else if (name.endsWith('matrix*')) this.rawOpt() + const saved = this.dstyle + this.dstyle = !!env.display + const rows = this.rows((t) => t === '\\end') + this.dstyle = saved + this.expect('\\end') + const end = this.raw().trim() + if (end !== name) throw new TexError(`\\begin{${name}} ended by \\end{${end}}`) + const t = this.table(rows, env, spec) + if (!env.open && !env.close) return t + const fence = (c: string) => mo(c, { fence: 'true', stretchy: 'true', symmetric: 'true' }) + return this.node(el('mrow', [...(env.open ? [fence(env.open)] : []), t, ...(env.close ? [fence(env.close)] : [])], undefined, { fence: [env.open ?? '', env.close ?? ''] })) + } +} + +/** the text of a node's tokens */ +export function textOf(n: MNode): string { + return n.x ?? (n.k ?? []).map(textOf).join('') +} + +export interface TexMathml { + /** the `math` element */ + node: MNode + /** why the formula is not drawn (the source is shown), or the commands not known (shown as written) */ + error?: string +} + +/** + * MathML of a TeX formula: a `math` element (block for display math) whose `semantics` holds the drawing and the TeX as + * an `application/x-tex` annotation. Never throws. + */ +export function texToMathml(tex: string, display: boolean): TexMathml { + const ann = tok('annotation', tex, { encoding: 'application/x-tex' }) + const wrap = (body: MNode) => el('math', [el('semantics', [body, ann])], display ? { display: 'block' } : undefined) + const fail = (why: string): TexMathml => ({ node: wrap(el('merror', [tok('mtext', tex.trim() || ' ')])), error: why }) + if (tex.length > MAX_TEX) return fail(`Longer than ${MAX_TEX.toLocaleString('en-US')} characters`) + try { + const p = new Parser(tex, display) + const body = p.top() + return { node: wrap(body), ...(p.unknown.length ? { error: `Not supported: ${[...new Set(p.unknown)].join(', ')}` } : {}) } + } catch (e) { + // a TexError, or anything else (a stack overflow): the source, whole + return fail(e instanceof TexError ? e.message : 'Cannot be read') + } +} + +const escText = (s: string) => s.replace(/&/g, '&').replace(//g, '>') +const escAttr = (s: string) => escText(s).replace(/"/g, '"') + +/** a MathML tree as markup; `ns`: the root says its namespace (a standalone page, the clipboard) */ +export function mathmlString(n: MNode, ns = false): string { + let a = ns && n.t === 'math' ? ' xmlns="http://www.w3.org/1998/Math/MathML"' : '' + for (const [k, v] of Object.entries(n.a ?? {})) a += ` ${k}="${escAttr(v)}"` + if (n.x !== undefined) return `<${n.t}${a}>${escText(n.x)}` + if (n.t === 'mspace' || n.t === 'none') return `<${n.t}${a}/>` + return `<${n.t}${a}>${(n.k ?? []).map((k) => mathmlString(k)).join('')}` +} + +/** the MathML markup of a TeX formula (see texToMathml) */ +export const texMathml = (tex: string, display: boolean, ns = true) => mathmlString(texToMathml(tex, display).node, ns) diff --git a/src/core/plain.ts b/src/core/plain.ts index 6583d30..ac08c61 100644 --- a/src/core/plain.ts +++ b/src/core/plain.ts @@ -4,7 +4,7 @@ * are drawn too). A gzip-compressed plain file (.svgz, .csv.gz) is unpacked first. * Used by the engine (a source that is not an Office package) and by File → Open. */ -import { detectSeparator, markdownToHtml, parasToDocx, parseCsv, rowsToXlsx, textToParas, type ImportPara } from './convert' +import { detectSeparator, markdownToHtml, parasToDocx, parseCsv, rowsToXlsx, textToParas, type ImportPara, type ImportRun } from './convert' import { DIAGRAM_EXT, DIAGRAM_NAME, FENCE_LANG, renderDiagrams, sniffDiagram, type DiagramLang } from './diagram' import { decodeEntities, htmlToParas } from './html' import { BlobSource, type ByteSource } from './source' @@ -264,7 +264,7 @@ const fromHtml = (html: string, dom = true): ImportPara[] => htmlToParas(html, dom).map((p) => ({ heading: p.heading, list: p.list, - runs: p.runs.flatMap((r) => (r.t === 'text' ? [{ text: r.text, set: r.d, href: r.href }] : [])), + runs: p.runs.flatMap((r): ImportRun[] => (r.t === 'text' ? [{ text: r.text, set: r.d, href: r.href }] : r.t === 'ref' && r.math ? [{ text: '', math: r.math }] : [])), })) /** a diagram's pictures, or (when it cannot be drawn) why, and its source */ diff --git a/src/core/plainmerge.ts b/src/core/plainmerge.ts index ffe012b..d3429f3 100644 --- a/src/core/plainmerge.ts +++ b/src/core/plainmerge.ts @@ -5,7 +5,7 @@ * knowing the paragraphs it imported as; the edited document is aligned with the imported paragraphs, a chunk whose * paragraphs all came back unchanged is kept verbatim, and only what was edited is written from the model. */ -import type { Block, Inline, Paragraph } from './docx/types' +import type { Block, Inline, Paragraph, TexMath } from './docx/types' import type { ImportPara } from './convert' import { htmlParas, markdownParas } from './plain' @@ -37,11 +37,15 @@ function runsKey(parts: { key: string; text: string }[], heading: boolean): stri const norm = (c: string | undefined) => (c ?? '').replace('#', '').toLowerCase() +/** a formula: its TeX as written, whatever its look */ +const mathPart = (m: TexMath) => ({ key: 'm', text: m.delim + m.tex + m.delim }) + /** what the paragraph reads and looks like, for telling an edited one from one that came back as imported */ function importSig(p: ImportPara): string { if (p.image) return 'IMG' const link = (r: { href?: string }) => (r.href ? 'a' + r.href : '') const parts = p.runs.map((r) => { + if (r.math) return mathPart(r.math) const s = r.set ?? {} const href = link(r) const key = [s.b ? 'b' : '', s.i ? 'i' : '', s.strike ? 's' : '', !href && s.u && s.u !== 'none' ? 'u' : '', s.font && MONO.test(s.font) ? 'c' : '', href, !href && s.color ? 'k' + norm(s.color) : '', s.hl ? 'h' + norm(s.hl) : ''].join('') @@ -65,6 +69,10 @@ function blockSig(b: Block): string { continue } if ((r.t !== 'text' && r.t !== 'ref') || r.tc?.k === 'del' || r.s.vanish) continue + if (r.t === 'ref' && r.math) { + parts.push(mathPart(r.math)) + continue + } const href = r.t === 'text' && r.href ? 'a' + r.href : '' const s = r.s const key = [s.b ? 'b' : '', s.i ? 'i' : '', s.strike || s.dstrike ? 's' : '', !href && s.u && s.u !== 'none' ? 'u' : '', s.font && MONO.test(s.font) ? 'c' : '', href, !href && s.color && s.color !== 'auto' ? 'k' + norm(s.color) : '', s.hl && s.hl !== 'none' ? 'h' + norm(s.hl) : ''].join('') diff --git a/src/react/docx/DocxEditor.ts b/src/react/docx/DocxEditor.ts index d292804..ec36f62 100644 --- a/src/react/docx/DocxEditor.ts +++ b/src/react/docx/DocxEditor.ts @@ -984,7 +984,8 @@ export class DocxEditor implements RichHost { /** * Copy / cut of text: the clipboard gets the paragraphs as the model has them (list items, headings, runs), not the - * page's DOM (which holds the list markers as text and the colors as computed). Pictures and ranges across a table are + * page's DOM (which holds the list markers as text and the colors as computed). TeX formulas go as MathML holding + * their TeX (pasted back as formulas) and as their source in the plain text. Pictures and ranges across a table are * left to the browser. */ copy(sel: Sel, data: DataTransfer, cut: boolean): boolean { @@ -995,7 +996,7 @@ export class DocxEditor implements RichHost { const p = this.para(pa)! return { ...p, runs: sliceRunsOf(p, i === 0 ? s.o : 0, i === pas.length - 1 ? e.o : plen(p.runs as RRun[])) } }) - if (parts.some((p) => p.runs.some((r) => r.t !== 'text'))) return false + if (parts.some((p) => p.runs.some((r) => r.t !== 'text' && !(r.t === 'ref' && r.math)))) return false data.setData('text/html', docxBodyHtml(parts)) data.setData('text/plain', docxToText(parts).replace(/\n$/, '')) if (cut) opDelete(this, sel) diff --git a/src/react/docx/render.tsx b/src/react/docx/render.tsx index 932b232..b9dd142 100644 --- a/src/react/docx/render.tsx +++ b/src/react/docx/render.tsx @@ -1,6 +1,7 @@ -import { memo, type CSSProperties, type PointerEvent as RPointerEvent, type ReactNode } from 'react' +import { createElement, memo, useMemo, type CSSProperties, type PointerEvent as RPointerEvent, type ReactElement, type ReactNode } from 'react' import { cssColor } from '../../core/color' -import type { Block, Inline, Paragraph, ParaStyle, RunStyle, Table, TrackMark } from '../../core/docx/types' +import type { Block, Inline, Paragraph, ParaStyle, RunStyle, Table, TexMath, TrackMark } from '../../core/docx/types' +import { texToMathml, type MNode } from '../../core/math/tex' import { safeHref } from '../../core/url' import type { DocumentClient } from '../../core/worker/client' import { useMediaUrl } from '../common/hooks' @@ -119,6 +120,25 @@ function Img({ ctx, target, w, h, alt }: { ctx: RenderCtx; target: string; w: nu return {alt } +/** a MathML tree as elements (React puts them in the MathML namespace under `math`) */ +function mathEl(n: MNode, key?: number): ReactElement { + return createElement(n.t, { key, ...n.a }, n.x ?? n.k?.map(mathEl)) +} + +/** + * A TeX formula, drawn by the browser from its MathML (selectable, read by screen readers, printed); the TeX rides + * along as an annotation. A formula that cannot be drawn shows its source, its reason as the tooltip. + */ +function MathView({ math, sz, attrs }: { math: TexMath; sz?: number; attrs: { title?: string } }) { + const display = math.delim === '$$' + const { node, error } = useMemo(() => texToMathml(math.tex, display), [math.tex, display]) + return ( + + {mathEl(node)} + + ) +} + function InlineView({ x, ctx, o }: { x: Inline; ctx: RenderCtx; o: number }): ReactNode { // an object inside a tracked change: outlined in the author's colour (dimmed when deleted) const objTc = x.t !== 'text' ? x.tc : undefined @@ -160,6 +180,7 @@ function InlineView({ x, ctx, o }: { x: Inline; ctx: RenderCtx; o: number }): Re ) } case 'ref': + if (x.math) return return ( {x.text} diff --git a/src/react/file/text.ts b/src/react/file/text.ts index 980ca36..f5dc8ee 100644 --- a/src/react/file/text.ts +++ b/src/react/file/text.ts @@ -1,12 +1,15 @@ import { cssColor } from '../../core/color' import type { Block, Inline, Paragraph, RunStyle, Table } from '../../core/docx/types' +import { findMath, mathSource } from '../../core/math/scan' +import { texMathml } from '../../core/math/tex' import type { SlideModel, TextBody } from '../../core/pptx/types' import { safeHref } from '../../core/url' // ---------------------------------------------------------------- DOCX -/** text as it reads with its tracked changes accepted: deleted text is left out */ -const plain = (runs: Inline[]) => runs.map((r) => (r.t === 'text' || r.t === 'ref' ? (r.s.vanish || r.tc?.k === 'del' ? '' : r.text) : '')).join('') +/** text as it reads with its tracked changes accepted: deleted text is left out; a TeX formula is its source */ +const plain = (runs: Inline[]) => + runs.map((r) => (r.t === 'text' || r.t === 'ref' ? (r.s.vanish || r.tc?.k === 'del' ? '' : r.t === 'ref' && r.math ? mathSource(r.math) : r.text) : '')).join('') const cellText = (blocks: Block[]) => blocks @@ -26,7 +29,31 @@ export function docxToText(blocks: Block[]): string { return out.join('\n') + '\n' } -const mdEsc = (s: string) => s.replace(/([\\`*_[\]<>~])/g, '\\$1') +/** (a dollar sign is marked \u0003: escaped or not once the whole paragraph is known, see dollars) */ +const mdEsc = (s: string) => s.replace(/([\\`*_[\]<>~])/g, '\\$1').replace(/\$/g, '\u0003') + +/** + * A paragraph's Markdown with its formulas put in (marked \u0004n\u0004) and its dollar signs (\u0003) escaped only when, + * left as they are, the text would not read back as the same formulas (`costs $5` stays as it is). + */ +function dollars(out: string, maths: string[]): string { + const put = (s: string) => (maths.length ? s.replace(/\u0004(\d+)\u0004/g, (_, k: string) => maths[Number(k)]) : s) + if (!out.includes('\u0003')) return put(out) + const want: [number, number][] = [] + let read = '' + for (const part of out.split(/(\u0004\d+\u0004)/)) { + const k = /^\u0004(\d+)\u0004$/.exec(part) + if (!k) read += part.replace(/\u0003/g, '$') + else { + const src = maths[Number(k[1])] + want.push([read.length, read.length + src.length]) + read += src + } + } + const found = findMath(read) + const same = found.length === want.length && found.every((f, x) => f.start === want[x][0] && f.end === want[x][1]) + return put(out.replace(/\u0003/g, same ? '$' : '\\$')) +} /** a paragraph that would read as a heading, quote, list item or rule given its first characters: the first one escaped */ const mdLead = (s: string) => s.replace(/^(\s*)(#{1,6}(?=\s)|>|[-+*](?=\s)|\d+(?=[.)]\s)|(?:[-*_]\s*){3,}$)/, (_, sp: string, m: string) => (/^\d/.test(m) ? `${sp}${m}\\` : `${sp}\\${m}`)) @@ -41,6 +68,7 @@ export type Pictures = Map function mdInline(runs: Inline[], heading = false, images?: Pictures): string { let out = '' let i = 0 + const maths: string[] = [] while (i < runs.length) { const r = runs[i] if (r.t === 'img' && r.tc?.k !== 'del' && images?.has(r.target)) { @@ -48,6 +76,12 @@ function mdInline(runs: Inline[], heading = false, images?: Pictures): string { out += `![${mdEsc(r.alt ?? '')}](${images.get(r.target)})` continue } + // a TeX formula: as it was written + if (r.t === 'ref' && r.math && r.tc?.k !== 'del') { + i++ + out += `\u0004${maths.push(mathSource(r.math)) - 1}\u0004` + continue + } if ((r.t !== 'text' && r.t !== 'ref') || r.tc?.k === 'del') { i++ continue @@ -79,7 +113,7 @@ function mdInline(runs: Inline[], heading = false, images?: Pictures): string { const body = r.s.font && MONO.test(r.s.font) ? `\`${core.replace(/`/g, '``')}\`` : mdEsc(core) out += `${lead}${strike}${mark}${body}${[...mark].reverse().join('')}${strike}${trail}` } - return out.replace(/\*\*\*\*/g, '') + return dollars(out.replace(/\*\*\*\*/g, ''), maths) } function mdTable(t: Table): string { @@ -170,6 +204,11 @@ function htmlInline(runs: Inline[], heading = false, images?: Pictures): string out += safeHref(r.href) ? `${img}` : img continue } + // a TeX formula: MathML, the TeX in an annotation + if (r.t === 'ref' && r.math) { + out += texMathml(r.math.tex, r.math.delim === '$$') + continue + } if (r.t !== 'text' && r.t !== 'ref') continue if (r.s.vanish) continue if (r.t === 'text' && safeHref(r.href)) { @@ -198,7 +237,10 @@ const cellHtml = (blocks: Block[], images?: Pictures) => const HTML_CSS = 'body{max-width:48em;margin:2em auto;padding:0 1em;font:16px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;color:#222}' + 'table{border-collapse:collapse}td{border:1px solid #ccc;padding:4px 8px;vertical-align:top}img{max-width:100%;height:auto}' + - 'code{font-family:ui-monospace,Menlo,Consolas,monospace;background:#f3f3f3;padding:0 .2em}' + 'code{font-family:ui-monospace,Menlo,Consolas,monospace;background:#f3f3f3;padding:0 .2em}' + + // MathML table columns in browsers that read only CSS for them (Chromium) + 'mtd[columnalign=left]{text-align:left}mtd[columnalign=right]{text-align:right}mtable[columnspacing="0em 2em"]>mtr>mtd{padding-left:0;padding-right:0}' + + 'mtable[columnspacing="0em 2em"]>mtr>mtd[columnalign=right]:not(:first-child){padding-left:2em}merror{border:0;background:none;color:#a33;font-family:ui-monospace,Menlo,Consolas,monospace}' /** * A web page of the document: headings (outline levels), paragraphs, nested lists, bold / italic / underline / strike, diff --git a/src/react/styles.css b/src/react/styles.css index 151703f..9f1758d 100644 --- a/src/react/styles.css +++ b/src/react/styles.css @@ -424,6 +424,16 @@ padding: 3.6pt 7.2pt; overflow: hidden; } +/* TeX math ($…$, $$…$$ in Markdown): MathML drawn by the browser */ +.de-p > span.de-math { white-space: normal; text-indent: 0; } +.de-math[data-display] { display: block; } +.de-math merror { border: 0; background: none; color: var(--de-status-error, #d93025); font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.85em; } +/* table columns (aligned, cases, array) where the browser reads only CSS for them (Chromium) */ +.de-math mtd[columnalign='left'] { text-align: left; } +.de-math mtd[columnalign='right'] { text-align: right; } +.de-math mtable[columnspacing='0em 2em'] > mtr > mtd { padding-left: 0; padding-right: 0; } +.de-math mtable[columnspacing='0em 2em'] > mtr > mtd[columnalign='right']:not(:first-child) { padding-left: 2em; } +.de-math mtable[columnspacing='1em'] > mtr > mtd + mtd { padding-left: 1em; } /* ---------------------------------------------------------------- xlsx */ .de-xlsx { diff --git a/test/markdown-math.test.ts b/test/markdown-math.test.ts new file mode 100644 index 0000000..79af2e8 --- /dev/null +++ b/test/markdown-math.test.ts @@ -0,0 +1,317 @@ +/** + * TeX math in Markdown (#33): which `$` are math, the formulas as equations of the document (the TeX and its + * delimiters with them), saved back as written, in the Word file, the text / web page exports and the clipboard. + */ +import { describe, expect, test } from 'bun:test' +import { markdownToHtml } from '../src/core/convert' +import { paraSpec } from '../src/core/docx/spec' +import type { Block, EditResult, Inline, Paragraph } from '../src/core/docx/types' +import { htmlToParas } from '../src/core/html' +import { displayEnd, findMath, mathSource } from '../src/core/math/scan' +import { mergeSource } from '../src/core/plainmerge' +import { BlobSource } from '../src/core/source' +import { Engine } from '../src/core/worker/engine' +import { ZipArchive } from '../src/core/zip' +import { docxBodyHtml, docxToHtml, docxToMarkdown, docxToText } from '../src/react/file/text' +import * as markdown from '../packages/markdown/src/index' + +const spans = (s: string) => findMath(s).map((m) => [m.delim, m.tex]) + +describe('which dollars are math', () => { + test('inline $…$ and display $$…$$, also over lines', () => { + expect(spans('Euler: $e^{i\\pi} + 1 = 0$.')).toEqual([['$', 'e^{i\\pi} + 1 = 0']]) + expect(spans('$$x^2$$ and $$\n\\frac{a}{b}\n$$')).toEqual([['$$', 'x^2'], ['$$', '\n\\frac{a}{b}\n']]) + expect(spans('a $x\ny$ b')).toEqual([['$', 'x\ny']]) + expect(spans('$a$$b$')).toEqual([['$', 'a'], ['$', 'b']]) + expect(findMath('x $a$ y')[0]).toMatchObject({ start: 2, end: 5 }) + }) + + test('currency and other dollars are text (pandoc rules)', () => { + expect(spans('It costs $5 and $10.')).toEqual([]) + expect(spans('From $20,000 to $30,000')).toEqual([]) + // a space after the opening or before the closing $ + expect(spans('$ x$ and $x $')).toEqual([]) + // the closing $ is followed by a digit + expect(spans('$x$5')).toEqual([]) + // the first $ after the opening one closes it, or there is no formula there + expect(spans('costs $5 and $10 or $x$')).toEqual([['$', 'x']]) + expect(spans('$$ $$ and $$')).toEqual([]) + expect(spans('just $')).toEqual([]) + }) + + test('escaped dollars', () => { + expect(spans('\\$x\\$')).toEqual([]) + expect(spans('\\$5 and $y$')).toEqual([['$', 'y']]) + // inside math, an escaped dollar is TeX's + expect(spans('$a\\$b$')).toEqual([['$', 'a\\$b']]) + // an escaped backslash does not escape the dollar + expect(spans('\\\\$x$')).toEqual([['$', 'x']]) + }) + + test('code spans win', () => { + expect(spans('`$x$`')).toEqual([]) + expect(spans('``a $x$ b`` and $y$')).toEqual([['$', 'y']]) + // the formula would end inside a code span: no formula + expect(spans('$a `b$` c')).toEqual([]) + // an unclosed backtick is a backtick + expect(spans('`a $x$')).toEqual([['$', 'x']]) + }) + + test('the Markdown of a formula; a $$ block over lines', () => { + expect(mathSource({ tex: 'x', delim: '$' })).toBe('$x$') + expect(mathSource({ tex: '\n x \n', delim: '$$' })).toBe('$$\n x \n$$') + // what $…$ cannot hold (a pasted formula) + expect(mathSource({ tex: ' x ', delim: '$' })).toBe('$$ x $$') + expect(mathSource({ tex: 'a$b', delim: '$' })).toBe('$$a$b$$') + expect(mathSource({ tex: 'a\\$b', delim: '$' })).toBe('$a\\$b$') + expect(mathSource({ tex: 'a\\', delim: '$' })).toBe('$$a\\$$') + expect(displayEnd(['$$', '- a', '# b', '$$'], 0)).toBe(3) + expect(displayEnd(['$$', 'a', '', '$$'], 0)).toBe(-1) + expect(displayEnd(['$$x$$'], 0)).toBe(-1) + expect(displayEnd([' $$', 'a', '$$'], 0)).toBe(-1) + }) +}) + +describe('Markdown → HTML of the document', () => { + const ann = (tex: string, display = false) => `${tex}` + + test('paragraphs, headings, list items, table cells, quotes', () => { + expect(markdownToHtml('A $x^2$ b.')).toBe(`

    A ${ann('x^2')} b.

    `) + expect(markdownToHtml('# Title $aTitle ${ann('a<b')}`) + expect(markdownToHtml('- item $x$')).toBe(`
      \n
    • item ${ann('x')}
    • \n
    `) + expect(markdownToHtml('| a | b |\n| --- | --- |\n| $x$ | 2 |')).toContain(`${ann('x')}`) + expect(markdownToHtml('> quote $q$')).toBe(`
    quote ${ann('q')}
    `) + // the rest of the Markdown around it still is Markdown + expect(markdownToHtml('**bold** $a*b*c$ *it*')).toBe(`

    bold ${ann('a*b*c')} it

    `) + }) + + test('a $$ block keeps its lines, whatever they start with', () => { + expect(markdownToHtml('$$\n a\n- b\n# c\n$$')).toBe(`

    ${ann('\n a\n- b\n# c\n', true)}

    `) + expect(markdownToHtml('Text before\n$$\nx\n$$\nafter')).toBe(`

    Text before ${ann('\nx\n', true)} after

    `) + }) + + test('code blocks and indented code have no math; escaped dollars are dollars', () => { + expect(markdownToHtml('```\n$x$\n```')).toBe('
    $x$
    ') + expect(markdownToHtml(' $x$ indented')).toBe('

    $x$ indented

    ') + expect(markdownToHtml('`$x$` and \\$5')).toBe('

    $x$ and $5

    ') + expect(markdownToHtml('It costs $5 and $10.')).toBe('

    It costs $5 and $10.

    ') + }) + + test('MathML with a TeX annotation is a formula (the clipboard, KaTeX, Wikipedia), other MathML is its text', () => { + const katex = '

    Area πr2\\pi r^2 m

    ' + const [p] = htmlToParas(katex, false) + expect(p.runs.map((r) => (r.t === 'ref' ? r.math : r.t === 'text' ? r.text : r.t))).toEqual(['Area ', { tex: '\\pi r^2', delim: '$' }, ' m']) + const [d] = htmlToParas('xx', false) + expect((d.runs[0] as Inline & { t: 'ref' }).math).toEqual({ tex: 'x', delim: '$$' }) + const [plain] = htmlToParas('

    y

    ', false) + expect(plain.runs).toMatchObject([{ t: 'text', text: 'y' }]) + }) +}) + +// ---------------------------------------------------------------- the document + +const enc = (s: string) => new TextEncoder().encode(s) + +async function open(text: string | Blob, name = 'notes.md') { + const events: { event: string; data: any }[] = [] + const engine = new Engine((event, data) => events.push({ event, data })) + await engine.open(typeof text === 'string' ? enc(text) : text, { name }) + await engine.call('docx.start', []) + const blocks: Block[] = events.filter((e) => e.event === 'docx:blocks').flatMap((e) => e.data) + return { engine, blocks } +} + +const paras = (bs: Block[]) => bs.filter((b): b is Paragraph => b.t === 'p') +const maths = (bs: Block[]) => paras(bs).flatMap((p) => p.runs.flatMap((r) => (r.t === 'ref' && r.math ? [r.math] : []))) +const saveMd = (src: string, blocks: Block[]) => mergeSource(src, 'markdown', blocks, (bs) => docxToMarkdown(bs)) ?? docxToMarkdown(blocks) + +async function partText(blob: Blob, path: string) { + return (await ZipArchive.open(new BlobSource(blob))).text(path) +} + +const MD = `# Notes + +Euler’s identity: $e^{i\\pi} + 1 = 0$. + +$$ +\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a} +$$ + +It costs $5 and \\$10, see \`$x$\`. + +$$ + \\begin{aligned} + a &= b \\\\ + c &= d + \\end{aligned} +$$ + +Broken: $\\frac{a}{$ and $\\foo x$. +` + +describe('a Markdown file with math opened as a document', () => { + test('formulas are equations holding their TeX and delimiters, byte for byte; the rest is text', async () => { + const { engine, blocks } = await open(MD) + expect(maths(blocks)).toEqual([ + { tex: 'e^{i\\pi} + 1 = 0', delim: '$' }, + { tex: '\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n', delim: '$$' }, + { tex: '\n \\begin{aligned}\n a &= b \\\\\n c &= d\n \\end{aligned}\n', delim: '$$' }, + { tex: '\\frac{a}{', delim: '$' }, + { tex: '\\foo x', delim: '$' }, + ]) + const ps = paras(blocks) + expect(ps[1].runs.map((r) => r.t)).toEqual(['text', 'ref', 'text']) + // the equation reads as its linear form (find, word count) + expect((ps[1].runs[1] as Inline & { t: 'ref' }).text).toBe('e^iπ+1=0') + expect(ps[3].runs.map((r) => (r.t === 'text' ? r.text : r.t)).join('')).toBe('It costs $5 and $10, see $x$.') + // opened is not edited + expect(await engine.call('dirty', [])).toBe(false) + // saved unedited: the file as it was + expect(saveMd(MD, blocks)).toBe(MD) + }) + + test('an edit next to an equation writes the paragraph again, its TeX as written', async () => { + const { blocks } = await open(MD) + const p = paras(blocks)[1] + ;(p.runs[0] as { text: string }).text = 'Euler’s famous identity: ' + ;(p.runs[2] as { text: string }).text = '!' + expect(saveMd(MD, blocks)).toBe(MD.replace('Euler’s identity: $e^{i\\pi} + 1 = 0$.', 'Euler’s famous identity: $e^{i\\pi} + 1 = 0$!')) + // the whole file written from the document (Download as Markdown): the formulas as written too + const whole = docxToMarkdown(blocks) + expect(whole).toContain('$e^{i\\pi} + 1 = 0$!') + expect(whole).toContain('$$\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n$$') + expect(whole).toContain('$$\n \\begin{aligned}\n a &= b \\\\\n c &= d\n \\end{aligned}\n$$') + expect(whole).toContain('Broken: $\\frac{a}{$ and $\\foo x$.') + }) + + test('an edit in the worker (as the editor sends it): the equation is copied, its TeX comes back', async () => { + const { engine, blocks } = await open(MD) + const p = paras(blocks)[1] + const edited: Paragraph = { ...p, edited: true, runs: [{ ...(p.runs[0] as Inline & { t: 'text' }), text: 'Identity: ' }, p.runs[1], p.runs[2]] } + const res = (await engine.call('docx.edit', [{ anchor: p.pid, list: [paraSpec([edited])], want: true }])) as EditResult + const back = res.blocks[p.pid!][0] as Paragraph + expect(back.runs.map((r) => (r.t === 'ref' ? r.math : r.t === 'text' ? r.text : r.t))).toEqual(['Identity: ', { tex: 'e^{i\\pi} + 1 = 0', delim: '$' }, '.']) + expect(await engine.call('dirty', [])).toBe(true) + }) + + test('a pasted formula (no source run) is written from its TeX; the Word file says the TeX namespace is ignorable', async () => { + const { engine, blocks } = await open('Plain *text* here.\n', 'plain.md') + const p = paras(blocks)[0] + const math: Inline = { t: 'ref', text: 'x²', s: {}, math: { tex: 'x^2 < y', delim: '$' } } + const edited: Paragraph = { ...p, edited: true, runs: [...p.runs, math] } + const res = (await engine.call('docx.edit', [{ anchor: p.pid, list: [paraSpec([edited])], want: true }])) as EditResult + const back = res.blocks[p.pid!][0] as Paragraph + expect(back.runs.at(-1)).toMatchObject({ t: 'ref', math: { tex: 'x^2 < y', delim: '$' } }) + const docx = (await engine.call('save', [])) as Blob + const xml = await partText(docx, 'word/document.xml') + expect(xml).toMatch(/]*xmlns:oe="urn:p10node:office-engine:math"[^>]*mc:Ignorable="[^"]*\boe\b/) + expect(xml).toContain('oe:tex="x^2 < y" oe:delim="$"') + const again = await open(docx, 'plain.docx') + expect(maths(again.blocks)).toEqual([{ tex: 'x^2 < y', delim: '$' }]) + }) + + test('the Word file: real equations (OMML) Word can show, the TeX kept for a round trip', async () => { + const { engine } = await open(MD) + const docx = (await engine.call('save', [])) as Blob + const xml = await partText(docx, 'word/document.xml') + expect(xml).toContain(' xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"') + expect(xml).toContain(' mc:Ignorable="oe"') + expect(xml).toContain('') + expect(xml).toContain('') + // opened again as a Word file: the same formulas, saved as Markdown as written + const again = await open(docx, 'notes.docx') + expect(maths(again.blocks)).toEqual(maths((await open(MD)).blocks)) + expect(docxToMarkdown(again.blocks)).toContain('$$\n \\begin{aligned}\n a &= b \\\\\n c &= d\n \\end{aligned}\n$$') + }) + + test('Windows line breaks: the formula keeps them when its neighbour is edited', async () => { + const crlf = MD.replace(/\n/g, '\r\n') + const { blocks } = await open(crlf) + const p = paras(blocks).find((x) => x.runs.some((r) => r.t === 'ref'))! + ;(p.runs[2] as { text: string }).text = '!' + const out = saveMd(crlf, blocks) + expect(out).toBe(crlf.replace('= 0$.', '= 0$!')) + expect(out).toContain('$$\r\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\r\n$$') + }) + + test('dollar signs in edited text are escaped only when they would be math', async () => { + const md = 'Price $5 here.\n\nTyped: x.\n' + const { blocks } = await open(md) + const [a, b] = paras(blocks) + ;(a.runs[0] as { text: string }).text = 'Price $5 and $6 here.' + ;(b.runs[0] as { text: string }).text = 'Typed: $x$ and $y$.' + expect(saveMd(md, blocks)).toBe('Price $5 and $6 here.\n\nTyped: \\$x\\$ and \\$y\\$.\n') + // next to a formula: escaped when they would pair with it + const m = await open('Cost $x$ only.\n') + const p = paras(m.blocks)[0] + ;(p.runs[0] as { text: string }).text = 'Cost $5 ' + expect(docxToMarkdown(m.blocks)).toBe('Cost $5 $x$ only.\n') + ;(p.runs[0] as { text: string }).text = 'Cost $b' + expect(docxToMarkdown(m.blocks)).toBe('Cost \\$b$x$ only.\n') + ;(p.runs[0] as { text: string }).text = 'Cost ' + ;(p.runs[2] as { text: string }).text = ' only $5.' + expect(docxToMarkdown(m.blocks)).toBe('Cost $x$ only $5.\n') + }) + + test('plain text and web page exports; the clipboard', async () => { + const { blocks } = await open(MD) + const text = docxToText(blocks) + expect(text).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$.') + expect(text).toContain('$$\n\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n$$') + const html = docxToHtml(blocks, { title: 'Notes' }) + expect(html).toContain('e') + expect(html).toContain('e^{i\\pi} + 1 = 0') + expect(html).toContain('') + // a broken one: its source, marked + expect(html).toContain('\\frac{a}{') + expect(html).toContain('\\foo') + // what the clipboard gets is read back as the same formulas + const back = htmlToParas(docxBodyHtml(paras(blocks).slice(1, 3)), false) + expect(back.flatMap((p) => p.runs.flatMap((r) => (r.t === 'ref' && r.math ? [r.math] : [])))).toEqual(maths(blocks).slice(0, 2)) + }) + + test('an HTML file with MathML holding TeX opens with the formulas, saved back as MathML', async () => { + const page = '

    Area: A\\pi r^2.

    \n' + const { blocks } = await open(page, 'page.html') + expect(maths(blocks)).toEqual([{ tex: '\\pi r^2', delim: '$' }]) + expect(mergeSource(page, 'html', blocks, (bs) => docxBodyHtml(bs))).toBe(page) + }) +}) + +describe('@p10node/markdown: math', () => { + const md = markdown.render + + test('inline and display formulas as MathML, the TeX in an annotation', () => { + expect(md('Euler: $e^{i\\pi}=-1$.')).toBe( + '

    Euler: eiπ=−1e^{i\\pi}=-1.

    ', + ) + const block = md('$$\n\\frac{a}{b}\n- c\n$$') + expect(block).toStartWith('

    ab−c') + // in a list item, a heading (the id from the text, not the TeX), a table + expect(md('- $x$')).toContain('

  • ') + expect(md('| a |\n|---|\n| $x$ |')).toContain(' { + expect(md('It costs $5 and $10.')).toBe('

    It costs $5 and $10.

    ') + expect(md('\\$x\\$ and `$y$`')).toBe('

    $x$ and $y$

    ') + expect(md('```\n$x$\n```')).toBe('
    $x$\n
    ') + expect(md(' $x$')).toBe('
    $x$\n
    ') + expect(md('$x$', { math: false })).toBe('

    $x$

    ') + }) + + test('still safe: the TeX is text, a color is a color, broken TeX is shown as text', () => { + const html = md('$$ $\\color{red" onclick="x}{y}$ $\\href{javascript:alert(1)}{x}$ $\\frac{a}{$') + // (no script element, no attribute that runs or loads something) + expect(html).not.toMatch(/]*\s(?:on\w+|href|src)=/i) + expect(html).toContain('<script>') + expect(html).toContain('\\href') + expect(html).toContain('\\frac{a}{') + // only MathML elements of our own, and only their attributes + const tags = new Set([...html.matchAll(/<([a-z]+)/g)].map((m) => m[1])) + for (const t of tags) expect(['p', 'math', 'semantics', 'mrow', 'mi', 'mo', 'mn', 'mtext', 'merror', 'mstyle', 'annotation', 'mfrac', 'msup']).toContain(t) + const attrs = new Set([...html.matchAll(/<[a-z]+([^>]*)>/g)].flatMap((t) => [...t[1].matchAll(/\s([a-z-]+)="/g)].map((m) => m[1]))) + for (const a of attrs) expect(['xmlns', 'encoding', 'stretchy', 'mathcolor', 'display', 'mathvariant']).toContain(a) + }) +}) diff --git a/test/math.test.ts b/test/math.test.ts new file mode 100644 index 0000000..3e05446 --- /dev/null +++ b/test/math.test.ts @@ -0,0 +1,373 @@ +/** TeX math → MathML (and OMML): the constructs people write in Markdown, and what it does with what it cannot read */ +import { describe, expect, test } from 'bun:test' +import { mathOmml, mathOf } from '../src/core/math/omml' +import { MAX_TEX, mathmlString, texMathml, texToMathml, type MNode } from '../src/core/math/tex' +import { ommlText } from '../src/core/docx/omml' +import { parseXml } from '../src/core/xml' + +/** the drawing of a formula as markup (without the math / semantics wrapper and the annotation) */ +const body = (tex: string, display = false) => { + const r = texToMathml(tex, display) + const sem = r.node.k![0] + return mathmlString(sem.k![0]) +} +const errorOf = (tex: string, display = false) => texToMathml(tex, display).error +/** the invisible function application after a function name */ +const FA = '\u2061' +const NBSP = '\u00a0' + +describe('TeX → MathML: tokens', () => { + test('letters, numbers, operators', () => { + expect(body('x+3.14-y')).toBe('x+3.14−y') + expect(body('a*b/c')).toBe('a∗b/c') + // spaces do not matter, comments are skipped + expect(body('a =\tb % a comment\n')).toBe('a=b') + // brackets typed as themselves do not grow + expect(body('(x)')).toBe('(x)') + }) + + test('Greek letters and symbols; capital Greek upright', () => { + expect(body('\\alpha\\beta\\varepsilon\\varphi')).toBe('αβεφ') + expect(body('\\Gamma')).toBe('Γ') + expect(body('\\varGamma')).toBe('𝛤') + expect(body('\\infty\\partial\\nabla\\hbar\\ell')).toBe('∞∂∇ℏℓ') + }) + + test('operators, relations, arrows', () => { + expect(body('a \\le b \\ne c \\approx d \\equiv e')).toBe('a≤b≠c≈d≡e') + expect(body('\\pm\\times\\cdot\\cup\\in\\subseteq')).toBe('±×⋅∪∈⊆') + expect(body('\\to\\Rightarrow\\iff\\mapsto\\longrightarrow')).toBe('→⇒⟺↦⟶') + expect(body('\\ldots\\cdots\\vdots\\ddots')).toBe('…⋯⋮⋱') + }) + + test('\\not and the negated relations', () => { + expect(body('a \\not= b')).toBe('a≠b') + expect(body('\\not\\in')).toBe('∉') + expect(body('\\not\\subset')).toBe('⊄') + // no negated form: a long solidus over it + expect(body('\\not\\perp')).toBe('⊥\u0338') + }) + + test('escaped characters', () => { + expect(body('\\{x\\}')).toBe('{x}') + expect(body('100\\%')).toBe('100%') + expect(body('\\$5')).toBe('$5') + }) +}) + +describe('TeX → MathML: scripts, primes, big operators', () => { + test('sub / superscripts', () => { + expect(body('x^2')).toBe('x2') + expect(body('x_i^2')).toBe('xi2') + expect(body('x^{2n}_{i,j}')).toBe('xi,j2n') + expect(body('e^{i\\pi}')).toBe('eiπ') + // no base + expect(body('^2')).toBe('2') + }) + + test('primes', () => { + expect(body("f'")).toBe('f′') + expect(body("f''(x)")).toBe('f″(x)') + expect(body("f'^2")).toBe('f′2') + }) + + test('big operators: limits under / over in display style, beside inline; integrals beside', () => { + expect(body('\\sum_{i=1}^n', true)).toBe('∑i=1n') + expect(body('\\sum_{i=1}^n')).toBe('∑i=1n') + expect(body('\\prod_k', true)).toBe('∏k') + expect(body('\\int_0^1', true)).toBe('∫01') + expect(body('\\oint_C', true)).toBe('∮C') + // \limits / \nolimits + expect(body('\\sum\\limits_{k}')).toBe('∑k') + expect(body('\\sum\\nolimits_{k}', true)).toBe('∑k') + expect(body('\\int\\limits_a^b', true)).toBe('∫ab') + // inside a fraction the style is not display: limits beside + expect(body('\\frac{\\sum_k a_k}{n}', true)).toContain('∑k') + }) + + test('named functions: upright, the function application, limits for lim / max', () => { + expect(body('\\sin x')).toBe(`sin${FA}x`) + // no space before a bracket + expect(body('\\log(x)')).toBe(`log${FA}(x)`) + expect(body('\\sin^2 x')).toBe(`sin2${FA}x`) + expect(body('\\lim_{x\\to 0} f', true)).toBe(`limx→0${FA}f`) + expect(body('\\lim_{n} a')).toContain('limn') + expect(body('\\operatorname{argmax}_x f', true)).toContain('argmaxx') + expect(body('\\operatorname*{argmax}_x f', true)).toContain('argmaxx') + expect(body('\\limsup')).toContain('lim sup') + }) + + test('double scripts are an error', () => { + expect(errorOf('x^2^3')).toBe('Double superscript') + expect(errorOf('x_1_2')).toBe('Double subscript') + }) +}) + +describe('TeX → MathML: fractions, roots, delimiters, accents', () => { + test('fractions and binomials', () => { + expect(body('\\frac{a}{b}')).toBe('ab') + expect(body('\\frac12')).toBe('12') + expect(body('\\dfrac{a}{b}')).toBe('ab') + expect(body('\\tfrac{a}{b}')).toBe('ab') + expect(body('\\binom{n}{k}')).toBe('(nk)') + expect(body('{a \\over b}')).toBe('ab') + expect(body('n \\choose k')).toBe('(nk)') + }) + + test('roots', () => { + expect(body('\\sqrt{x}')).toBe('x') + expect(body('\\sqrt[3]{x+1}')).toBe('x+13') + expect(body('\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}')).toBe( + '−b±b2−4ac2a', + ) + }) + + test('\\left … \\right, \\middle and sized delimiters', () => { + const f = '' + expect(body('\\left( \\frac{a}{b} \\right)')).toBe(`${f}(ab${f})`) + expect(body('\\left\\{ x \\right.')).toBe(`${f}{x`) + expect(body('\\left\\langle x \\middle| y \\right\\rangle')).toBe(`${f}⟨x|y${f}⟩`) + expect(body('\\left\\lfloor x \\right\\rfloor')).toContain('⌊') + expect(body('\\big( \\Bigg]')).toBe('(]') + expect(body('\\bigl| x \\bigr|')).toContain('minsize="1.2em"') + expect(errorOf('\\left( x')).toBe('Missing \\right') + expect(errorOf('x \\right)')).toBe('\\right without \\left') + expect(errorOf('\\left x \\right)')).toBe('Not a delimiter: x') + }) + + test('accents, over / under lines, braces', () => { + expect(body('\\hat{x}')).toBe('x^') + expect(body('\\widehat{xy}')).toBe('xy^') + expect(body('\\vec v')).toBe('v→') + expect(body('\\bar{x}\\tilde{y}\\dot{z}\\ddot{w}')).toBe( + 'x¯y~z˙w¨', + ) + expect(body('\\overline{AB}')).toBe('AB‾') + expect(body('\\underline{x}')).toBe('x_') + // a brace takes its label under / over it, in any style + expect(body('\\overbrace{a+b}^{n}')).toBe('a+b⏞n') + expect(body('\\underbrace{x}_{k}')).toBe('x⏟k') + expect(body('\\overset{!}{=}')).toBe('=!') + expect(body('\\underset{x}{\\min}')).toBe(`min${FA}x`) + expect(body('\\xrightarrow{f}')).toBe('→f') + }) +}) + +describe('TeX → MathML: text, fonts, spaces, color, environments', () => { + test('\\text: words, spaces at its ends kept, math inside', () => { + expect(body('\\text{if } x')).toBe(`if${NBSP}x`) + expect(body('\\text{a $x^2$ b}')).toBe(`a${NBSP}x2${NBSP}b`) + expect(body('\\textbf{bold}')).toBe('bold') + expect(body('\\text{50\\% off}')).toBe('50% off') + }) + + test('font commands: the letters of the math alphabets', () => { + expect(body('\\mathbb{R}')).toBe('ℝ') + expect(body('\\mathbb{N}\\mathbb{Z}\\mathbb{Q}\\mathbb{C}')).toBe('ℕℤℚℂ') + expect(body('\\mathbf{x}')).toBe('𝐱') + expect(body('\\mathbf{v}_1')).toBe('𝐯1') + expect(body('\\mathcal{L}\\mathcal{A}')).toBe('ℒ𝒜') + expect(body('\\mathfrak{g}')).toBe('𝔤') + expect(body('\\mathsf{A}\\mathtt{a}\\mathit{x}')).toBe('𝖠𝚊𝑥') + expect(body('\\boldsymbol{\\alpha}\\bm{x}')).toBe('𝜶𝒙') + expect(body('\\mathbf{\\Gamma}')).toBe('𝚪') + expect(body('\\mathbb{1}')).toBe('𝟙') + expect(body('\\mathrm{d}x')).toBe('dx') + expect(body('\\mathrm{max}')).toBe('max') + expect(body('{\\rm d}x')).toBe('dx') + }) + + test('spaces', () => { + expect(body('a\\,b')).toBe('ab') + expect(body('a\\quad b\\qquad c')).toBe('abc') + expect(body('a\\!b\\;c\\:d\\ e')).toContain('') + expect(body('\\hspace{3mu}')).toBe('') + expect(body('\\hspace{1cm}')).toBe('') + expect(body('a~b')).toBe(`a${NBSP}b`) + }) + + test('color, when it is a color', () => { + expect(body('\\color{red} x y')).toBe('xy') + expect(body('{\\color{#0a0} x} y')).toBe('xy') + expect(body('\\textcolor{blue}{x}')).toBe('x') + expect(body('\\color[HTML]{FF8800} x')).toBe('x') + // not a color: ignored, nothing of it reaches an attribute + expect(body('\\color{red;background:url(x)} x')).toBe('x') + expect(body('\\textcolor{expression(1)}{x}')).toBe('x') + }) + + test('styles, phantoms, boxes, modulo', () => { + expect(body('\\displaystyle \\sum_k')).toBe('∑k') + expect(body('\\phantom{x}')).toBe('x') + expect(body('\\boxed{x}')).toBe('x') + expect(body('a \\bmod b')).toContain('mod') + expect(body('a \\equiv b \\pmod{n}')).toContain('(modn)') + }) + + test('matrices', () => { + expect(body('\\begin{matrix} a & b \\\\ c & d \\end{matrix}')).toBe('abcd') + const f = (c: string) => `${c}` + const m = '1001' + for (const [env, open, close] of [['pmatrix', '(', ')'], ['bmatrix', '[', ']'], ['Bmatrix', '{', '}'], ['vmatrix', '|', '|'], ['Vmatrix', '‖', '‖']]) + expect(body(`\\begin{${env}} 1 & 0 \\\\ 0 & 1 \\end{${env}}`)).toBe(`${f(open)}${m}${f(close)}`) + // a last \\ does not make an empty row; a short row is filled + expect(body('\\begin{matrix} a & b \\\\ c \\\\ \\end{matrix}')).toBe('abc') + }) + + test('cases, aligned, gathered, array', () => { + expect(body('\\begin{cases} 1 & x > 0 \\\\ 0 & \\text{else} \\end{cases}')).toBe( + '{' + + '1x>0' + + '0else', + ) + // aligned: right / left pairs, the relation after & spaced as a relation, display style inside + expect(body('\\begin{aligned} a &= b \\\\ &= c \\end{aligned}')).toBe( + '' + + 'a=b' + + '=c', + ) + expect(body('\\begin{align*} x &= 1 \\end{align*}')).toContain('columnalign="right left"') + expect(body('\\begin{gathered} a \\\\ b \\end{gathered}')).toBe('ab') + expect(body('\\begin{array}{l|cr} a & b & c \\end{array}')).toBe( + 'abc', + ) + // \\ at the top of a display formula: lines, centered + expect(body('a \\\\ b', true)).toBe('ab') + expect(body('\\sum_{\\substack{i') + expect(errorOf('\\begin{foo} x \\end{foo}')).toBe('Unknown environment foo') + expect(errorOf('\\begin{matrix} x \\end{pmatrix}')).toBe('\\begin{matrix} ended by \\end{pmatrix}') + expect(errorOf('\\begin{matrix} x')).toBe('Missing \\end') + }) + + test('\\tag shows its label at the right', () => { + expect(body('E = mc^2 \\tag{1}', true)).toContain('(1)') + }) +}) + +describe('TeX → MathML: the math element, errors, bounds', () => { + test('a math element: block for display, the TeX as written in an annotation', () => { + const tex = '\n \\frac{a}{b} < c\n' + expect(texMathml(tex, true, false)).toBe( + 'ab<c' + + '\n \\frac{a}{b} < c\n', + ) + }) + + test('a command it does not know: shown as written, the rest drawn, said in the error', () => { + const r = texToMathml('\\foo{x} + \\bar{y} + \\baz', false) + expect(r.error).toBe('Not supported: \\foo, \\baz') + const s = mathmlString(r.node) + expect(s).toContain('\\foox+') + expect(s).toContain('y') + // a command name from the file is never a lookup of an object's own properties + expect(texToMathml('\\constructor \\toString \\hasOwnProperty \\valueOf', false).error).toBe('Not supported: \\constructor, \\toString, \\hasOwnProperty, \\valueOf') + }) + + test('input it cannot read: the whole source in an merror, the reason given', () => { + for (const [tex, why] of [ + ['\\frac{a}{', 'Missing }'], + ['{x', 'Missing }'], + ['x}', 'Unexpected }'], + ['\\frac{a}', 'Missing argument'], + ['a & b \\end{x}', '\\end without \\begin'], + ['\\sqrt[3', 'Missing ]'], + ['x^', 'Missing argument'], + ['\\', 'A backslash ends the text'], + ['#1', 'Unexpected #'], + ['\\text{$x}', 'Unclosed $ in \\text'], + ['\\hspace{wide}', 'Not a length: wide'], + ] as [string, string][]) { + const r = texToMathml(tex, false) + expect([tex, r.error]).toEqual([tex, why]) + expect(mathmlString(r.node)).toBe(`${tex.replace(/&/g, '&').replace(//g, '>')}${tex.replace(/&/g, '&').replace(//g, '>')}`) + } + }) + + test('bounded: nesting depth, length, elements', () => { + expect(texToMathml('{'.repeat(200) + 'x' + '}'.repeat(200), false).error).toBe('Nested too deeply') + expect(texToMathml('\\frac{'.repeat(100) + 'x' + '}{y}'.repeat(100), false).error).toBe('Nested too deeply') + expect(texToMathml('\\sqrt{'.repeat(100) + 'x' + '}'.repeat(100), false).error).toBe('Nested too deeply') + expect(texToMathml('x'.repeat(MAX_TEX + 1), false).error).toBe('Longer than 20,000 characters') + const t0 = performance.now() + expect(texToMathml('x^{y}_{z}'.repeat(2200), false).error).toBe(undefined) + expect(performance.now() - t0).toBeLessThan(2000) + }) + + test('never throws: broken and random input', () => { + const broken = [ + '', ' ', '}', '{', '{}', '^', '_', "'", '&', '\\\\', '\\', '\\left', '\\right', '\\left(', '\\right)', '\\middle|', '\\begin', '\\begin{', '\\begin{}', '\\end{matrix}', + '\\frac', '\\frac{', '\\frac{}{}', '\\sqrt[', '\\sqrt[]{}', '\\sqrt{}', '\\text', '\\text{', '\\color', '\\color{', '\\color{}{}', '\\not', '\\not}', '\\big', '\\big}', '\\left.\\right.', + 'x^{', 'x_', 'x^^', "x''^'", '\\over', 'a\\over', '\\over b', '\\choose', '\\operatorname', '\\operatorname*{}', '\\mathbb', '\\mathbb{', '\\hspace', '\\hspace{', '\\kern', + '\\begin{array}', '\\begin{array}{', '\\begin{array}{zz} a \\end{array}', '\\begin{cases}', '\\substack', '\\substack{', '\\tag', '\\tag{', '\\label', '\\xrightarrow', '\\xrightarrow[', + '$', '$$', '#', '%', '~', '\\%', '\\$', '\\#', '\\&', '\\_', '\\{', '\\}', '\\|', '\\,', '\\ ', '\\\n', '\u0000', '\uffff', '\ud800', '😀^😀', 'α_β', '\\left\\{\\middle\\|\\right\\}', + '\\begin{matrix}&&&\\\\&&\\end{matrix}', '\\frac{\\frac{\\frac{}{}}{}}{}', '\\sqrt[\\sqrt[\\sqrt]]', '\\text{\\text{\\text{$\\text{$x$}$}}}', '\\color{red}', '{\\displaystyle}', '\\rm', + ] + for (const tex of broken) { + for (const display of [false, true]) { + const r = texToMathml(tex, display) + expect(r.node.t).toBe('math') + expect(() => mathOmml({ tex, delim: display ? '$$' : '$' })).not.toThrow() + } + } + // random strings of TeX's own characters and commands + const parts = ['\\frac', '\\sqrt', '{', '}', '^', '_', '&', '\\\\', '\\left(', '\\right)', '\\begin{matrix}', '\\end{matrix}', 'x', '1', '+', '\\alpha', '\\text{a}', "'", '[', ']', '\\', ' ', '\\over', '\\not', '\\color{red}', '\\sum', '\\lim', '$'] + let seed = 7 + const rnd = (n: number) => ((seed = (seed * 1103515245 + 12345) & 0x7fffffff), seed % n) + for (let k = 0; k < 3000; k++) { + let tex = '' + for (let j = rnd(16); j >= 0; j--) tex += parts[rnd(parts.length)] + const r = texToMathml(tex, rnd(2) === 1) + expect(r.node.t).toBe('math') + expect(textOfAnnotation(r.node)).toBe(tex) + } + }) +}) + +const textOfAnnotation = (n: MNode) => n.k![0].k![1].x + +describe('TeX → OMML (the Word file a Markdown document is)', () => { + const omml = (tex: string, display = false) => mathOmml({ tex, delim: display ? '$$' : '$' }) + /** the OMML without the run properties, for reading */ + const bare = (x: string) => x.replace(/.*?<\/w:rPr>/g, '').replace(/ xml:space="preserve"/g, '') + + test('inline: m:oMath; display: m:oMathPara; the TeX in attributes, line breaks kept', () => { + expect(bare(omml('x^2'))).toBe('x2') + const x = omml('\n a < b & "c"\n', true) + expect(x).toStartWith('') + const el = parseXml(`${x}`).c[0] + expect(mathOf(el.a)).toEqual({ tex: '\n a < b & "c"\n', delim: '$$' }) + }) + + test('structures: fraction, radical, n-ary with its operand, delimiters, matrix, accents, functions', () => { + expect(bare(omml('\\frac{a}{b}'))).toContain('ab') + expect(bare(omml('\\binom{n}{k}'))).toContain('') + expect(bare(omml('\\sqrt{x}'))).toContain('x') + expect(bare(omml('\\sqrt[3]{x}'))).toContain('3') + // the sum takes what follows it, up to the relation + expect(bare(omml('\\sum_{i=1}^{n} i^2 = s', true))).toContain( + 'i=1ni2=', + ) + expect(bare(omml('\\int_0^1 x\\,dx'))).toContain('') + expect(bare(omml('\\int f'))).toContain('') + expect(bare(omml('\\left( x \\middle| y \\right]'))).toContain('xy') + expect(bare(omml('\\begin{pmatrix} a & b \\end{pmatrix}'))).toContain('') + expect(bare(omml('\\begin{aligned} a &= b \\end{aligned}'))).toContain('') + expect(bare(omml('\\hat{x}'))).toContain('x') + expect(bare(omml('\\overline{x}'))).toContain('') + expect(bare(omml('\\overbrace{x}^{n}'))).toContain('') + expect(bare(omml('\\sin x'))).toContain('sin') + expect(bare(omml('\\text{if}'))).toContain('if') + expect(omml('\\color{red}{x}')).toContain('') + }) + + test('the linear text the document shows for search and plain text', () => { + const text = (tex: string, display = false) => ommlText(parseXml(`${omml(tex, display)}`).c[0]) + expect(text('e^{i\\pi} + 1 = 0')).toBe('e^iπ+1=0') + expect(text('\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}', true)).toBe('(−b±√(b²−4ac))/2a') + expect(text('\\sum_{i=1}^n i')).toBe('∑_(i=1)^n i') + }) + + test('a formula that cannot be read is its source, as text', () => { + expect(bare(omml('\\frac{a}{'))).toBe('\\frac{a}{') + }) +})