diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 701802bb62..778f3548df 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -4,9 +4,8 @@ const { stripIndent } = require('hexo-util'); const { cyan } = require('chalk'); const { Environment } = require('nunjucks'); const Promise = require('bluebird'); -const placeholder = '\uFFFC'; -const rPlaceholder = //g; const rSwigRawFullBlock = /{% *raw *%}[\s\S]+{% *endraw *%}/; +const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); class NunjucksTag { constructor(name, fn) { @@ -229,20 +228,15 @@ class Tag { options = {}; } - const cache = []; - - const escapeContent = str => { + str = str.replace(/]*>[\s\S]+?<\/code>/gm, str => { // https://hexo.io/docs/tag-plugins#Raw // Only escape code block when there is no raw tag included if (rSwigRawFullBlock.test(str)) return str; - return ``; - }; - - str = str.replace(/]*>[\s\S]+?<\/code>/gm, escapeContent); + return escapeSwigTag(str); + }); return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); }) .catch(err => Promise.reject(formatNunjucksError(err, str))) - .then(result => result.replace(rPlaceholder, (_, index) => cache[index])) .asCallback(callback); } } diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 42dd113e2b..67f97c55f0 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -8,6 +8,7 @@ const { highlight, escapeHTML } = require('hexo-util'); const { spy, useFakeTimers } = require('sinon'); const frontMatter = require('hexo-front-matter'); const fixture = require('../../fixtures/post_render'); +const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); describe('Post', () => { const Hexo = require('../../../lib/hexo'); @@ -1015,7 +1016,7 @@ describe('Post', () => { engine: 'markdown' }); - data.content.trim().should.eql('

In Go’s templates, blocks look like this: {{block "template name" .}} (content) {{end}}.

'); + data.content.trim().should.eql(`

In Go’s templates, blocks look like this: ${escapeSwigTag('{{block "template name" .}} (content) {{end}}')}.

`); }); // test for https://github.com/hexojs/hexo/issues/3346#issuecomment-595497849 @@ -1027,7 +1028,7 @@ describe('Post', () => { engine: 'markdown' }); - data.content.trim().should.eql('

{{ 1 + 1 }} 2

'); + data.content.trim().should.eql(`

${escapeSwigTag('{{ 1 + 1 }}')} 2

`); }); // https://github.com/hexojs/hexo/issues/4317 @@ -1099,7 +1100,7 @@ describe('Post', () => { }); data.content.trim().should.eql([ - '

{{ 1 + 1 }} 3 {{ 2 + 2 }}
Text

', + `

${escapeSwigTag('{{ 1 + 1 }}')} 3 ${escapeSwigTag('{{ 2 + 2 }}')}
Text

`, '', 'Raw 1', '', @@ -1136,13 +1137,13 @@ describe('Post', () => { }); // indented pullquote - data.content.trim().should.contains('
{% pullquote %}foo foo foo{% endpullquote %}
'); + data.content.trim().should.contains(`
${escapeSwigTag('{% pullquote %}foo foo foo{% endpullquote %}')}
`); data.content.trim().should.contains('

test001

'); // pullquote tag data.content.trim().should.contains('

bar bar bar

\n
'); data.content.trim().should.contains('

test002

'); // indented youtube tag - data.content.trim().should.contains('
{% youtube https://example.com/demo.mp4 %}
'); + data.content.trim().should.contains(`
${escapeSwigTag('{% youtube https://example.com/demo.mp4 %}')}
`); // youtube tag data.content.trim().should.contains('
'); });