-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,13 +1,11 @@ | ||||||
| import type { HighlightOptions } from '../../../extend/syntax_highlight'; | ||||||
| import type Hexo from '../../../hexo'; | ||||||
| import type { RenderData } from '../../../types'; | ||||||
| import assert from 'assert'; | ||||||
|
|
||||||
| const rBacktick = /^((?:(?:[^\S\r\n]*>){0,3}|[-*+]|[0-9]+\.)[^\S\r\n]*)(`{3,}|~{3,})[^\S\r\n]*((?:.*?[^`\s])?)[^\S\r\n]*\n((?:[\s\S]*?\n)?)(?:(?:[^\S\r\n]*>){0,3}[^\S\r\n]*)\2[^\S\r\n]?(\n+|$)/gm; | ||||||
| const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/; | ||||||
| const rLangCaption = /([^\s]+)\s*(.+)?/; | ||||||
| const rCommentEscape = /(<!--[\s\S]*?-->)/g; | ||||||
| const rCommentHolder = /(?:<|<)!--comment\uFFFC(\d+)--(?:>|>)/g; | ||||||
| const rAdditionalOptions = /\s((?:line_number|line_threshold|first_line|wrap|mark|language_attr|highlight):\S+)/g; | ||||||
|
|
||||||
| const escapeSwigTag = (str: string) => str.replace(/{/g, '{').replace(/}/g, '}'); | ||||||
|
|
@@ -83,44 +81,34 @@ function parseArgs(args: string) { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| class CodeBlockEscape { | ||||||
| public stored: string[]; | ||||||
|
|
||||||
| constructor() { | ||||||
| this.stored = []; | ||||||
| } | ||||||
|
|
||||||
| static escapeContent(cache: string[], flag: string, str: string) { | ||||||
| return `<!--${flag}\uFFFC${cache.push(str) - 1}-->`; | ||||||
| } | ||||||
|
|
||||||
| static restoreContent(cache: string[]) { | ||||||
| return (_: string, index: number) => { | ||||||
| assert(cache[index]); | ||||||
| const value = cache[index]; | ||||||
| cache[index] = null; | ||||||
| return value; | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| restoreComments(str: string) { | ||||||
| return str.replace(rCommentHolder, CodeBlockEscape.restoreContent(this.stored)); | ||||||
| } | ||||||
|
|
||||||
| escapeComments(str: string) { | ||||||
| return str.replace(rCommentEscape, (_, content) => CodeBlockEscape.escapeContent(this.stored, 'comment', content)); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| export = (ctx: Hexo): (data: RenderData) => void => { | ||||||
| return function backtickCodeBlock(data: RenderData): void { | ||||||
| const dataContent = data.content; | ||||||
|
|
||||||
| if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.extend.highlight.query(ctx.config.syntax_highlighter)) return; | ||||||
| const cacheObj = new CodeBlockEscape(); | ||||||
| data.content = cacheObj.escapeComments(data.content); | ||||||
| data.content = data.content.replace(rBacktick, ($0, start, $2, _args, _content, end) => { | ||||||
| // get all comment starts and ends | ||||||
| const commentStarts = []; | ||||||
| const commentEnds = []; | ||||||
| let match: RegExpExecArray | null; | ||||||
| rCommentEscape.lastIndex = 0; | ||||||
| while ((match = rCommentEscape.exec(dataContent)) !== null) { | ||||||
| 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] | ||||||
|
||||||
| // 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] |
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
Oops, something went wrong.
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.
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.