From 49ae6670e280e4a3531377dca9fe878d7485db2f Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Mon, 6 Jan 2025 13:28:28 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AE=20Trim=20all=20math=20to=20ensure?= =?UTF-8?q?=20no=20new=20lines=20in=20latex=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See #1704 --- .changeset/tricky-moles-enjoy.md | 5 +++++ packages/myst-parser/src/tokensToMyst.ts | 12 ++++++++---- packages/myst-parser/tests/math.spec.ts | 2 ++ packages/mystmd/tests/basic-tex-math/input.md | 6 ++++++ packages/mystmd/tests/outputs/basic-tex-math.tex | 6 ++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 .changeset/tricky-moles-enjoy.md diff --git a/.changeset/tricky-moles-enjoy.md b/.changeset/tricky-moles-enjoy.md new file mode 100644 index 000000000..372f8bd5b --- /dev/null +++ b/.changeset/tricky-moles-enjoy.md @@ -0,0 +1,5 @@ +--- +"myst-parser": patch +--- + +Explicitly trim math strings diff --git a/packages/myst-parser/src/tokensToMyst.ts b/packages/myst-parser/src/tokensToMyst.ts index b68233654..3ca4ca97a 100644 --- a/packages/myst-parser/src/tokensToMyst.ts +++ b/packages/myst-parser/src/tokensToMyst.ts @@ -280,9 +280,10 @@ const defaultMdast: Record = { math_inline_double: { type: 'math', noCloseToken: true, - isText: true, + isLeaf: true, getAttrs(t) { return { + value: t.content.trim(), enumerated: t.meta?.enumerated, }; }, @@ -290,10 +291,11 @@ const defaultMdast: Record = { math_block: { type: 'math', noCloseToken: true, - isText: true, + isLeaf: true, getAttrs(t) { const name = t.info || undefined; return { + value: t.content.trim(), ...normalizeLabel(name), enumerated: t.meta?.enumerated, }; @@ -302,10 +304,11 @@ const defaultMdast: Record = { math_block_label: { type: 'math', noCloseToken: true, - isText: true, + isLeaf: true, getAttrs(t) { const name = t.info || undefined; return { + value: t.content.trim(), ...normalizeLabel(name), enumerated: t.meta?.enumerated, }; @@ -314,10 +317,11 @@ const defaultMdast: Record = { amsmath: { type: 'math', noCloseToken: true, - isText: true, + isLeaf: true, getAttrs(t, tokens, index, state) { const tight = computeAmsmathTightness(state.src, t.map); const attrs = { + value: t.content.trim(), enumerated: t.meta?.enumerated, } as Record; if (tight) attrs.tight = tight; diff --git a/packages/myst-parser/tests/math.spec.ts b/packages/myst-parser/tests/math.spec.ts index ea4c60079..0594ad051 100644 --- a/packages/myst-parser/tests/math.spec.ts +++ b/packages/myst-parser/tests/math.spec.ts @@ -8,6 +8,7 @@ import type { Math } from 'myst-spec-ext'; describe('Test math tightness', () => { test.each([ [true, 'p\n$$Ax=b$$\np'], + [true, 'p\n$$\nAx=b\n$$\np'], [undefined, 'p\n\n$$Ax=b$$\n\np'], ['before', 'p\n$$Ax=b$$\n\np'], // ['after', 'p\n\n$$Ax=b$$\np'], // TODO: this is (maybe) a bug in the dollar-math parser @@ -18,6 +19,7 @@ describe('Test math tightness', () => { expect((select('math', tree) as Math).tight).toBe(undefined); mathNestingTransform(tree, file); expect((select('math', tree) as Math).tight).toBe(tight); + expect((select('math', tree) as Math).value).toBe('Ax=b'); }); test.each([ [true, 'p\n:::{math}\nAx=b\n:::\np'], diff --git a/packages/mystmd/tests/basic-tex-math/input.md b/packages/mystmd/tests/basic-tex-math/input.md index db290ab00..a0b0d3bd0 100644 --- a/packages/mystmd/tests/basic-tex-math/input.md +++ b/packages/mystmd/tests/basic-tex-math/input.md @@ -4,6 +4,12 @@ Paragraph $$Ax=b$$ Paragraph +Paragraph around dollar math +$$ +Ax=b +$$ +Paragraph + Paragraph \begin{equation} Ax=b diff --git a/packages/mystmd/tests/outputs/basic-tex-math.tex b/packages/mystmd/tests/outputs/basic-tex-math.tex index 08cd56e8b..fe4dee745 100644 --- a/packages/mystmd/tests/outputs/basic-tex-math.tex +++ b/packages/mystmd/tests/outputs/basic-tex-math.tex @@ -4,6 +4,12 @@ \end{equation} Paragraph +Paragraph around dollar math +\begin{equation} +Ax=b +\end{equation} +Paragraph + Paragraph \begin{equation} Ax=b