Skip to content

Commit 7e7ed98

Browse files
Merge pull request #57 from testomatio/feature/51
feat: simplify paragraph parsing in customMarkdownConverter and enhance markdownToBlocks tests for multiple paragraphs
2 parents 7c0ab49 + ce21d93 commit 7e7ed98

2 files changed

Lines changed: 35 additions & 52 deletions

File tree

src/editor/customMarkdownConverter.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,63 +1309,15 @@ function parseQuote(lines: string[], index: number): { block: CustomPartialBlock
13091309
}
13101310

13111311
function parseParagraph(lines: string[], index: number): { block: CustomPartialBlock; nextIndex: number } {
1312-
const buffer: string[] = [];
1313-
let next = index;
1314-
1315-
const isTermination = (line: string) => {
1316-
const trimmed = line.trim();
1317-
if (!trimmed) {
1318-
return true;
1319-
}
1320-
if (trimmed.startsWith(":::test-case")) {
1321-
return true;
1322-
}
1323-
if (trimmed.startsWith("* ")) {
1324-
return true;
1325-
}
1326-
if (trimmed.startsWith("#")) {
1327-
return true;
1328-
}
1329-
if (trimmed.startsWith(">")) {
1330-
return true;
1331-
}
1332-
if (trimmed.startsWith("```") ) {
1333-
return true;
1334-
}
1335-
if (detectListType(trimmed)) {
1336-
return true;
1337-
}
1338-
return false;
1339-
};
1340-
1341-
while (next < lines.length) {
1342-
const line = lines[next];
1343-
if (
1344-
isTableRowLine(line) &&
1345-
next + 1 < lines.length &&
1346-
isSeparatorRow(lines[next + 1])
1347-
) {
1348-
break;
1349-
}
1350-
if (isTermination(line) && buffer.length > 0) {
1351-
break;
1352-
}
1353-
if (!line.trim()) {
1354-
next += 1;
1355-
break;
1356-
}
1357-
buffer.push(line.trim());
1358-
next += 1;
1359-
}
1360-
1312+
const line = lines[index];
13611313
return {
13621314
block: {
13631315
type: "paragraph",
13641316
props: cloneBaseProps(),
1365-
content: createTextContent(unescapeMarkdown(buffer.join(" "))),
1317+
content: createTextContent(unescapeMarkdown(line.trim())),
13661318
children: [],
13671319
},
1368-
nextIndex: next,
1320+
nextIndex: index + 1,
13691321
};
13701322
}
13711323

src/editor/markdownToBlocks.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { markdownToBlocks } from "./customMarkdownConverter";
2+
import { markdownToBlocks, blocksToMarkdown } from "./customMarkdownConverter";
33

44
const baseProps = {
55
textAlignment: "left" as const,
@@ -131,6 +131,37 @@ describe("markdownToBlocks", () => {
131131
});
132132
});
133133

134+
it("treats each consecutive text line as a separate paragraph block", () => {
135+
const markdown = "One line\nAnother line\nThird line";
136+
const blocks = markdownToBlocks(markdown);
137+
138+
expect(blocks).toHaveLength(3);
139+
expect(blocks[0]).toEqual({
140+
type: "paragraph",
141+
props: baseProps,
142+
content: [{ type: "text", text: "One line", styles: {} }],
143+
children: [],
144+
});
145+
expect(blocks[1]).toEqual({
146+
type: "paragraph",
147+
props: baseProps,
148+
content: [{ type: "text", text: "Another line", styles: {} }],
149+
children: [],
150+
});
151+
expect(blocks[2]).toEqual({
152+
type: "paragraph",
153+
props: baseProps,
154+
content: [{ type: "text", text: "Third line", styles: {} }],
155+
children: [],
156+
});
157+
});
158+
159+
it("round-trips consecutive text lines through blocksToMarkdown", () => {
160+
const markdown = "One line\nAnother line\nThird line";
161+
const blocks = markdownToBlocks(markdown);
162+
expect(blocksToMarkdown(blocks as any)).toBe("One line\nAnother line\nThird line");
163+
});
164+
134165
it("parses combined bold+italic using nested delimiters", () => {
135166
const blocks = markdownToBlocks(
136167
"The _**Username**_ and **_Password_** fields and ***both*** and ___both___.",

0 commit comments

Comments
 (0)