feat: Improve Markdown parser list and table detection

- Enhance the accuracy of list detection to correctly identify
  ordered, unordered, and task lists.
- Improve table detection by ensuring a valid separator line
  exists before confirming a table.
- Fix a bug in footnote definition detection to handle cases
  where the closing bracket is missing.
This commit is contained in:
Mahmoud Emad
2025-03-17 22:46:26 +02:00
parent 04ee73e8dd
commit f2138f104f

View File

@@ -23,8 +23,8 @@ fn (p Parser) is_list_start() bool {
}
// Unordered list: *, -, +
if (p.text[p.pos] == `*` || p.text[p.pos] == `-` || p.text[p.pos] == `+`) &&
(p.peek(1) == ` ` || p.peek(1) == `\t`) {
if (p.text[p.pos] == `*` || p.text[p.pos] == `-` || p.text[p.pos] == `+`)
&& (p.peek(1) == ` ` || p.peek(1) == `\t`) {
return true
}
@@ -34,17 +34,18 @@ fn (p Parser) is_list_start() bool {
for i < p.text.len && p.text[i].is_digit() {
i++
}
if i < p.text.len && p.text[i] == `.` && i + 1 < p.text.len && (p.text[i + 1] == ` ` || p.text[i + 1] == `\t`) {
if i < p.text.len && p.text[i] == `.` && i + 1 < p.text.len
&& (p.text[i + 1] == ` ` || p.text[i + 1] == `\t`) {
return true
}
}
// Task list: - [ ], - [x], etc.
if p.pos + 4 < p.text.len &&
(p.text[p.pos] == `-` || p.text[p.pos] == `*` || p.text[p.pos] == `+`) &&
p.text[p.pos + 1] == ` ` && p.text[p.pos + 2] == `[` &&
(p.text[p.pos + 3] == ` ` || p.text[p.pos + 3] == `x` || p.text[p.pos + 3] == `X`) &&
p.text[p.pos + 4] == `]` {
if p.pos + 4 < p.text.len
&& (p.text[p.pos] == `-` || p.text[p.pos] == `*` || p.text[p.pos] == `+`)
&& p.text[p.pos + 1] == ` ` && p.text[p.pos + 2] == `[`
&& (p.text[p.pos + 3] == ` ` || p.text[p.pos + 3] == `x` || p.text[p.pos + 3] == `X`)
&& p.text[p.pos + 4] == `]` {
return true
}
@@ -80,7 +81,8 @@ fn (p Parser) is_table_start() bool {
}
// Skip whitespace at the beginning of the next line
for next_line_start < p.text.len && (p.text[next_line_start] == ` ` || p.text[next_line_start] == `\t`) {
for next_line_start < p.text.len
&& (p.text[next_line_start] == ` ` || p.text[next_line_start] == `\t`) {
next_line_start++
}
@@ -93,8 +95,8 @@ fn (p Parser) is_table_start() bool {
mut j := next_line_start + 1
for j < p.text.len && p.text[j] != `\n` {
// Only allow -, |, :, space, or tab in the separator line
if p.text[j] != `-` && p.text[j] != `|` && p.text[j] != `:` &&
p.text[j] != ` ` && p.text[j] != `\t` {
if p.text[j] != `-` && p.text[j] != `|` && p.text[j] != `:` && p.text[j] != ` `
&& p.text[j] != `\t` {
return false
}
j++
@@ -109,7 +111,10 @@ fn (p Parser) is_footnote_definition() bool {
return false
}
// Check for pattern like [^id]:
return p.text[p.pos] == `[` && p.text[p.pos + 1] == `^` &&
p.text[p.pos + 2] != `]` && p.text.index_after(']:', p.pos + 2) > p.pos + 2
if idx := p.text.index_after(']:', p.pos + 2) {
return p.text[p.pos] == `[` && p.text[p.pos + 1] == `^` && p.text[p.pos + 2] != `]`
&& idx > p.pos + 2
} else {
return false
}
}