Skip to content

Commit

Permalink
fix: ensure Markdown formatted to HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Dec 24, 2024
1 parent a72bd26 commit a3d027e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/lib/markdown.ts
Original file line number Diff line number Diff line change
@@ -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.replace(/^\s+|\s+$/g, '');
}

const htmlWithoutPreTags = converter
.makeHtml(processedHtml)
.replace(/<pre><code>|<\/code><\/pre>/g, '');
return htmlWithoutPreTags;
};
14 changes: 9 additions & 5 deletions src/lib/parser/DeckParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<ul>\n<li>' + 'What is the capital of Kenya?' + '</li>\n</ul>');
expect(deck.cards[0].back).toBe('<pre><code>Nairobi</code></pre>');
expect(deck.cards[0].back).toBe('<p>Nairobi</p>');
expect(deck.cards[1].name).toBe('<ul>\n<li>' + 'What is the capital of Norway' + '</li>\n</ul>');
expect(deck.cards[1].back).toBe('<pre><code>Oslo</code></pre>');
expect(deck.cards[1].back).toBe('<p>Oslo</p>');
expect(deck.cards[2].name).toBe('<ul>\n<li>' + 'What is the capital of Sweden'+'</li>\n</ul>');

console.log('Deck card 2 back:', deck.cards[2].back);

expect(deck.cards[2].back).toBe('<p>Stockholm</p>');
expect(deck.cards.length).toBe(3);
expect(deck.cards[3].name).toBe('<ul>\n<li>' + 'What is the capital of Finland' + '</li>\n</ul>');
expect(deck.cards[3].back).toBe('<p>Helsinki</p>');
expect(deck.cards.length).toBe(4);
})
22 changes: 15 additions & 7 deletions src/lib/parser/handleNestedBulletPointsInMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ export const handleNestedBulletPointsInMarkdown = (
let isCreating = false;
let currentFront = '';
let currentBack = '';
const trimWhitespace = true;

for (const line of lines) {
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
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,
});
Expand All @@ -85,8 +86,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;
Expand All @@ -96,13 +100,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,
Expand All @@ -127,7 +132,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);
}
Expand Down
18 changes: 11 additions & 7 deletions src/test/fixtures/simple-deck.md
Original file line number Diff line number Diff line change
@@ -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**

0 comments on commit a3d027e

Please sign in to comment.