Skip to content

Commit a271bb4

Browse files
committed
refactor: collapse loop handling for perf
1 parent 420251e commit a271bb4

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

index.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -629,56 +629,40 @@ function parseTableRow(
629629

630630
state.inTable = true
631631

632-
const tableRow = [] as MarkdownToJSX.ParserResult[]
633-
let cells = [[]]
632+
let cells: MarkdownToJSX.ParserResult[][] = [[]]
634633
let acc = ''
635634

636635
function flush() {
637-
if (acc) {
638-
tableRow.push.apply(tableRow, parse(acc, state))
639-
acc = ''
640-
}
636+
if (!acc) return
637+
638+
const cell = cells[cells.length - 1]
639+
cell.push.apply(cell, parse(acc, state))
640+
acc = ''
641641
}
642642

643643
source
644644
.trim()
645645
// isolate situations where a pipe should be ignored (inline code, escaped, etc)
646646
.split(/( *(?:`[^`]*`|\\\||\|) *)/)
647-
.forEach(fragment => {
647+
.filter(Boolean)
648+
.forEach((fragment, i, arr) => {
648649
if (fragment.trim() === '|') {
649650
flush()
650651

651-
tableRow.push(
652-
tableOutput
653-
? { type: RuleType.tableSeparator }
654-
: { type: RuleType.text, text: fragment }
655-
)
652+
if (tableOutput && i !== 0 && i !== arr.length - 1) {
653+
// Split the current row
654+
cells.push([])
655+
} else if (!tableOutput) {
656+
acc += fragment
657+
flush()
658+
}
656659
} else if (fragment !== '') {
657660
acc += fragment
658661
}
659662
})
660663

661664
flush()
662665

663-
tableRow.forEach(function (node, i) {
664-
if (node.type === RuleType.tableSeparator) {
665-
// Filter out empty table separators at the start/end:
666-
if (i !== 0 && i !== tableRow.length - 1) {
667-
// Split the current row:
668-
cells.push([])
669-
}
670-
} else {
671-
if (
672-
node.type === RuleType.text &&
673-
(tableRow[i + 1] == null ||
674-
tableRow[i + 1].type === RuleType.tableSeparator)
675-
) {
676-
node.text = node.text.trimEnd()
677-
}
678-
cells[cells.length - 1].push(node)
679-
}
680-
})
681-
682666
state.inTable = prevInTable
683667

684668
return cells

0 commit comments

Comments
 (0)