Skip to content
Open
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
15 changes: 9 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,28 @@ function formatMarkdownTables(text: string): string {
return result.join("\n")
}

// Split on `|` but not on `\|` (escaped pipe is literal content, not a delimiter)
function splitByUnescapedPipe(text: string): string[] {
return text.split(/(?<!\\)\|/)
}

function isTableRow(line: string): boolean {
const trimmed = line.trim()
return trimmed.startsWith("|") && trimmed.endsWith("|") && trimmed.split("|").length > 2
return trimmed.startsWith("|") && trimmed.endsWith("|") && splitByUnescapedPipe(trimmed).length > 2
}

function isSeparatorRow(line: string): boolean {
const trimmed = line.trim()
if (!trimmed.startsWith("|") || !trimmed.endsWith("|")) return false
const cells = trimmed.split("|").slice(1, -1)
const cells = splitByUnescapedPipe(trimmed).slice(1, -1)
return cells.length > 0 && cells.every((cell) => /^\s*:?-+:?\s*$/.test(cell))
}

function isValidTable(lines: string[]): boolean {
if (lines.length < 2) return false

const rows = lines.map((line) =>
line
.split("|")
splitByUnescapedPipe(line)
.slice(1, -1)
.map((cell) => cell.trim()),
)
Expand All @@ -96,8 +100,7 @@ function formatTable(lines: string[]): string[] {
}

const rows = lines.map((line) =>
line
.split("|")
splitByUnescapedPipe(line)
.slice(1, -1)
.map((cell) => cell.trim()),
)
Expand Down