diff --git a/package-lock.json b/package-lock.json index f675c449..434f7fa6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11630,6 +11630,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/showdown/-/showdown-2.1.0.tgz", "integrity": "sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==", + "license": "MIT", "dependencies": { "commander": "^9.0.0" }, diff --git a/src/lib/markdown.ts b/src/lib/markdown.ts index e8179a3d..efa905dc 100644 --- a/src/lib/markdown.ts +++ b/src/lib/markdown.ts @@ -1,10 +1,24 @@ import showdown from 'showdown'; -export const markdownToHTML = (html: string) => { +export const markdownToHTML = ( + html: string, + trimWhitespace: boolean = false +) => { const converter = new showdown.Converter({ noHeaderId: true, disableForced4SpacesIndentedSublists: true, + simpleLineBreaks: true, }); converter.setFlavor('github'); - return converter.makeHtml(html); + + let processedHtml = html; + + if (trimWhitespace) { + processedHtml = html.trim(); + } + + const htmlWithoutPreTags = converter + .makeHtml(processedHtml) + .replace(/
|<\/code><\/pre>/g, '');
+ return htmlWithoutPreTags;
};
diff --git a/src/lib/parser/DeckParser.test.ts b/src/lib/parser/DeckParser.test.ts
index 0e992c93..103b2759 100644
--- a/src/lib/parser/DeckParser.test.ts
+++ b/src/lib/parser/DeckParser.test.ts
@@ -95,15 +95,19 @@ test('Markdown nested bullet points', async () => {
"reversed": "false",
"basic-reversed": "false",
}));
- expect(deck.name).toBe('Simple deck');
+
+ expect(deck.name).toBe('Simple Deck');
+
expect(deck.cards[0].name).toBe('\n- ' + 'What is the capital of Kenya?' + '
\n
');
- expect(deck.cards[0].back).toBe('Nairobi
');
+ expect(deck.cards[0].back).toBe('Nairobi
');
expect(deck.cards[1].name).toBe('\n- ' + 'What is the capital of Norway' + '
\n
');
- expect(deck.cards[1].back).toBe('Oslo
');
+ expect(deck.cards[1].back).toBe('Oslo
');
expect(deck.cards[2].name).toBe('\n- ' + 'What is the capital of Sweden'+'
\n
');
console.log('Deck card 2 back:', deck.cards[2].back);
-
+
expect(deck.cards[2].back).toBe('Stockholm
');
- expect(deck.cards.length).toBe(3);
+ expect(deck.cards[3].name).toBe('\n- ' + 'What is the capital of Finland' + '
\n
');
+ expect(deck.cards[3].back).toBe('Helsinki
');
+ expect(deck.cards.length).toBe(4);
})
diff --git a/src/lib/parser/handleNestedBulletPointsInMarkdown.ts b/src/lib/parser/handleNestedBulletPointsInMarkdown.ts
index cd1b8bf7..8c47b73e 100644
--- a/src/lib/parser/handleNestedBulletPointsInMarkdown.ts
+++ b/src/lib/parser/handleNestedBulletPointsInMarkdown.ts
@@ -55,14 +55,14 @@ export const handleNestedBulletPointsInMarkdown = (
let isCreating = false;
let currentFront = '';
let currentBack = '';
+ const trimWhitespace = true;
for (const line of lines) {
if (line.trim().length === 0) {
continue;
}
- const isEnd = lines.length - 1 == lines.indexOf(line);
- if (isEnd || (BULLET_POINT_REGEX.exec(line) && isCreating)) {
+ if (BULLET_POINT_REGEX.exec(line) && isCreating) {
const dom = cheerio.load(currentBack, {
xmlMode: true,
});
@@ -85,8 +85,11 @@ export const handleNestedBulletPointsInMarkdown = (
}
});
- currentBack = dom.html();
- const note = new Note(currentFront, markdownToHTML(currentBack));
+ currentBack = dom.html() || '';
+ const note = new Note(
+ currentFront,
+ markdownToHTML(currentBack, trimWhitespace)
+ );
note.media = media;
deck.cards.push(note);
isCreating = false;
@@ -96,13 +99,14 @@ export const handleNestedBulletPointsInMarkdown = (
if (BULLET_POINT_REGEX.exec(line) && !isCreating) {
isCreating = true;
- currentFront = markdownToHTML(line);
+ currentFront = markdownToHTML(line, trimWhitespace);
currentBack = '';
} else if (isCreating) {
currentBack += line + '\n';
}
}
+ // Ensure the last card is processed
if (currentBack !== '' || currentFront !== '') {
const dom = cheerio.load(currentBack, {
xmlMode: true,
@@ -127,7 +131,10 @@ export const handleNestedBulletPointsInMarkdown = (
});
currentBack = dom.html() || '';
- const note = new Note(currentFront, markdownToHTML(currentBack));
+ const note = new Note(
+ currentFront,
+ markdownToHTML(currentBack, trimWhitespace)
+ );
note.media = media;
deck.cards.push(note);
}
diff --git a/src/test/fixtures/simple-deck.md b/src/test/fixtures/simple-deck.md
index d8073f08..20725c33 100644
--- a/src/test/fixtures/simple-deck.md
+++ b/src/test/fixtures/simple-deck.md
@@ -1,13 +1,17 @@
-# Simple deck
+# Simple Deck
- What is the capital of Kenya?
-
+
Nairobi
-
+
- What is the capital of Norway
-
+
Oslo
-
+
- What is the capital of Sweden
-
- Stockholm
+
+ Stockholm
+
+- What is the capital of Finland
+
+ **Helsinki**
\ No newline at end of file