Skip to content

Fix Typst label handling in diff output#8

Open
kjgoodrick wants to merge 2 commits into
sou1118:mainfrom
kjgoodrick:main
Open

Fix Typst label handling in diff output#8
kjgoodrick wants to merge 2 commits into
sou1118:mainfrom
kjgoodrick:main

Conversation

@kjgoodrick
Copy link
Copy Markdown

Summary

Fix Typst label handling in generated diff output.

This change treats Typst labels like <sample-anchor> as atomic syntax during
inline diffing. It prevents diff markup from being inserted inside label syntax,
and keeps unchanged labels outside added/deleted wrappers so references can
continue to resolve.

Closes #7.

Changes

  • Parse labels at the start of a paragraph as their own block.
  • Treat label-only blocks as atomic so they are not word-diffed internally.
  • Tokenize <label> syntax as a single token during mixed-granularity diffing.
  • Render inserted labels bare, and suppress deleted label spans to avoid invalid
    or duplicate labels.
  • Add regression tests for renamed labels and unchanged labels near inserted
    content.

Treat Typst labels as atomic syntax during tokenization and avoid word-level diffing label-only blocks. Keep labels at paragraph starts as separate blocks so unchanged labels remain attached to nearby content, and render inserted labels bare while suppressing deleted label spans.

Add regressions for renamed labels and unchanged labels near inserted text.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves Typst label handling in typdiff output so generated diffs remain valid Typst and label-based references continue to resolve. It does so by recognizing <label> syntax as atomic during inline diffing, splitting paragraph-start labels into their own blocks, and rendering labels without wrapping them in diff markup.

Changes:

  • Parse labels at paragraph start as their own paragraph blocks and treat label-only blocks as atomic (skip word-diffing inside them).
  • Tokenize <label> as a single token during mixed-granularity word diffing to prevent diff markup from splitting label syntax.
  • Render inserted labels bare and suppress deleted label output; add regression tests covering renamed and unchanged labels near edits.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/lib.rs Introduces TypstLabel helper and marks label-only paragraph blocks as atomic.
src/diff.rs Updates mixed tokenizer to treat <label> as an atomic token (via TypstLabel).
src/parse.rs Splits paragraph-start labels into their own paragraph blocks; adds parser regression test.
src/render.rs Renders labels bare (and suppresses deleted label output) to avoid invalid/duplicate labels; adds renderer regression test.
tests/integration.rs Adds integration tests for renamed labels and unchanged label placement relative to added content.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/render.rs Outdated
}
SpanTag::Deleted => {
if is_label_only(&span.text) {
prev_was_diff = false;
@kjgoodrick
Copy link
Copy Markdown
Author

Thanks for the review, fixed.

I updated render_spans() so skipped deleted label-only spans no longer clear the previous diff state. I also fixed a related deleted-side rendering case where #ref(<label>, ...) could incorrectly escape the label argument and produce invalid Typst.

Added regression coverage for both fixes, including the following cases:

  • skipped deleted labels preserving the diff-call guard
  • #ref(<label>, ...) keeping the first label argument intact
  • replaced footnote refs rendering correctly on the deleted side

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typdiff can generate invalid Typst when diffing labels and cross-references

2 participants