Skip to content

Commit fbb69c3

Browse files
D-Sketonuiolee
andauthoredFeb 13, 2025
fix: Parsing error for code blocks in list items (hexojs#5617)
Co-authored-by: Uiolee <22849383+uiolee@users.noreply.github.com>
1 parent bc07e1a commit fbb69c3

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed
 

‎lib/plugins/filter/before_post_render/backtick_code_block.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type Hexo from '../../../hexo';
22
import type { RenderData } from '../../../types';
33

4-
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;
4+
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;
55
const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/;
66
const rLangCaption = /([^\s]+)\s*(.+)?/;
77

‎test/scripts/filters/backtick_code_block.ts

+74
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,80 @@ describe('Backtick code block', () => {
504504
codeBlock(data);
505505
data.content.should.contain('\n\n# New line');
506506
});
507+
508+
// https://github.com/hexojs/hexo/issues/5423
509+
it('with ordered list', () => {
510+
const data = {
511+
content: [
512+
'1. ``` js',
513+
code,
514+
'```',
515+
'2. ``` js',
516+
code,
517+
'```'
518+
].join('\n')
519+
};
520+
521+
codeBlock(data);
522+
data.content.should.eql([
523+
'1. <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
524+
'2. <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
525+
].join('\n'));
526+
});
527+
528+
// https://github.com/hexojs/hexo/issues/5423
529+
it('with unordered list', () => {
530+
let data = {
531+
content: [
532+
'- ``` js',
533+
code,
534+
'```',
535+
'- ``` js',
536+
code,
537+
'```'
538+
].join('\n')
539+
};
540+
541+
codeBlock(data);
542+
data.content.should.eql([
543+
'- <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
544+
'- <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
545+
].join('\n'));
546+
547+
data = {
548+
content: [
549+
'* ``` js',
550+
code,
551+
'```',
552+
'* ``` js',
553+
code,
554+
'```'
555+
].join('\n')
556+
};
557+
558+
codeBlock(data);
559+
data.content.should.eql([
560+
'* <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
561+
'* <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
562+
].join('\n'));
563+
564+
data = {
565+
content: [
566+
'+ ``` js',
567+
code,
568+
'```',
569+
'+ ``` js',
570+
code,
571+
'```'
572+
].join('\n')
573+
};
574+
575+
codeBlock(data);
576+
data.content.should.eql([
577+
'+ <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>',
578+
'+ <hexoPostRenderCodeBlock>' + highlight(code, { lang: 'js' }) + '</hexoPostRenderCodeBlock>'
579+
].join('\n'));
580+
});
507581
});
508582

509583
describe('prismjs', () => {

0 commit comments

Comments
 (0)