Skip to content

Unpaired ] inside an inline equation makes escape_content escape the wrong bracket in added or deleted blocks #62

Open

Description

@andrader

Summary

When a whole block is added or deleted, escape_content escapes the wrong ] if an inline equation inside a function's content argument contains an unpaired ], such as a half-open interval $(0, 1]$. The ] inside the equation is counted as the closing bracket of the function argument, so the argument's real closing bracket is escaped as \], and the output does not compile.

Checked on main at d7d062b.

Minimal example

old.typ:

First.

new.typ:

First.

Text #emph[a $(0, 1]$ b] end.
typdiff old.typ new.typ -o diff.typ && typst compile diff.typ

Output (preamble omitted):

First.

#diff-added[Text #emph[a $(0, 1]$ b\] end.]

typst compile fails with:

error: unclosed delimiter

The same happens when the block is deleted (#diff-deleted[Text #emph[a $(0, 1]$ b\] end.]). A block that is only modified word by word is fine, and so is $[0, 1)$, where the count happens to balance.

Cause

escape_content in src/render.rs tracks bracket depth over the raw text to decide which ] are unbalanced. It doesn't skip inline equations, so the ] in $(0, 1]$ takes the depth from 1 to 0, and the ] that really closes #emph[ is then treated as unbalanced and escaped. Raw text and strings inside code arguments presumably have the same problem.

Possible fix

Skip inline equations when counting, reusing find_closing_dollar from #26, and do the same for raw text and quoted strings. Alternatively, take the bracket structure from typst_syntax::parse, so only brackets that are markup delimiters are counted.

Context

I found this diffing a thesis in which a new chapter has #cited[... a constant $q in (0, 1]$, ...].

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