From dfe0d03de4ddf651b3b7b27557e4be8821ed137d Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:34:55 +0800 Subject: [PATCH 1/3] fix: check tag completeness --- lib/hexo/post.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/hexo/post.ts b/lib/hexo/post.ts index f9269484e2..cc74567d38 100644 --- a/lib/hexo/post.ts +++ b/lib/hexo/post.ts @@ -84,13 +84,14 @@ class PostRenderEscape { if (state === STATE_PLAINTEXT) { // From plain text to swig if (char === '{') { - if (next_char === '{') { + // check if it is a complete tag {{ }} + if (next_char === '{' && /\{\{.+?\}\}/.test(str)) { state = STATE_SWIG_VAR; idx++; - } else if (next_char === '#') { + } else if (next_char === '#' && /\{#.+?#\}/.test(str)) { state = STATE_SWIG_COMMENT; idx++; - } else if (next_char === '%') { + } else if (next_char === '%' && /\{%.+?%\}/.test(str)) { state = STATE_SWIG_TAG; idx++; swig_tag_name = ''; From 8fd34c9364cca1f49865e73bed4e5c0013ff2134 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Jan 2024 15:35:31 +0800 Subject: [PATCH 2/3] test(escapeTag): dont drop uncomplete tags --- test/scripts/hexo/post.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 6dd792f617..cf7dd423b2 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -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; @@ -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) {} + }); }); From 5dd70e8aeb30ea5968dd4bbbb3d03fdd7857a7c9 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:15:17 +0800 Subject: [PATCH 3/3] early return --- lib/hexo/post.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/hexo/post.ts b/lib/hexo/post.ts index cc74567d38..ed0c87a28e 100644 --- a/lib/hexo/post.ts +++ b/lib/hexo/post.ts @@ -67,6 +67,9 @@ class PostRenderEscape { * @returns string */ escapeAllSwigTags(str: string) { + if (!/(\{\{.+?\}\})|(\{#.+?#\})|(\{%.+?%\})/.test(str)) { + return str; + } let state = STATE_PLAINTEXT; let buffer = ''; let output = ''; @@ -85,13 +88,13 @@ class PostRenderEscape { if (state === STATE_PLAINTEXT) { // From plain text to swig if (char === '{') { // check if it is a complete tag {{ }} - if (next_char === '{' && /\{\{.+?\}\}/.test(str)) { + if (next_char === '{') { state = STATE_SWIG_VAR; idx++; - } else if (next_char === '#' && /\{#.+?#\}/.test(str)) { + } else if (next_char === '#') { state = STATE_SWIG_COMMENT; idx++; - } else if (next_char === '%' && /\{%.+?%\}/.test(str)) { + } else if (next_char === '%') { state = STATE_SWIG_TAG; idx++; swig_tag_name = '';