Repository navigation
fix(markdown): keep dollar amounts out of inline math #439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4f65265
92a74ea
fab83c8
be72b58
ba3e496
e56a14e
9e2c9d7
3768658
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { asciiAlpha, asciiDigit, markdownLineEndingOrSpace } from "micromark-util-character"; | ||
| import type { Code, Construct, Extension, Tokenizer } from "micromark-util-types"; | ||
| import remarkMath from "remark-math"; | ||
| import type { Processor } from "unified"; | ||
|
|
||
| const DOLLAR = 36; | ||
|
|
||
| /** | ||
| * remark-math pairs any two `$`, so prices ("$5 and $10", "R$5 or US$10") | ||
| * render as glued italics. Wraps its inline tokenizer with Pandoc's rule: the | ||
| * opening `$` is followed by non-space, the closing `$` is preceded by | ||
| * non-space and not followed by a digit. A letter glued before an opener that | ||
| * precedes a digit (R$5, US$10) is a currency sign. A rejected span stays text, | ||
| * and its closing `$` is tried as the next opener. `$$…$$` is unchanged. | ||
| */ | ||
| function pandocMathText(mathText: Construct): Construct { | ||
| const tokenize: Tokenizer = function (effects, ok, nok) { | ||
| const before = this.previous; | ||
| const consumed: Code[] = []; | ||
| const tracked = { ...effects, consume: (code: Code) => (consumed.push(code), effects.consume(code)) }; | ||
| const done = (after: Code) => { | ||
| const size = consumed.findIndex((code) => code !== DOLLAR); | ||
| if (size !== 1) return ok(after); | ||
| const first = consumed[1]; | ||
| const last = consumed[consumed.length - 2]; | ||
| const literal = markdownLineEndingOrSpace(first) || markdownLineEndingOrSpace(last) || asciiDigit(after) || (asciiAlpha(before) && asciiDigit(first)); | ||
| return literal ? nok(after) : ok(after); | ||
|
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For explicit LaTeX such as AGENTS.md reference: AGENTS.md:L176-L176 Useful? React with 👍 / 👎.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in b3ac0b4: |
||
| }; | ||
| return mathText.tokenize.call(this, tracked, done, nok); | ||
| }; | ||
| return { ...mathText, tokenize }; | ||
| } | ||
|
|
||
| /** remark-math with Pandoc's rule for single-dollar inline math (see `pandocMathText`). */ | ||
| export default function remarkPandocMath(this: Processor) { | ||
| remarkMath.call(this); | ||
| const extension = (this.data().micromarkExtensions ?? []).at(-1) as Extension; | ||
| const text = extension.text as Record<number, Construct>; | ||
| text[DOLLAR] = pandocMathText(text[DOLLAR]); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 The Roast: You converted LaTeX inline math
\\(...\\)to$$...$$(display math) instead of$...$(inline math). In LaTeX,\\(...\\)is inline math. In Markdown/KaTeX,$$...$$is display math (centered, block-level). This means\\(x\\)2now renders as a display equation "x" followed by "2" on the next line, instead of inline "x²". The comment even says "$$…$$inline" — it's not inline, it's display. You've turned every explicit LaTeX inline formula into a centered block equation.🩹 The Fix:
📏 Severity: warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a regression: remark-math only produces display math (
mathnode,katex-display) for a$$fence that opens a flow block.$$…$$inside a line is the sameinlineMathnode as$…$(micromark'smathTextaccepts any delimiter size). A render check on this branch:\(x\)2→<p>[katex]2</p>,a \( x + y \) b→<p>a [katex] b</p>, and a list item stays inline. None of them containkatex-display. A\(x\)alone on a line also stays inline, because normalization converts it inside the line and never creates a separate fence. Reverting to$…$would bring back the bug Codex reported:\( x + y \)would be read as a price.