Skip to content

Commit 6c1bfcc

Browse files
committed
Keep blank lines in backtick code block on blockquote (fix hexojs#3769)
Blank lines in backtick code block on blockquote should be kept. But `backtick_code_block.js` removes them. This patch fixes this behavior. This bug is introduced by the PR hexojs#3765
1 parent 9662366 commit 6c1bfcc

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

lib/plugins/filter/before_post_render/backtick_code_block.js

Lines changed: 4 additions & 5 deletions
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*>){0,3}\s*)(`{3,}|~{3,}) *(.*) *\n([\s\S]+?)\s*\2(\n+|$)/gm;
6+
const rBacktick = /^((?:[^\S\r\n]*>){0,3}[^\S\r\n]*)(`{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

@@ -51,10 +51,9 @@ function backtickCodeBlock(data) {
5151
}
5252

5353
// 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');
54+
if (start.includes('>')) {
55+
const depth = start.split('>').length - 1;
56+
const regexp = new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg');
5857
const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
5958
content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
6059
}

test/scripts/hexo/post.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,55 @@ describe('Post', () => {
837837
});
838838
});
839839

840+
// test for Issue #3769
841+
it('render() - blank lines in backtick cocde block in blockquote', () => {
842+
const code = [
843+
'',
844+
'',
845+
'',
846+
'{',
847+
' "test": 123',
848+
'',
849+
'',
850+
'}',
851+
''
852+
];
853+
const highlighted = util.highlight(code.join('\n'));
854+
const addQuote = s => '>' + (s ? ` ${s}` : '');
855+
const code2 = code.map((s, i) => {
856+
if (i === 0 || i === 2 || i === 6) return addQuote(s);
857+
return s;
858+
});
859+
const quotedContent = [
860+
'This is a code-block',
861+
'',
862+
'> ```',
863+
...code2,
864+
'```',
865+
'',
866+
'This is a following paragraph'
867+
];
868+
const content = [
869+
'Hello',
870+
'',
871+
...quotedContent.map(addQuote)
872+
].join('\n');
873+
874+
return post.render(null, {
875+
content,
876+
engine: 'markdown'
877+
}).then(data => {
878+
data.content.trim().should.eql([
879+
'<p>Hello</p>',
880+
'<blockquote>',
881+
'<p>This is a code-block</p>',
882+
'<blockquote>',
883+
highlighted.replace('{', '&#123;').replace('}', '&#125;'),
884+
'</blockquote>',
885+
'<p>This is a following paragraph</p>',
886+
'</blockquote>'
887+
].join('\n'));
888+
});
889+
});
890+
840891
});

0 commit comments

Comments
 (0)