Skip to content

Commit

Permalink
perf(post): merge swig regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 24, 2020
1 parent c12772d commit d477f57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
22 changes: 13 additions & 9 deletions lib/extend/tag.js
Expand Up @@ -5,6 +5,7 @@ const { cyan, magenta, red } = require('chalk');
const { Environment } = require('nunjucks');
const Promise = require('bluebird');
const rSwigRawFullBlock = /{% *raw *%}/;
const rCodeTag = /<code[^<>]*>[\s\S]+?<\/code>/g;
const escapeSwigTag = str => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');

class NunjucksTag {
Expand Down Expand Up @@ -231,15 +232,18 @@ class Tag {
// Get path of post from source
const { source = '' } = options;

str = str.replace(/<code[^<>]*>[\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);
}
}
Expand Down
9 changes: 2 additions & 7 deletions lib/hexo/post.js
Expand Up @@ -13,9 +13,7 @@ const yfm = require('hexo-front-matter');
const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content'];

const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/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) => `<!--\uFFFC${cache.push(str) - 1}-->`;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -277,7 +273,6 @@ class Post {
}
}, options);
}).then(content => {
// restore { and } inside inline code
data.content = content;

// Run "after_post_render" filters
Expand Down

0 comments on commit d477f57

Please sign in to comment.