Skip to content

Commit

Permalink
fix(hexojs#4317): non-greedy regexp for tag escape (hexojs#4358)
Browse files Browse the repository at this point in the history
* fix(hexojs#4317): non-greedy regexp for tag escape
* test(post): reset highlight status
  • Loading branch information
SukkaW committed Jun 15, 2020
1 parent 5e0fba9 commit edef5c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ class Tag {

const escapeContent = str => `<!--${placeholder}${cache.push(str) - 1}-->`;

str = str.replace(/<pre><code.*>[\s\S]*?<\/code><\/pre>/gm, escapeContent);
// FIXME: The behavoir of hexo-renderer-marked has been changed.
// There will be no "class" for <code> tag
// See: https://github.com/hexojs/hexo-renderer-marked/pull/134
str = str.replace(/<pre><code.*?>[\s\S]*?<\/code><\/pre>/gm, escapeContent);

return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); })
.catch(err => Promise.reject(formatNunjucksError(err, str)))
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/post_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ exports.expected_for_issue_3346 = [
'<p>quote content</p>\n',
'</blockquote>'
].join('');

exports.content_for_issue_4317 = [
'```sh',
'echo "Hi"',
'',
'```',
'{% gist gist_id %}',
'',
'```sh',
'echo "Hi"',
'```',
'',
'{% gist gist_id_2 %}',
'',
'```sh',
'echo "Hi"',
'```'
].join('\n');
23 changes: 22 additions & 1 deletion test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -1029,4 +1029,25 @@ describe('Post', () => {

data.content.trim().should.eql('<p><code>&amp;#123;&amp;#123; 1 + 1 &amp;#125;&amp;#125;</code> 2</p>');
});

// 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(`<pre><code class="sh">${escapeHTML('echo "Hi"')}</code></pre>`);
data.content.trim().should.contains('<script src="//gist.github.com/gist_id.js"></script>');
data.content.trim().should.contains('<script src="//gist.github.com/gist_id_2.js"></script>');

// Re-anable highlight for other tests
hexo.config.highlight.enable = true;
});
});

0 comments on commit edef5c2

Please sign in to comment.