Repository navigation
fix: respect CommonMark tilde code fences and prevent silent AST truncation - #2
Merged
Merged
Conversation
…cation - In CommonMark, tildes (~) only open fenced code blocks when a run of 3 or more tildes appears at the start of a line (indented <= 3 spaces). Previously, any unescaped single tilde in prose (e.g. "~1,110 tasks") or strikethrough was treated as an inline code span opener, suppressing inline math expansion across subsequent prose. Disentangle backticks and tildes, add isFenceLineStart and isFenceLineEnd to prevent premature closure on comments, and validate info strings. - In parser.go, assert all tokens are consumed in parse() (p.peek().typ == tokEOF). Previously, dangling unconsumed tokens or unmatched \end constructs caused parse() to succeed with a partial AST, silently dropping the rest of the expression. Failing with an error allows Expand to preserve raw markdown verbatim rather than emitting a truncated fragment. - Add regression tests covering inline tildes, tilde ranges, strikethrough, comments inside fences, and unconsumed token error handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses two parser/expander bugs that cause math suppression and text loss:
Inline Tildes Suppressing Math Expansion (
markdown.go):termtex.Expandpreviously treated any unescaped tilde (~) identically to a backtick code span opener (inCode = true). Any single tilde in prose (e.g.~1,110 tasks) or GFM strikethrough (~~text~~) suppressed math expansion ($...$) for the remainder of the text.isFenceLineStartandisFenceLineEnd, and validate that opening info strings cannot contain tilde characters.Silent AST Truncation on Incomplete Parses (
parser.go):\end{...}, or dangling delimiters,parseRows()stopped early butparse()returnederr == nil.Renderthen rendered only the leading prefix, silently dropping the rest of the expression.parse(), assert that all tokens have been consumed (p.peek().typ == tokEOF). If unconsumed tokens remain, return an error. This causesExpandto fall back to preserving the raw markdown verbatim rather than emitting a truncated fragment.Tests
TestExpandTildeInProse: covers single tildes in prose, ranges, strikethrough, comments inside fences, and indentation.TestExpandPreservesUnparseableMathVerbatim: verifiesExpandpreserves unparseable math verbatim.TestUnconsumedTokensError: verifiesparse()errors on dangling tokens.