Skip to content

fix: nunjucks/code blocks in HTML comments were incorrectly converted #5616

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import type { NodeJSLikeCallback, RenderData } from '../types';
const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content'];

const rHexoPostRenderEscape = /<hexoPostRenderCodeBlock>([\s\S]+?)<\/hexoPostRenderCodeBlock>/g;
const rCommentEscape = /(<!--[\s\S]*?-->)/g;

const rSwigPlaceHolder = /(?:<|&lt;)!--swig\uFFFC(\d+)--(?:>|&gt;)/g;
const rCodeBlockPlaceHolder = /(?:<|&lt;)!--code\uFFFC(\d+)--(?:>|&gt;)/g;
const rCommentHolder = /(?:<|&lt;)!--comment\uFFFC(\d+)--(?:>|&gt;)/g;

const STATE_PLAINTEXT = Symbol('plaintext');
const STATE_SWIG_VAR = Symbol('swig_var');
Expand Down Expand Up @@ -60,6 +62,14 @@ class PostRenderEscape {
return str.replace(rCodeBlockPlaceHolder, PostRenderEscape.restoreContent(this.stored));
}

restoreComments(str: string) {
return str.replace(rCommentHolder, PostRenderEscape.restoreContent(this.stored));
}

escapeComments(str: string) {
return str.replace(rCommentEscape, (_, content) => PostRenderEscape.escapeContent(this.stored, 'comment', content));
}

escapeCodeBlocks(str: string) {
return str.replace(rHexoPostRenderEscape, (_, content) => PostRenderEscape.escapeContent(this.stored, 'code', content));
}
Expand Down Expand Up @@ -436,6 +446,8 @@ class Post {
// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, { context: ctx });
}).then(() => {
// Escape all comments to avoid conflict with Nunjucks and code block
data.content = cacheObj.escapeComments(data.content);
data.content = cacheObj.escapeCodeBlocks(data.content);
// Escape all Nunjucks/Swig tags
if (disableNunjucks === false) {
Expand Down Expand Up @@ -465,6 +477,7 @@ class Post {
}, options);
}).then(content => {
data.content = cacheObj.restoreCodeBlocks(content);
data.content = cacheObj.restoreComments(data.content);

// Run "after_post_render" filters
return ctx.execFilter('after_post_render', data, { context: ctx });
Expand Down
58 changes: 58 additions & 0 deletions test/scripts/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,4 +1452,62 @@ describe('Post', () => {

data.content.should.eql('<a href="https://hexo.io/" title="tttitle" target="">foobar</a>');
});

// https://github.com/hexojs/hexo/issues/5433
it('render() - nunjucks nesting in comments', async () => {
const content = [
'foo',
'<!--',
'{% raw %}',
'test',
'{% endraw %}',
'-->',
'bar'
].join('\n');

const data = await post.render('', {
content,
engine: 'markdown'
});

data.content.should.eql([
'<p>foo</p>',
'<!--',
'{% raw %}',
'test',
'{% endraw %}',
'-->',
'<p>bar</p>',
''
].join('\n'));
});

// https://github.com/hexojs/hexo/issues/5433
it('render() - code fence nesting in comments', async () => {
const code = 'alert("Hello world")';
const highlighted = highlight(code);
const content = [
'foo',
'<!--',
'```',
code,
'```',
'-->',
'bar'
].join('\n');

const data = await post.render('', {
content,
engine: 'markdown'
});

data.content.should.eql([
'<p>foo</p>',
'<!--',
`<hexoPostRenderCodeBlock>${highlighted}</hexoPostRenderCodeBlock>`,

This comment was marked as off-topic.

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe we can solve it in backtick_code_block.ts

Copy link
Member

Choose a reason for hiding this comment

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

You are right. I forgot that the code block was processed before post render

'-->',
'<p>bar</p>',
''
].join('\n'));
});
});
Loading