Skip to content
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

fix(escapeAllSwigTags): check tag completeness #5395

Merged
merged 3 commits into from
Jan 18, 2024
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
4 changes: 4 additions & 0 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class PostRenderEscape {
* @returns string
*/
escapeAllSwigTags(str: string) {
if (!/(\{\{.+?\}\})|(\{#.+?#\})|(\{%.+?%\})/.test(str)) {
return str;
}
let state = STATE_PLAINTEXT;
let buffer = '';
let output = '';
Expand All @@ -84,6 +87,7 @@ class PostRenderEscape {

if (state === STATE_PLAINTEXT) { // From plain text to swig
if (char === '{') {
// check if it is a complete tag {{ }}
if (next_char === '{') {
state = STATE_SWIG_VAR;
idx++;
Expand Down
29 changes: 28 additions & 1 deletion test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}')
describe('Post', () => {
const Hexo = require('../../../dist/hexo');
const hexo = new Hexo(join(__dirname, 'post_test'));
require('../../../lib/plugins/highlight/')(hexo);
require('../../../dist/plugins/highlight/')(hexo);
const { post } = hexo;
const now = Date.now();
let clock;
Expand Down Expand Up @@ -1369,4 +1369,31 @@ describe('Post', () => {

hexo.config.syntax_highlighter = 'highlight.js';
});

// https://github.com/hexojs/hexo/issues/5301
it('render() - dont escape uncomplete tags', async () => {
const content = 'dont drop `{% }` 11111 `{# }` 22222 `{{ }` 33333';

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

data.content.should.contains('11111');
data.content.should.contains('22222');
data.content.should.contains('33333');
data.content.should.not.contains('`'); // `
});

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

try {
await post.render(null, {
content,
engine: 'markdown'
});
should.fail();
} catch (err) {}
});
});
Loading