update note content
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user