refactor: Migrate the project functionality and optimize the code structure

This commit is contained in:
hayden
2024-10-12 17:31:11 +08:00
committed by hayden
parent d96aff80c2
commit c1747a79f3
71 changed files with 6741 additions and 1320 deletions

49
src/hooks/markdown.ts Normal file
View File

@@ -0,0 +1,49 @@
import MarkdownIt from 'markdown-it'
import metadata_block from 'markdown-it-metadata-block'
import TurndownService from 'turndown'
import yaml from 'yaml'
interface MarkdownOptions {
metadata?: Record<string, any>
}
export const useMarkdown = (opts?: MarkdownOptions) => {
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
})
md.use(metadata_block, {
parseMetadata: yaml.parse,
meta: opts?.metadata ?? {},
})
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const aIndex = tokens[idx].attrIndex('target')
if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank'])
} else {
tokens[idx].attrs![aIndex][1] = '_blank'
}
return self.renderToken(tokens, idx, options)
}
const turndown = new TurndownService({
headingStyle: 'atx',
bulletListMarker: '-',
})
turndown.addRule('paragraph', {
filter: 'p',
replacement: function (content) {
return `\n\n${content}`
},
})
return { render: md.render.bind(md), parse: turndown.turndown.bind(turndown) }
}
export type MarkdownTool = ReturnType<typeof useMarkdown>