Skip to content

Commit

Permalink
Allow backtick code block in "blockquote" tag plugin (#2318)
Browse files Browse the repository at this point in the history
When backtick code block(s) exist as contents of a "blockquote" tag plugin,
each code block is translated to a string "undefined" in HTML (Issue #2318).

In analyzing markdown source text, while the replacement of these elements
with placeholders are nesting, recoveries from placeholders are executed
only once.  So I modify to repeat the recovery process until all
placeholders are recovered.
  • Loading branch information
seaoak committed Oct 11, 2019
1 parent 6f6084c commit 1c58748
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/hexo/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const moment = require('moment');
const Promise = require('bluebird');
const { join, extname } = require('path');
Expand Down Expand Up @@ -29,7 +30,14 @@ PostRenderCache.prototype.escapeContent = function(str) {

PostRenderCache.prototype.loadContent = function(str) {
const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
return str.replace(rPlaceholder, (_, index) => this.cache[index]);
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
};

PostRenderCache.prototype.escapeAllSwigTags = function(str) {
Expand Down
23 changes: 23 additions & 0 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,27 @@ describe('Post', () => {
});
});

// test for Issue [#2318](https://github.com/hexojs/hexo/issues/2318)
it('render() - allow backtick code block in "blockquote" tag plugn (Issue#2318)', () => {
const code = 'alert("Hello world")';
const highlighted = util.highlight(code);

const content = [
'{% blockquote %}',
'```',
code,
'```',
'{% endblockquote %}'
].join('\n');

return post.render(null, {
content
}).then(data => {
data.content.trim().should.eql([
'<blockquote>' + highlighted,
'</blockquote>'
].join('\n'));
});
});

});

0 comments on commit 1c58748

Please sign in to comment.