diff --git a/lib/extend/tag.js b/lib/extend/tag.js index a99e4345d3..6eed6e5aa7 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -5,6 +5,7 @@ const { cyan, magenta, red } = require('chalk'); const { Environment } = require('nunjucks'); const Promise = require('bluebird'); const rSwigRawFullBlock = /{% *raw *%}/; +const rCodeTag = /]*>[\s\S]+?<\/code>/g; const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); class NunjucksTag { @@ -231,15 +232,18 @@ class Tag { // Get path of post from source const { source = '' } = options; - str = str.replace(/]*>[\s\S]+?<\/code>/g, str => { - // https://hexo.io/docs/tag-plugins#Raw - // https://mozilla.github.io/nunjucks/templating.html#raw - // Only escape code block when there is no raw tag included - return str.match(rSwigRawFullBlock) ? str : escapeSwigTag(str); - }); - - return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); }) - .catch(err => Promise.reject(formatNunjucksError(err, str, source))) + return Promise.fromCallback(cb => { + this.env.renderString( + str.replace(rCodeTag, s => { + // https://hexo.io/docs/tag-plugins#Raw + // https://mozilla.github.io/nunjucks/templating.html#raw + // Only escape code block when there is no raw tag included + return s.match(rSwigRawFullBlock) ? s : escapeSwigTag(s); + }), + options, + cb + ); + }).catch(err => Promise.reject(formatNunjucksError(err, str, source))) .asCallback(callback); } } diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 3713e6a3c9..3f430f8591 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -13,9 +13,7 @@ const yfm = require('hexo-front-matter'); const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content']; const rPlaceholder = /(?:<|<)!--\uFFFC(\d+)--(?:>|>)/g; -const rSwigVar = /{{[\s\S]+?}}/g; -const rSwigComment = /{#[\s\S]+?#}/g; -const rSwigBlock = /{%[\s\S]+?%}/g; +const rSwigBlock = /{[{#%][\s\S]+?[}#%]}/g; const rSwigFullBlock = /{% *(\S+?)(?: *| +.+?)%}[\s\S]+?{% *end\1 *%}/g; const _escapeContent = (cache, str) => ``; @@ -42,9 +40,7 @@ class PostRenderCache { escapeAllSwigTags(str) { const escape = _str => _escapeContent(this.cache, _str); - return str.replace(rSwigComment, '') // Remove swig comment first to reduce string size being matched next - .replace(rSwigVar, escape) // Escape swig var since it is a simple regexp - .replace(rSwigFullBlock, escape) // swig full block must escaped before swig block to avoid confliction + return str.replace(rSwigFullBlock, escape) // swig full block must escaped before swig block to avoid confliction .replace(rSwigBlock, escape); } } @@ -277,7 +273,6 @@ class Post { } }, options); }).then(content => { - // restore { and } inside inline code data.content = content; // Run "after_post_render" filters