Skip to content

fix: Incorrectly ignores single-line code blocks when escaping Swig and HTML comments#5722

Merged
yoshinorin merged 5 commits intohexojs:masterfrom
D-Sketon:fix/swig
Mar 26, 2026
Merged

fix: Incorrectly ignores single-line code blocks when escaping Swig and HTML comments#5722
yoshinorin merged 5 commits intohexojs:masterfrom
D-Sketon:fix/swig

Conversation

@D-Sketon
Copy link
Copy Markdown
Member

@D-Sketon D-Sketon commented Nov 6, 2025

close #5721

Main Changes:

Introduced the STATE_INLINE_CODE state. In this state, apart from raw Swig tags being escaped, all other text (including HTML comments and Swig tags) will not be processed.

Impact:

  1. Fixed the issue where HTML comments within inline code blocks were not being encoded.
  2. Swig comments like {# #} within inline code blocks will no longer be skipped during rendering (I am uncertain whether this change is reasonable or if the previous behavior was a bug).
  3. Incomplete Swig tags within inline code blocks will now enter the STATE_INLINE_CODE state, eliminating the need for backtracking processing, which will improve performance to some extent.

Pull request tasks

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

@D-Sketon D-Sketon changed the title fix: Incorrectly ignores single-line code blocks when escaping Swig a… fix: Incorrectly ignores single-line code blocks when escaping Swig and HTML comments Nov 6, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 6, 2025

How to test

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

@coveralls
Copy link
Copy Markdown

coveralls commented Nov 6, 2025

Pull Request Test Coverage Report for Build 19190628262

Details

  • 65 of 65 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 99.502%

Files with Coverage Reduction New Missed Lines %
lib/hexo/post.ts 1 99.68%
Totals Coverage Status
Change from base Build 18971852506: 0.01%
Covered Lines: 9990
Relevant Lines: 10040

💛 - Coveralls

@D-Sketon D-Sketon marked this pull request as ready for review November 8, 2025 08:33
@D-Sketon D-Sketon requested a review from Copilot November 8, 2025 08:33
Copy link
Copy Markdown
Contributor

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 adds comprehensive support for handling inline code blocks (backticks) in the Hexo post rendering system. The changes prevent Swig tags, comments, and other special syntax within inline code blocks from being processed, ensuring they are properly escaped and displayed as literal text instead.

  • Adds inline code block detection and handling logic to the post rendering escape system
  • Extends test coverage for inline code blocks with various edge cases including swig tags, comments, and different backtick counts
  • Fixes an existing test expectation to match actual HTML entity encoding behavior

Reviewed Changes

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

File Description
lib/hexo/post.ts Adds STATE_INLINE_CODE state and associated logic to detect and escape inline code blocks, removes unused rCommentEscape regex and escapeComments method
test/scripts/hexo/post.ts Adds Bluebird import and stub import, fixes test expectation for quotes encoding, adds extensive test coverage for inline code blocks including edge cases with swig tags, comments, and newlines

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

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: D-Sketon <2055272094@qq.com>
@D-Sketon D-Sketon requested a review from a team November 10, 2025 14:41
@yoshinorin
Copy link
Copy Markdown
Member

I commented on the issue reproduction conditions. See #5721 (comment)

Copy link
Copy Markdown
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.

The code is a bit complex, but it appears to be working as intended. I have one question though.

if ((char === '\n' && next_char === '\n')
|| (char === '\r' && next_char === '\n' && str[idx + 2] === '\r' && str[idx + 3] === '\n')
|| (char === '\r' && next_char === '\n' && str[idx + 2] === '\n')
|| (char === '\n' && next_char === '\r' && str[idx + 2] === '\n')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Question:

Could you explain the reason for including the \r\n\n and \n\r\n patterns? Is it because line endings might be mixed between Linux/Mac and Windows?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This can be considered a form of defensive programming. I'm not sure if scenarios with mixed \n and \r\n would actually occur in practice.

Copy link
Copy Markdown
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.

LGTM

@yoshinorin yoshinorin merged commit 48c764a into hexojs:master Mar 26, 2026
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

Development

Successfully merging this pull request may close these issues.

Incorrectly ignores single-line code blocks when escaping Swig and HTML comments

4 participants