Skip to content

Commit 6b329e9

Browse files
authored
Merge pull request #3765 from seaoak/feature/correct_filter_backtick_code_block_on_blockquote
fix: Correct processing of backtick code block on blockquote (fix Issue#2969)
2 parents 79bdc95 + deaad6b commit 6b329e9

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lib/plugins/filter/before_post_render/backtick_code_block.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const stripIndent = require('strip-indent');
44
const { highlight } = require('hexo-util');
55

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

@@ -50,6 +50,15 @@ function backtickCodeBlock(data) {
5050
}
5151
}
5252

53+
// PR #3765
54+
const endOfStart = start.split('\n').pop();
55+
if (endOfStart && endOfStart.includes('>')) {
56+
const depth = endOfStart.split('>').length - 1;
57+
const regexp = new RegExp(`^(\\s*>){0,${depth}}\\s`, 'mg');
58+
const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
59+
content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
60+
}
61+
5362
content = highlight(stripIndent(content), options)
5463
.replace(/{/g, '{')
5564
.replace(/}/g, '}');

test/scripts/hexo/post.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,67 @@ describe('Post', () => {
739739
].join('\n'));
740740
});
741741
});
742+
743+
// test for Issue #2969
744+
it('render() - backtick cocde block in blockquote', () => {
745+
const code = 'alert("Hello world")';
746+
const highlighted = util.highlight(code);
747+
const quotedContent = [
748+
'This is a code-block',
749+
'',
750+
'```',
751+
code,
752+
'```'
753+
];
754+
755+
const content = [
756+
'Hello',
757+
'',
758+
...quotedContent.map(s => '> ' + s)
759+
].join('\n');
760+
761+
return post.render(null, {
762+
content,
763+
engine: 'markdown'
764+
}).then(data => {
765+
data.content.trim().should.eql([
766+
'<p>Hello</p>',
767+
'<blockquote>',
768+
'<p>This is a code-block</p>',
769+
highlighted + '</blockquote>'
770+
].join('\n'));
771+
});
772+
});
773+
774+
// test derived from Issue #2969
775+
it('render() - "lang=dos" backtick cocde block in blockquote', () => {
776+
const code = '> dir';
777+
const highlighted = util.highlight(code);
778+
const quotedContent = [
779+
'This is a code-block',
780+
'',
781+
'```',
782+
code,
783+
'```'
784+
];
785+
786+
const content = [
787+
'Hello',
788+
'',
789+
...quotedContent.map(s => '> ' + s)
790+
].join('\n');
791+
792+
return post.render(null, {
793+
content,
794+
engine: 'markdown'
795+
}).then(data => {
796+
data.content.trim().should.eql([
797+
'<p>Hello</p>',
798+
'<blockquote>',
799+
'<p>This is a code-block</p>',
800+
highlighted + '</blockquote>'
801+
].join('\n'));
802+
});
803+
});
804+
742805
});

0 commit comments

Comments
 (0)