Skip to content

Inline raw text containing $ breaks a following inline equation in word-level diff #58

Open

Description

@sou1118

Summary

When a paragraph contains inline raw text with a $ inside (e.g. `$HOME`) and a later inline equation, the word-level diff can produce invalid Typst.

This was found while reviewing #26 and was intentionally left out of that PR. It's expected to be fixed by the raw-text handling in #28.

Minimal example

old.typ:

Use `$HOME` and $x$ here.

new.typ:

Use `$PATH` and $y$ here.
typdiff old.typ new.typ -o diff.typ && typst compile diff.typ

Output (preamble omitted):

Use `#diff-deleted[$HOME` and $x]#diff-added[$PATH` and $y]$ here.

typst compile fails with:

error: unknown variable: PATH

Cause

tokenize_mixed doesn't know about raw text. The $ inside the backticks is taken as the start of an inline equation, and find_closing_dollar scans forward to the opening $ of $x$. The resulting token runs across the closing backtick, so the raw delimiters and the equation delimiters end up split across #diff-*[...] calls.

When there's no later $ in the paragraph, the unclosed-$ fallback added in #26 keeps the output valid. That's why `$HOME` on its own works.

Possible fix

Treat inline raw text as an atomic token in tokenize_mixed before the $ branch is reached, so a $ inside backticks is never seen as an equation delimiter. #28 adds this via TypstRaw::end.

Once that lands, test_tokenize_unclosed_dollar_is_single_token will need its expected tokens updated, since `$HOME` will become a single token.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions