Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,7 @@ class PostRenderEscape {
swig_string_quote = '';
}
}
// {% } or {% %
if (((char !== '%' && next_char === '}') || (char === '%' && next_char !== '}')) && swig_string_quote === '') {
// From swig back to plain text
swig_tag_name = '';
state = STATE_PLAINTEXT;
pushAndReset(`{%${str.slice(buffer_start, idx)}${char}`);
} else if (char === '%' && next_char === '}' && swig_string_quote === '') { // From swig back to plain text
if (char === '%' && next_char === '}' && swig_string_quote === '') { // From swig back to plain text
idx++;
if (swig_tag_name !== '' && str.includes(`end${swig_tag_name}`)) {
state = STATE_SWIG_FULL_TAG;
Expand Down
47 changes: 47 additions & 0 deletions test/scripts/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,53 @@ describe('Post', () => {
data.content.should.contains('22222');
});

it('render() - tags with swig character', async () => {
const tagSpy = spy();
hexo.extend.tag.register('testTag', (args, content) => {
tagSpy(args, content);
return '';
}, {
ends: true
});
let content = '{% testTag 111 222 %}\n3333\n{% endtestTag %}';
await post.render('', {
content,
engine: 'markdown'
});
tagSpy.calledOnce.should.be.true;
tagSpy.firstCall.args[0].should.eql(['111', '222']);
tagSpy.firstCall.args[1].should.eql('3333');

content = '{% testTag 111% % 222 %}\n333\n{% endtestTag %}';
await post.render('', {
content,
engine: 'markdown'
});
tagSpy.calledTwice.should.be.true;
tagSpy.secondCall.args[0].should.eql(['111%', '%', '222']);
tagSpy.secondCall.args[1].should.eql('333');

content = '{% testTag 111 } 222} %}\n333\n{% endtestTag %}';
await post.render('', {
content,
engine: 'markdown'
});
tagSpy.calledThrice.should.be.true;
tagSpy.thirdCall.args[0].should.eql(['111', '}', '222}']);
tagSpy.thirdCall.args[1].should.eql('333');

content = '{% testTag 111 222 %}\n333% % } %}\n{% endtestTag %}';
await post.render('', {
content,
engine: 'markdown'
});
tagSpy.callCount.should.eql(4);
tagSpy.getCall(3).args[0].should.eql(['111', '222']);
tagSpy.getCall(3).args[1].should.eql('333% % } %}');

hexo.extend.tag.unregister('testTag');
});

it('render() - incomplete tags throw error', async () => {
const content = 'nunjucks should throw {# } error';

Expand Down
Loading