Fix: emit parse error for unterminated string in tag attribute (fixes #114)#608
Open
shivamtiwari3 wants to merge 1 commit into
Open
Conversation
…arkdoc#114) Root cause: parseTags() called findTagEnd() which returns null when a string attribute has no closing quote (the scanner stays in STATES.string and never sees the '%}' as a valid tag end). The null branch silently skipped past the '{%' with no error, so the entire malformed tag was dropped as plain text. Fix: when findTagEnd returns null but a raw '%}' does exist in the content, emit an 'error' token instead of skipping — this surfaces as a critical parse-error on the resulting AST node so callers can diagnose the problem. When no '%}' is present at all the '{%' is genuinely not a Markdoc tag and the original skip behaviour is preserved.
5 tasks
Author
|
Hi — happy to make any further changes if helpful. Just checking if this is still under consideration. |
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
Fixes #114.
parseTags()insrc/utils.tsnow emits aparse-error(level:critical) when a tag has an unterminated string attribute, instead of silently discarding the tag as plain text.Root Cause
findTagEnd()(utils.ts:26–51) uses a state machine with three states:normal,string, andescape. When it sees"inSTATES.normalit transitions toSTATES.stringand stays there until a closing"is found. If the closing quote is missing (e.g.{% quote content="test /%}), the scanner never returns toSTATES.normal, so it never matches the%}as a valid tag end and returnsnull.In
parseTags(), theend == nullbranch (previously lines 87–91) simply advancedpospast{%and calledcontinue— the entire malformed construct was silently treated as plain text with no diagnostic.Solution
When
findTagEndreturnsnull, check whether a raw%}exists later in the content (viacontent.indexOf('%}', ...)):%}present but unreachable (unterminated string): push anerrortoken with message"Unterminated string in tag attribute". The parser inparser.ts:131–134promotes this into aparse-errornode with levelcriticalon the AST, making it visible to validators and error reporters.%}at all:{%is not a Markdoc tag (e.g. code examples that happen to contain{%). Preserve the existing skip behaviour so no regression occurs for these cases.The change is 28 lines in
parseTags()and adds no new dependencies.Testing
parseTags — malformed tags — generates an error token for an unterminated string attribute— reproduces the exact scenario from the issue ({% quote content="test /%}); previously produced zero error tokens, now produces one.parseTags — malformed tags — leaves as text when closing delimiter is entirely absent— asserts that{%with no%}anywhere (e.g. code blocks) is still left as plain text, preventing regression.parseTags — malformed tags — still parses valid tags after a malformed one— asserts that recovery works and the rest of the content is parsed normally.Run with:
Checklist