Skip to content

Commit

Permalink
Merge pull request hexojs#3770 from seaoak/bugfix/keep_blank_lines_in…
Browse files Browse the repository at this point in the history
…_backtick_code_block_on_blockquote

fix(backtick_code): Keep blank lines in backtick code block on blockquote
  • Loading branch information
curbengh authored Nov 2, 2019
2 parents 19ac04c + 6c1bfcc commit 7bf79f3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
9 changes: 4 additions & 5 deletions 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*>){0,3}\s*)(`{3,}|~{3,}) *(.*) *\n([\s\S]+?)\s*\2(\n+|$)/gm;
const rBacktick = /^((?:[^\S\r\n]*>){0,3}[^\S\r\n]*)(`{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 @@ -51,10 +51,9 @@ 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');
if (start.includes('>')) {
const depth = start.split('>').length - 1;
const regexp = new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg');
const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
}
Expand Down
51 changes: 51 additions & 0 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,55 @@ describe('Post', () => {
});
});

// test for Issue #3769
it('render() - blank lines in backtick cocde block in blockquote', () => {
const code = [
'',
'',
'',
'{',
' "test": 123',
'',
'',
'}',
''
];
const highlighted = util.highlight(code.join('\n'));
const addQuote = s => '>' + (s ? ` ${s}` : '');
const code2 = code.map((s, i) => {
if (i === 0 || i === 2 || i === 6) return addQuote(s);
return s;
});
const quotedContent = [
'This is a code-block',
'',
'> ```',
...code2,
'```',
'',
'This is a following paragraph'
];
const content = [
'Hello',
'',
...quotedContent.map(addQuote)
].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>',
'<blockquote>',
highlighted.replace('{', '&#123;').replace('}', '&#125;'),
'</blockquote>',
'<p>This is a following paragraph</p>',
'</blockquote>'
].join('\n'));
});
});

});

0 comments on commit 7bf79f3

Please sign in to comment.