Skip to content

Conversation

@D-Sketon
Copy link
Member

@D-Sketon D-Sketon commented Oct 27, 2025

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

  • Add test cases for the changes.
  • Passed the CI test.

@github-actions
Copy link

How to test

git clone -b fix/8.1.0-1 https://github.com/D-Sketon/hexo.git
cd hexo
npm install
npm test

@coveralls
Copy link

coveralls commented Oct 27, 2025

Pull Request Test Coverage Report for Build 18879282237

Details

  • 44 of 46 (95.65%) changed or added relevant lines in 2 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 99.489%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/plugins/filter/before_post_render/backtick_code_block.ts 21 23 91.3%
Files with Coverage Reduction New Missed Lines %
lib/hexo/post.ts 2 99.48%
Totals Coverage Status
Change from base Build 18818430665: -0.04%
Covered Lines: 9938
Relevant Lines: 9989

💛 - Coveralls

@D-Sketon D-Sketon marked this pull request as ready for review October 28, 2025 15:02
@D-Sketon D-Sketon requested a review from Copilot October 28, 2025 15:02
Copy link

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 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_COMMENT to the state machine for proper handling of HTML comments containing Swig tags
  • Replaced the comment escape/restore mechanism in backtick_code_block.ts with 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]
Copy link

Copilot AI Oct 28, 2025

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.

Suggested change
// 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]

Copilot uses AI. Check for mistakes.
Comment on lines +266 to +271
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;
}
Copy link

Copilot AI Oct 28, 2025

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.

Copilot uses AI. Check for mistakes.
@D-Sketon D-Sketon requested a review from a team October 28, 2025 15:03
Copy link
Member

@yoshinorin yoshinorin left a 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 :)

@D-Sketon D-Sketon merged commit b28edcf into hexojs:master Oct 31, 2025
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants