-
-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(regression): handle comment nesting in code fence and nunjucks #5717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
How to testgit clone -b fix/8.1.0-1 https://github.com/D-Sketon/hexo.git
cd hexo
npm install
npm test |
Pull Request Test Coverage Report for Build 18879282237Details
💛 - Coveralls |
There was a problem hiding this 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 fixes issues with nested comments in code fences and Nunjucks/Swig tags by implementing two distinct handling strategies: a new state machine state for Swig tags within HTML comments, and a positional check system for code fences within comments.
Key Changes:
- Added
STATE_PLAINTEXT_COMMENTto the state machine for proper handling of HTML comments containing Swig tags - Replaced the comment escape/restore mechanism in
backtick_code_block.tswith a positional check system that skips code fence processing within comment boundaries - Reordered the restoration sequence to restore comments before code blocks
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
test/scripts/hexo/post.ts |
Added test cases for incomplete comments, comments nested in Nunjucks tags, and comments nested in code fences |
lib/plugins/filter/before_post_render/backtick_code_block.ts |
Removed CodeBlockEscape class and implemented positional checking to skip code fence processing within comments |
lib/hexo/post.ts |
Added STATE_PLAINTEXT_COMMENT state to handle HTML comments in the state machine and reordered comment/code block restoration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| commentStarts.push(match.index); | ||
| commentEnds.push(match.index + match[0].length); | ||
| } | ||
| // notice that commentStarts and commentEnds are sorted, and commentStarts[i] < commentEnds[i], commentEnds[i] <= commentStarts[i+1] |
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The comment has a spacing inconsistency in the array notation. Use consistent spacing: commentStarts[i + 1] instead of commentStarts[i+1] to match code style conventions.
| // notice that commentStarts and commentEnds are sorted, and commentStarts[i] < commentEnds[i], commentEnds[i] <= commentStarts[i+1] | |
| // notice that commentStarts and commentEnds are sorted, and commentStarts[i] < commentEnds[i], commentEnds[i] <= commentStarts[i + 1] |
| if (state === STATE_PLAINTEXT_COMMENT) { | ||
| // Unterminated comment, just push the rest as comment | ||
| const comment = str.slice(plaintext_comment_start, length); | ||
| pushAndReset(PostRenderEscape.escapeContent(this.stored, 'comment', comment)); | ||
| break; | ||
| } |
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unterminated comment handling is duplicated between the main loop (lines 254-259) and this cleanup block. Consider extracting the comment escaping logic into a helper function to reduce duplication and improve maintainability.
yoshinorin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well on my machine.
I think we need to release a patch version (8.1.1). What do you think? If you can create a release PR for 8.1.1 after merging, I'll publish it. Thanks :)
What does it do?
fix #5716
fix #5715
The situation proved more complex than anticipated, requiring two distinct handling strategies:
Swig in Comments: To properly escape nested Swig tags within HTML comments, I introduced a dedicated state in the state machine. This state, which can only be entered from the plaintext state, effectively isolates and prevents erroneous processing of Swig inside comments.
Code Fences in Comments: Since code fences are regex-based and not part of the state machine, a different approach was needed. The solution is to first preprocess and identify all HTML comments. Then, before any code fence highlighting, a positional check is performed to skip those falling within comment boundaries.
Screenshots
Pull request tasks