Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct processing of backtick code block on blockquote (fix Issue#2969) #3765

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
});
});

});