Skip to content

Commit

Permalink
refactor(post): simplify the <escape> regex pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Mar 4, 2020
1 parent 32d5638 commit c21e2d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 50 deletions.
34 changes: 2 additions & 32 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const assert = require('assert');
const moment = require('moment');
const Promise = require('bluebird');
const { join, extname } = require('path');
Expand All @@ -11,29 +10,6 @@ const { copyDir, exists, listDir, mkdirs, readFile, rmdir, unlink, writeFile } =
const yfm = require('hexo-front-matter');

const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content'];
const rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
const rPlaceholder = /(?:<|&lt;)!--hexoPostRenderEscape:(\d+)--(?:>|&gt;)/g;

class PostRenderCache {
constructor() {
this.cache = [];
}

escapeContent(str) {
return str.replace(rEscapeContent, (_, content) => `<!--hexoPostRenderEscape:${this.cache.push(content) - 1}-->`);
}

loadContent(str) {
const restored = str.replace(rPlaceholder, (_, index) => {
assert(this.cache[index]);
const value = this.cache[index];
this.cache[index] = null;
return value;
});
if (restored === str) return restored;
return this.loadContent(restored); // self-recursive for nexted escaping
}
}

const prepareFrontMatter = data => {
for (const [key, item] of Object.entries(data)) {
Expand Down Expand Up @@ -230,17 +206,12 @@ class Post {
// disable Nunjucks when the renderer spcify that.
const disableNunjucks = ext && ctx.render.renderer.get(ext) && !!ctx.render.renderer.get(ext).disableNunjucks;

const cacheObj = new PostRenderCache();

return promise.then(content => {
data.content = content;

// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, { context: ctx });
}).then(() => {
// Escape content wrapped in <escape></escape>
data.content = cacheObj.escapeContent(data.content);

if (isSwig) {
// Render with Nunjucks if the post is a swig file
return tag.render(data.content, data);
Expand All @@ -256,9 +227,8 @@ class Post {
engine: data.engine,
toString: true,
onRenderEnd(content) {
// Replace cache data with real contents
data.content = cacheObj.loadContent(content);

data.content = content.replace(/<!--hexoBacktickCodeBlockEscape:/g, '')
.replace(/:hexoBacktickCodeBlockEscape-->/g, '');
return data.content;
}
}, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function backtickCodeBlock(data) {
.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');

return `${start}<escape>${content}</escape>${end}`;
return `${start}<!--hexoBacktickCodeBlockEscape:${content}:hexoBacktickCodeBlockEscape-->${end}`;
});
}

Expand Down
34 changes: 17 additions & 17 deletions test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('<escape>' + highlight(code, {lang: 'js'}) + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + highlight(code, {lang: 'js'}) + ':hexoBacktickCodeBlockEscape-->');
});

it('without language name', () => {
Expand All @@ -82,7 +82,7 @@ describe('Backtick code block', () => {
const expected = highlight(code);

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('without language name - ignore tab character', () => {
Expand All @@ -97,7 +97,7 @@ describe('Backtick code block', () => {
const expected = highlight(code);

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('title', () => {
Expand All @@ -115,7 +115,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('url', () => {
Expand All @@ -133,7 +133,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('link text', () => {
Expand All @@ -151,7 +151,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('indent', () => {
Expand All @@ -171,7 +171,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number false', () => {
Expand All @@ -191,7 +191,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number false, don`t first_line_number always1', () => {
Expand All @@ -212,7 +212,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number false, don`t care first_line_number inilne', () => {
Expand All @@ -233,7 +233,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number true', () => {
Expand All @@ -253,7 +253,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number, first_line_number always1, js=', () => {
Expand All @@ -275,7 +275,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number, first_line_number inline, js', () => {
Expand All @@ -297,7 +297,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number, first_line_number inline, js=1', () => {
Expand All @@ -319,7 +319,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('line number, first_line_number inline, js=2', () => {
Expand All @@ -341,7 +341,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('tab replace', () => {
Expand All @@ -367,7 +367,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + expected + ':hexoBacktickCodeBlockEscape-->');
});

it('wrap', () => {
Expand All @@ -382,7 +382,7 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('<escape>' + highlight(code, { lang: 'js', wrap: false }) + '</escape>');
data.content.should.eql('<!--hexoBacktickCodeBlockEscape:' + highlight(code, { lang: 'js', wrap: false }) + ':hexoBacktickCodeBlockEscape-->');

hexo.config.highlight.wrap = true;
});
Expand Down

0 comments on commit c21e2d0

Please sign in to comment.