update note content

This commit is contained in:
2023-05-13 08:09:52 +02:00
parent 69cec28fb6
commit 8eb7e57203
6 changed files with 85 additions and 8 deletions

View File

@@ -1,7 +1,8 @@
import { marked } from 'marked'
import DOMPurify from 'dompurify'
import Turndown from 'turndown'
export function mdToHtml(mdText: string) {
export function mdToHtml(mdText: string): string {
const renderer = new marked.Renderer()
const html = DOMPurify.sanitize(marked.parse(mdText, { renderer }))
@@ -14,3 +15,28 @@ export function mdToHtml(mdText: string) {
})
return doc.body.innerHTML
}
export function htmlToMd(htmlText: string): string {
const turndown = new Turndown({ headingStyle: 'atx' })
const escapes = [
[/\\/g, '\\\\'],
[/\*/g, '\\*'],
[/^-/g, '\\-'],
[/^\+ /g, '\\+ '],
[/^(=+)/g, '\\$1'],
[/^(#{1,6}) /g, '\\$1 '],
[/`/g, '\\`'],
[/^~~~/g, '\\~~~'],
// [/\[/g, '\\['],
// [/\]/g, '\\]'],
[/^>/g, '\\>'],
[/_/g, '\\_'],
[/^(\d+)\. /g, '$1\\. '],
]
turndown.escape = (string) =>
escapes.reduce((accumulator, escape) => {
return accumulator.replace(escape[0], escape[1] as string)
}, string)
const md = turndown.turndown(htmlText)
return md
}