Skip to content

Commit a2cc749

Browse files
authored
feat: convert Markdown files to HTML (#1417)
1 parent fa8920b commit a2cc749

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

package-lock.json

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"multer": "^1.4.4-lts.1",
6060
"pg": "^8.11.3",
6161
"react": "^18.2.0",
62-
"react-dom": "^18.2.0"
62+
"react-dom": "^18.2.0",
63+
"showdown": "^2.1.0"
6364
},
6465
"devDependencies": {
6566
"@types/base-64": "^1.0.0",
@@ -79,6 +80,7 @@
7980
"@types/node-fetch": "^2.6.4",
8081
"@types/react": "^18.2.43",
8182
"@types/react-dom": "18.2.18",
83+
"@types/showdown": "^2.0.6",
8284
"@typescript-eslint/eslint-plugin": "^6.10.0",
8385
"@typescript-eslint/parser": "^6.15.0",
8486
"eslint": "^8.53.0",

src/lib/markdown.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import showdown from 'showdown';
2+
3+
export const markdownToHTML = (html: string) => {
4+
const converter = new showdown.Converter({
5+
noHeaderId: true,
6+
});
7+
8+
return converter.makeHtml(html);
9+
};

src/lib/parser/DeckParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import getYouTubeID from './helpers/getYouTubeID';
2222
import { isFileNameEqual } from '../storage/types';
2323
import { isImageFileEmbedable } from '../storage/checks';
2424
import getDeckFilename from '../anki/getDeckFilename';
25+
import { getHTMLContents } from './getHTMLContents';
2526

2627
export class DeckParser {
2728
globalTags: cheerio.Cheerio | null;
@@ -45,8 +46,8 @@ export class DeckParser {
4546
this.globalTags = null;
4647

4748
const firstFile = this.files.find((file) => isFileNameEqual(file, name));
49+
const contents = getHTMLContents(firstFile);
4850

49-
const contents = firstFile?.contents;
5051
if (contents) {
5152
this.payload = this.handleHTML(
5253
name,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe } from 'node:test';
2+
import { getHTMLContents } from './getHTMLContents';
3+
4+
describe("getHTMLContents", () => {
5+
test("returns html contents", () => {
6+
expect(getHTMLContents({contents: "<h1>html</h1>", name: "index.html"})).toBe("<h1>html</h1>")
7+
})
8+
test("returns html for markdown", () => {
9+
expect(getHTMLContents({contents: "# md", name: "README.md"})).toBe("<h1>md</h1>")
10+
})
11+
})

src/lib/parser/getHTMLContents.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { isHTMLFile, isMarkdownFile } from '../storage/checks';
2+
import { markdownToHTML } from '../markdown';
3+
import { File } from '../anki/zip';
4+
5+
export function getHTMLContents(file: File | undefined) {
6+
const contents = file?.contents;
7+
if (!file || !contents) {
8+
return undefined;
9+
}
10+
11+
if (isHTMLFile(file.name)) {
12+
return file.contents;
13+
}
14+
15+
if (isMarkdownFile(file.name)) {
16+
return markdownToHTML(contents.toString());
17+
}
18+
}

0 commit comments

Comments
 (0)