From 93c3e73e66cd374f7c953a38e161373ed9bc529e Mon Sep 17 00:00:00 2001 From: Yuan-Ru Lin Date: Tue, 8 Sep 2026 20:19:03 -0700 Subject: [PATCH 1/6] fix: treat inline equations as atomic tokens in word-level diff Inline equations are now consumed as a single token up to the closing `$`, matching how block equations are already handled. A modified equation renders as a whole-equation deletion plus insertion. Fixes #25 --- src/diff.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/diff.rs b/src/diff.rs index 208733b..89ad146 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -180,6 +180,15 @@ fn tokenize_mixed(s: &str) -> Vec<&str> { // Standalone '#' tokens.push(&s[start..i]); } + } else if c == '$' { + // Inline equation: keep `$...$` as one atomic token + let start = i; + i += 1; + match s[i..].find('$') { + Some(off) => i += off + 1, + None => i = s.len(), + } + tokens.push(&s[start..i]); } else if c == '@' { // Typst reference: @label (alphanumeric, hyphens, underscores, colons, periods) let start = i; From 2198218a15f783c547577b844c7aab29d75729d2 Mon Sep 17 00:00:00 2001 From: Yuan-Ru Lin Date: Tue, 8 Sep 2026 20:25:14 -0700 Subject: [PATCH 2/6] Add test --- src/diff.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/diff.rs b/src/diff.rs index 89ad146..97311a6 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -552,6 +552,12 @@ mod tests { assert_eq!(tokens, vec![""]); } + #[test] + fn test_tokenize_inline_equation_is_atomic() { + let tokens = tokenize_mixed("is $t = 12.4 \"hour\"$ for"); + assert_eq!(tokens, vec!["is", " ", "$t = 12.4 \"hour\"$", " ", "for"]); + } + #[test] fn test_tokenize_cjk_char_level() { // CJK characters should be tokenized individually (no whitespace boundaries). From 4c103081ace9452fed0430f38cc56ef12df18efa Mon Sep 17 00:00:00 2001 From: Yuan-Ru Lin Date: Tue, 8 Sep 2026 20:29:39 -0700 Subject: [PATCH 3/6] fix: skip escaped `\$` when tokenizing inline equations A `$` preceded by a backslash is a literal dollar sign in markup, not a math delimiter. Also add unit tests for the atomic equation token and the escaped case. --- src/diff.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 97311a6..972684f 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -180,8 +180,10 @@ fn tokenize_mixed(s: &str) -> Vec<&str> { // Standalone '#' tokens.push(&s[start..i]); } - } else if c == '$' { - // Inline equation: keep `$...$` as one atomic token + } else if c == '$' && !s[..i].ends_with('\\') { + // Inline equation: keep `$...$` as one atomic token. + // A `$` preceded by a backslash is an escaped + // literal dollar sign in markup, not a math delimiter. let start = i; i += 1; match s[i..].find('$') { @@ -558,6 +560,13 @@ mod tests { assert_eq!(tokens, vec!["is", " ", "$t = 12.4 \"hour\"$", " ", "for"]); } + #[test] + fn test_tokenize_escaped_dollar_is_not_equation() { + let tokens = tokenize_mixed("\\$5 and $x$"); + assert!(tokens.contains(&"$x$")); + assert!(!tokens.iter().any(|t| t.starts_with("$5"))); + } + #[test] fn test_tokenize_cjk_char_level() { // CJK characters should be tokenized individually (no whitespace boundaries). From 3fc2f0aa5debbdaec7956335ab090d84d4dfc720 Mon Sep 17 00:00:00 2001 From: Yuan-Ru Lin Date: Tue, 8 Sep 2026 20:52:58 -0700 Subject: [PATCH 4/6] fix: use backslash parity to detect escaped `$` on both delimiters `\\$x$` has an even number of backslashes, so the `$` is a real opening delimiter; `$a \$ b$` contains an escaped dollar sign that must not terminate the equation early. Count trailing backslashes with a small helper and treat the `$` as escaped only when the count is odd. Tests cover both cases. Assisted-by: Claude Fable 5.1 --- src/diff.rs | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 972684f..c03b2cf 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -137,8 +137,8 @@ fn process_replace(old_range: &[Block], new_range: &[Block], results: &mut Vec`) are treated as atomic tokens so the diff never -/// fragments valid Typst syntax. +/// labels (`