| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import marked from 'marked'
- export function markdownToHTML(markdownData = '') {
- return marked(markdownData, {
- pedantic: false,
- breaks: false,
- gfm: true
- })
- }
- /**
- * tiptap 使用的工具函数
- */
- export function jsonToMarkdown(jsonData) {
- switch (jsonData?.type) {
- case 'doc':
- return reduceContent(jsonData)
- case 'paragraph':
- return reduceContent(jsonData) + '\n\n'
- case 'text':
- return decorateText(jsonData)
- case 'image':
- return ``
- default:
- return ''
- }
- }
- function reduceContent(jsonData) {
- return (
- jsonData?.content?.reduce(
- (acc, data) => `${acc}${jsonToMarkdown(data)}`,
- ''
- ) ?? ''
- )
- }
- function secureContent(text = '') {
- return text.replace(/</g, '\\<').replace(/>/g, '\\>')
- }
- function decorateText({ text, marks = [] }) {
- return marks.reduce((acc, current) => {
- switch (current?.type) {
- case 'bold':
- return `**${acc}**`
- case 'code':
- return `\`${acc}\``
- case 'italic':
- return `_${acc}_`
- case 'strike':
- return `~${acc}~`
- default:
- return current
- }
- }, secureContent(text))
- }
|