Skip to content

Commit

Permalink
fix: Parsing error for code blocks in list items (#5617)
Browse files Browse the repository at this point in the history
Co-authored-by: Uiolee <[email protected]>
  • Loading branch information
D-Sketon and uiolee authored Feb 13, 2025
1 parent bc07e1a commit fbb69c3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type Hexo from '../../../hexo';
import type { RenderData } from '../../../types';

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

Expand Down
74 changes: 74 additions & 0 deletions test/scripts/filters/backtick_code_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,80 @@ describe('Backtick code block', () => {
codeBlock(data);
data.content.should.contain('\n\n# New line');
});

// https://github.com/hexojs/hexo/issues/5423
it('with ordered list', () => {
const data = {
content: [
'1. ``` js',
code,
'```',
'2. ``` js',
code,
'```'
].join('\n')
};

codeBlock(data);
data.content.should.eql([
'1. <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
'2. <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
].join('\n'));
});

// https://github.com/hexojs/hexo/issues/5423
it('with unordered list', () => {
let data = {
content: [
'- ``` js',
code,
'```',
'- ``` js',
code,
'```'
].join('\n')
};

codeBlock(data);
data.content.should.eql([
'- <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
'- <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
].join('\n'));

data = {
content: [
'* ``` js',
code,
'```',
'* ``` js',
code,
'```'
].join('\n')
};

codeBlock(data);
data.content.should.eql([
'* <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
'* <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
].join('\n'));

data = {
content: [
'+ ``` js',
code,
'```',
'+ ``` js',
code,
'```'
].join('\n')
};

codeBlock(data);
data.content.should.eql([
'+ <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
'+ <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
].join('\n'));
});
});

describe('prismjs', () => {
Expand Down

0 comments on commit fbb69c3

Please sign in to comment.