Skip to content

Commit

Permalink
Correct processing of backtick code block on blockquote (fix Issue#2969)
Browse files Browse the repository at this point in the history
This patch makes the filter "backtick_code_block" to remove '>' characters at the head of each line.
How many '>' characters should be removed?
It is decided by the number of '>' characters at the first line.
This patch works well even if there are too few '>' characters at the head of a line.
(Simply removes all '>' characters less than or equal to the number of '>' characters at the first lines.)
  • Loading branch information
seaoak committed Oct 14, 2019
1 parent 79bdc95 commit 894408a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
10 changes: 9 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,14 @@ function backtickCodeBlock(data) {
}
}

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 894408a

Please sign in to comment.