Skip to content

Conversation

@D-Sketon
Copy link
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

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

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

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
Member

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

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.

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
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
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
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

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

3 participants