From 757697be45035db9a9624fba0c014af7c940e520 Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Tue, 24 Dec 2024 18:51:07 +0100 Subject: [PATCH] fix: ensure Markdown formatted to HTML --- package-lock.json | 1 + src/lib/markdown.ts | 18 ++++++++++++++++-- src/lib/parser/DeckParser.test.ts | 14 +++++++++----- .../handleNestedBulletPointsInMarkdown.ts | 19 +++++++++++++------ src/test/fixtures/simple-deck.md | 18 +++++++++++------- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index f675c449e..434f7fa60 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 e8179a3d9..efa905dc3 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 0e992c935..103b27591 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 cd1b8bf7d..8c47b73e1 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 d8073f088..20725c337 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