Skip to content

Commit ed00ee5

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Fixing logic and adding unit tests (#439)
Co-authored-by: Leandro Ferreira <[email protected]>
1 parent b43d418 commit ed00ee5

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

plugins/writeopia_import_document/src/commonMain/kotlin/io/writeopia/sdk/import/markdown/MarkdownParser.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object MarkdownParser {
1010

1111
fun parse(lines: List<String>): List<StoryStepApi> {
1212
var acc = -1
13+
// Take care when moving the code, order matters for comparations!
1314
return lines.map { it.trim() }
14-
.filter { it.isNotEmpty() }
1515
.mapIndexed { i, trimmed ->
1616
acc++
1717
when {
@@ -85,8 +85,17 @@ object MarkdownParser {
8585
)
8686
}
8787

88+
trimmed.startsWith("[] ") || trimmed.startsWith("-[] ") -> {
89+
val type = StoryTypes.CHECK_ITEM.type
90+
StoryStepApi(
91+
type = StoryTypeApi(type.name, type.number),
92+
text = trimmed.drop(3).trimStart(),
93+
position = acc
94+
)
95+
}
96+
8897
trimmed.startsWith("- ") || trimmed.startsWith("* ") -> {
89-
val type = StoryTypes.TEXT.type
98+
val type = StoryTypes.UNORDERED_LIST_ITEM.type
9099
StoryStepApi(
91100
type = StoryTypeApi(type.name, type.number),
92101
text = trimmed.drop(2).trimStart(),

plugins/writeopia_import_document/src/commonTest/kotlin/io/writeopia/sdk/imports/test/MarkdownParser.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,60 @@ class MarkdownParserTest {
9292

9393
assertEquals(results.first().type.number, StoryTypes.TITLE.type.number)
9494
}
95+
96+
@Test
97+
fun `empty lines should not be removed`() {
98+
val sample = listOf(
99+
"# Sample Markdown Document",
100+
"",
101+
"Welcome to this **Markdown** example!",
102+
)
103+
104+
val expected = listOf(
105+
StoryTypes.TITLE.type.number to "Sample Markdown Document",
106+
StoryTypes.TEXT.type.number to "",
107+
StoryTypes.TEXT.type.number to "Welcome to this **Markdown** example!",
108+
)
109+
110+
val results = MarkdownParser.parse(sample)
111+
assertEquals(results.size, 3)
112+
val parsedResults = results.map { storyStepApi ->
113+
storyStepApi.type.number to storyStepApi.text
114+
}
115+
116+
// It is necessary to compare like this because the ids won't match.
117+
assertEquals(expected, parsedResults)
118+
}
119+
120+
@Test
121+
fun `all types should be parsed`() {
122+
val sample = listOf(
123+
"# Sample Markdown Document",
124+
"",
125+
"[] Checkitem",
126+
"-[] Checkitem2",
127+
"- Item list",
128+
"#",
129+
"---",
130+
)
131+
132+
val expected = listOf(
133+
StoryTypes.TITLE.type.number to "Sample Markdown Document",
134+
StoryTypes.TEXT.type.number to "",
135+
StoryTypes.CHECK_ITEM.type.number to "Checkitem",
136+
StoryTypes.CHECK_ITEM.type.number to "Checkitem2",
137+
StoryTypes.UNORDERED_LIST_ITEM.type.number to "Item list",
138+
StoryTypes.TEXT.type.number to "",
139+
StoryTypes.DIVIDER.type.number to null,
140+
)
141+
142+
val results = MarkdownParser.parse(sample)
143+
assertEquals(results.size, 7)
144+
val parsedResults = results.map { storyStepApi ->
145+
storyStepApi.type.number to storyStepApi.text
146+
}
147+
148+
// It is necessary to compare like this because the ids won't match.
149+
assertEquals(expected, parsedResults)
150+
}
95151
}

0 commit comments

Comments
 (0)