Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const other = {
notSpaceStart: /^\S*/,
endingNewline: /\n$/,
listItemRegex: (bull: string) => new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`),
nextBulletRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`),
hrRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),
fencesBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`),
headingBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`),
htmlBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}<(?:[a-z].*>|!--)`, 'i'),
nextBulletRegex: (indent: number) => new RegExp(`^ {0,${indent}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`),
hrRegex: (indent: number) => new RegExp(`^ {0,${indent}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),
fencesBeginRegex: (indent: number) => new RegExp(`^ {0,${indent}}(?:\`\`\`|~~~)`),
headingBeginRegex: (indent: number) => new RegExp(`^ {0,${indent}}#`),
htmlBeginRegex: (indent: number) => new RegExp(`^ {0,${indent}}<(?:[a-z].*>|!--)`, 'i'),
};

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +
.replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
.getRegex();

const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/)
const list = edit(/^( {0,4}bull)([ \t][^\n]+?)?(?:\n|$)/)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

According to the CommonMark specification, a list marker can be indented by a maximum of 3 spaces. An indentation of 4 or more spaces should be interpreted as a code block. Changing this regex to allow 4 spaces ({0,4}) violates the spec and could lead to ambiguous parsing where indented code blocks are misinterpreted as lists. Please revert this to {0,3} to maintain CommonMark compliance.

Suggested change
const list = edit(/^( {0,4}bull)([ \t][^\n]+?)?(?:\n|$)/)
const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/)

.replace(/bull/g, bullet)
.getRegex();

Expand Down
49 changes: 20 additions & 29 deletions test/unit/Lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,15 +1045,15 @@ paragraph
items: [
{
type: 'list_item',
raw: '- item 1\n - item 2',
raw: '- item 1\n',
task: false,
checked: undefined,
loose: false,
text: 'item 1\n- item 2',
text: 'item 1',
tokens: [
{
type: 'text',
raw: 'item 1\n',
raw: 'item 1',
text: 'item 1',
tokens: [
{
Expand All @@ -1064,35 +1064,26 @@ paragraph
},
],
},
],
},
{
type: 'list_item',
raw: ' - item 2\n',
task: false,
checked: null,
loose: false,
text: 'item 2',
tokens: [
{
type: 'list',
raw: '- item 2',
ordered: false,
start: '',
loose: false,
items: [
type: 'text',
raw: 'item 2',
text: 'item 2',
tokens: [
{
type: 'list_item',
raw: '- item 2',
task: false,
checked: undefined,
loose: false,
type: 'text',
raw: 'item 2',
text: 'item 2',
tokens: [
{
type: 'text',
raw: 'item 2',
text: 'item 2',
tokens: [
{
type: 'text',
raw: 'item 2',
text: 'item 2',
escaped: false,
},
],
},
],
escaped: false,
},
],
},
Expand Down
25 changes: 25 additions & 0 deletions test/unit/nested-list-indentation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Marked } from '../../lib/marked.esm.js';
import assert from 'node:assert';
import { describe, it } from 'node:test';

describe('nested list indentation', () => {
const marked = new Marked();

it('should parse nested list with 4-space indentation correctly', () => {
const markdown = `- title
- desc
-`;
const expected = '<ul>\n<li>title</li>\n<li>desc</li>\n<li></li>\n</ul>\n';
const result = marked.parse(markdown);
assert.strictEqual(result, expected);
});

it('should parse nested list with 2-space indentation correctly', () => {
const markdown = `- title
- desc
-`;
const expected = '<ul>\n<li>title</li>\n<li>desc</li>\n<li></li>\n</ul>\n';
const result = marked.parse(markdown);
assert.strictEqual(result, expected);
});
});

Check failure on line 25 in test/unit/nested-list-indentation.test.js

View workflow job for this annotation

GitHub Actions / OtherTests

Newline required at end of file but not found
Loading