From edef5c2a9c1217aaab02f64397e7e94d2f98e4fa Mon Sep 17 00:00:00 2001 From: Sukka Date: Mon, 15 Jun 2020 23:24:14 +0800 Subject: [PATCH] fix(#4317): non-greedy regexp for tag escape (#4358) * fix(#4317): non-greedy regexp for tag escape * test(post): reset highlight status --- lib/extend/tag.js | 5 ++++- test/fixtures/post_render.js | 18 ++++++++++++++++++ test/scripts/hexo/post.js | 23 ++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 864020d909..ba83da9d33 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -232,7 +232,10 @@ class Tag { const escapeContent = str => ``; - str = str.replace(/
[\s\S]*?<\/code><\/pre>/gm, escapeContent);
+    // FIXME: The behavoir of hexo-renderer-marked has been changed.
+    // There will be no "class" for  tag
+    // See: https://github.com/hexojs/hexo-renderer-marked/pull/134
+    str = str.replace(/
[\s\S]*?<\/code><\/pre>/gm, escapeContent);
 
     return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); })
       .catch(err => Promise.reject(formatNunjucksError(err, str)))
diff --git a/test/fixtures/post_render.js b/test/fixtures/post_render.js
index a03afe7f09..d8f4cca4c5 100644
--- a/test/fixtures/post_render.js
+++ b/test/fixtures/post_render.js
@@ -72,3 +72,21 @@ exports.expected_for_issue_3346 = [
   '

quote content

\n', '' ].join(''); + +exports.content_for_issue_4317 = [ + '```sh', + 'echo "Hi"', + '', + '```', + '{% gist gist_id %}', + '', + '```sh', + 'echo "Hi"', + '```', + '', + '{% gist gist_id_2 %}', + '', + '```sh', + 'echo "Hi"', + '```' +].join('\n'); diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 33242bf74f..88820e5fe1 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -4,7 +4,7 @@ const { join } = require('path'); const moment = require('moment'); const Promise = require('bluebird'); const { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } = require('hexo-fs'); -const { highlight } = require('hexo-util'); +const { highlight, escapeHTML } = require('hexo-util'); const { spy, useFakeTimers } = require('sinon'); const frontMatter = require('hexo-front-matter'); const fixture = require('../../fixtures/post_render'); @@ -1029,4 +1029,25 @@ describe('Post', () => { data.content.trim().should.eql('

&#123;&#123; 1 + 1 &#125;&#125; 2

'); }); + + // https://github.com/hexojs/hexo/issues/4317 + it('render() - issue #4317', async () => { + const content = fixture.content_for_issue_4317; + hexo.config.highlight.enable = false; + + const data = await post.render(null, { + content, + engine: 'markdown' + }); + + // FIXME: The behavoir of hexo-renderer-marked has been changed. + // class="sh" won't show up in next release of hexo-renderer-marked. + // See: https://github.com/hexojs/hexo-renderer-marked/pull/134 + data.content.trim().should.contains(`
${escapeHTML('echo "Hi"')}
`); + data.content.trim().should.contains(''); + data.content.trim().should.contains(''); + + // Re-anable highlight for other tests + hexo.config.highlight.enable = true; + }); });