Skip to content

Commit c1c5aaa

Browse files
authored
fix(escapeAllSwigTags): check tag completeness (#5395)
1 parent 86350d9 commit c1c5aaa

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/hexo/post.ts

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class PostRenderEscape {
6969
* @returns string
7070
*/
7171
escapeAllSwigTags(str: string) {
72+
if (!/(\{\{.+?\}\})|(\{#.+?#\})|(\{%.+?%\})/.test(str)) {
73+
return str;
74+
}
7275
let state = STATE_PLAINTEXT;
7376
let buffer = '';
7477
let output = '';
@@ -86,6 +89,7 @@ class PostRenderEscape {
8689

8790
if (state === STATE_PLAINTEXT) { // From plain text to swig
8891
if (char === '{') {
92+
// check if it is a complete tag {{ }}
8993
if (next_char === '{') {
9094
state = STATE_SWIG_VAR;
9195
idx++;

test/scripts/hexo/post.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}')
1212
describe('Post', () => {
1313
const Hexo = require('../../../dist/hexo');
1414
const hexo = new Hexo(join(__dirname, 'post_test'));
15-
require('../../../lib/plugins/highlight/')(hexo);
15+
require('../../../dist/plugins/highlight/')(hexo);
1616
const { post } = hexo;
1717
const now = Date.now();
1818
let clock;
@@ -1369,4 +1369,31 @@ describe('Post', () => {
13691369

13701370
hexo.config.syntax_highlighter = 'highlight.js';
13711371
});
1372+
1373+
// https://github.com/hexojs/hexo/issues/5301
1374+
it('render() - dont escape uncomplete tags', async () => {
1375+
const content = 'dont drop `{% }` 11111 `{# }` 22222 `{{ }` 33333';
1376+
1377+
const data = await post.render(null, {
1378+
content,
1379+
engine: 'markdown'
1380+
});
1381+
1382+
data.content.should.contains('11111');
1383+
data.content.should.contains('22222');
1384+
data.content.should.contains('33333');
1385+
data.content.should.not.contains('`'); // `
1386+
});
1387+
1388+
it('render() - uncomplete tags throw error', async () => {
1389+
const content = 'nunjucks should thorw {# } error';
1390+
1391+
try {
1392+
await post.render(null, {
1393+
content,
1394+
engine: 'markdown'
1395+
});
1396+
should.fail();
1397+
} catch (err) {}
1398+
});
13721399
});

0 commit comments

Comments
 (0)