Skip to content

Commit

Permalink
Merge pull request #3765 from seaoak/feature/correct_filter_backtick_…
Browse files Browse the repository at this point in the history
…code_block_on_blockquote

fix: Correct processing of backtick code block on blockquote (fix Issue#2969)
  • Loading branch information
curbengh authored Oct 14, 2019
2 parents 79bdc95 + deaad6b commit 6b329e9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const stripIndent = require('strip-indent');
const { highlight } = require('hexo-util');

const rBacktick = /(\s*)(`{3,}|~{3,}) *(.*) *\n([\s\S]+?)\s*\2(\n+|$)/g;
const rBacktick = /^((?:\s*>){0,3}\s*)(`{3,}|~{3,}) *(.*) *\n([\s\S]+?)\s*\2(\n+|$)/gm;
const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/;
const rLangCaption = /([^\s]+)\s*(.+)?/;

Expand Down Expand Up @@ -50,6 +50,15 @@ function backtickCodeBlock(data) {
}
}

// PR #3765
const endOfStart = start.split('\n').pop();
if (endOfStart && endOfStart.includes('>')) {
const depth = endOfStart.split('>').length - 1;
const regexp = new RegExp(`^(\\s*>){0,${depth}}\\s`, 'mg');
const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
}

content = highlight(stripIndent(content), options)
.replace(/{/g, '{')
.replace(/}/g, '}');
Expand Down
63 changes: 63 additions & 0 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,67 @@ describe('Post', () => {
].join('\n'));
});
});

// test for Issue #2969
it('render() - backtick cocde block in blockquote', () => {
const code = 'alert("Hello world")';
const highlighted = util.highlight(code);
const quotedContent = [
'This is a code-block',
'',
'```',
code,
'```'
];

const content = [
'Hello',
'',
...quotedContent.map(s => '> ' + s)
].join('\n');

return post.render(null, {
content,
engine: 'markdown'
}).then(data => {
data.content.trim().should.eql([
'<p>Hello</p>',
'<blockquote>',
'<p>This is a code-block</p>',
highlighted + '</blockquote>'
].join('\n'));
});
});

// test derived from Issue #2969
it('render() - "lang=dos" backtick cocde block in blockquote', () => {
const code = '> dir';
const highlighted = util.highlight(code);
const quotedContent = [
'This is a code-block',
'',
'```',
code,
'```'
];

const content = [
'Hello',
'',
...quotedContent.map(s => '> ' + s)
].join('\n');

return post.render(null, {
content,
engine: 'markdown'
}).then(data => {
data.content.trim().should.eql([
'<p>Hello</p>',
'<blockquote>',
'<p>This is a code-block</p>',
highlighted + '</blockquote>'
].join('\n'));
});
});

});

0 comments on commit 6b329e9

Please sign in to comment.