fix: table regex to match tables anywhere in text (not just at start)
This commit is contained in:
@@ -53,12 +53,15 @@ export function parseMarkdown(text: string): ParsedSegment[] {
|
|||||||
function parseInlineContent(text: string): ParsedSegment[] {
|
function parseInlineContent(text: string): ParsedSegment[] {
|
||||||
const segments: ParsedSegment[] = [];
|
const segments: ParsedSegment[] = [];
|
||||||
|
|
||||||
// Check for tables first
|
// Check for tables - match table pattern anywhere in text
|
||||||
const tableRegex = /^\|.+\|\n\|[-:\s|]+\|\n((?:\|.+\|\n?)*)/gm;
|
// Table pattern: | header | ... |\n|---|...|\n| row | ... |
|
||||||
|
const tableRegex = /\|.+\|\n\|[-:\s|]+\|\n((?:\|.+\|\n?)*)/g;
|
||||||
|
let lastIndex = 0;
|
||||||
let tableMatch;
|
let tableMatch;
|
||||||
|
|
||||||
while ((tableMatch = tableRegex.exec(text)) !== null) {
|
while ((tableMatch = tableRegex.exec(text)) !== null) {
|
||||||
// Add content before table
|
// Add content before table
|
||||||
const beforeTable = text.substring(0, tableMatch.index);
|
const beforeTable = text.substring(lastIndex, tableMatch.index);
|
||||||
if (beforeTable.trim()) {
|
if (beforeTable.trim()) {
|
||||||
segments.push(...parseLines(beforeTable));
|
segments.push(...parseLines(beforeTable));
|
||||||
}
|
}
|
||||||
@@ -66,15 +69,22 @@ function parseInlineContent(text: string): ParsedSegment[] {
|
|||||||
// Parse table
|
// Parse table
|
||||||
const tableContent = tableMatch[0];
|
const tableContent = tableMatch[0];
|
||||||
const tableSegments = parseTable(tableContent);
|
const tableSegments = parseTable(tableContent);
|
||||||
segments.push(...tableSegments);
|
if (tableSegments.length > 0) {
|
||||||
|
segments.push(...tableSegments);
|
||||||
|
} else {
|
||||||
|
// If table parsing failed, treat as text
|
||||||
|
segments.push(...parseLines(tableContent));
|
||||||
|
}
|
||||||
|
|
||||||
// Update text for next iteration
|
lastIndex = tableMatch.index + tableContent.length;
|
||||||
text = text.substring(tableMatch.index + tableContent.length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add remaining content
|
// Add remaining content
|
||||||
if (text.trim()) {
|
if (lastIndex < text.length) {
|
||||||
segments.push(...parseLines(text));
|
const remaining = text.substring(lastIndex);
|
||||||
|
if (remaining.trim()) {
|
||||||
|
segments.push(...parseLines(remaining));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return segments;
|
return segments;
|
||||||
|
|||||||
Reference in New Issue
Block a user