Skip to content

Commit

Permalink
Fix table parsing ignoring empty cells
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jul 30, 2024
1 parent 2b62071 commit df60408
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class BlockTokenParser(private val context: MutableContext) : BlockTokenVisitor<
*/
fun splitRow(row: String): Sequence<String> =
row.split("(?<!\\\\)\\|".toRegex()).asSequence()
.filterNot { it.isBlank() }
.filter { it.isNotEmpty() }
.map { it.trim() }

/**
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/kotlin/eu/iamgio/quarkdown/BlockParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import eu.iamgio.quarkdown.ast.Node
import eu.iamgio.quarkdown.ast.OrderedList
import eu.iamgio.quarkdown.ast.PageBreak
import eu.iamgio.quarkdown.ast.Paragraph
import eu.iamgio.quarkdown.ast.Strong
import eu.iamgio.quarkdown.ast.Table
import eu.iamgio.quarkdown.ast.TaskListItem
import eu.iamgio.quarkdown.ast.Text
Expand Down Expand Up @@ -366,6 +367,32 @@ class BlockParserTest {
assertFalse(hasNext())
}

with(nodes.next().columns.iterator()) {
with(next()) {
assertEquals(Table.Alignment.LEFT, alignment)
assertTrue(header.text.isEmpty())
assertEquals(2, cells.size)
assertEquals(Strong(listOf(Text("C"))), cells[0].text.first())
assertEquals(Strong(listOf(Text("D"))), cells[1].text.first())
}
with(next()) {
assertEquals(Table.Alignment.NONE, alignment)
assertEquals(Text("A"), header.text.first())
assertEquals(2, cells.size)
assertEquals(Text("AC"), cells[0].text.first())
assertEquals(Text("AD"), cells[1].text.first())
}
with(next()) {
assertEquals(Table.Alignment.RIGHT, alignment)
assertEquals(Text("B"), header.text.first())
assertEquals(2, cells.size)
assertEquals(Text("BC"), cells[0].text.first())
assertEquals(Text("BD"), cells[1].text.first())
}

assertFalse(hasNext())
}

assertFalse(nodes.hasNext())
}

Expand Down
7 changes: 6 additions & 1 deletion core/src/test/resources/parsing/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ bar | baz
| abc | def |
| --- |:----|
| bar |
| bar | baz | boo |
| bar | baz | boo |

| | A | B |
|:------|----|---:|
| **C** | AC | BC |
| **D** | AD | BD |

0 comments on commit df60408

Please sign in to comment.