-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
markdown.ts
38 lines (34 loc) · 1.11 KB
/
markdown.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { parse } from '../markdown-parser'
import type { MarkdownOptions, MarkdownPlugin } from '../types'
import { MarkdownParsedContent } from '../types'
import { defineTransformer } from './utils'
export default defineTransformer({
name: 'markdown',
extensions: ['.md'],
parse: async (_id, content, options = {}) => {
const config = { ...options } as MarkdownOptions
config.rehypePlugins = await importPlugins(config.rehypePlugins)
config.remarkPlugins = await importPlugins(config.remarkPlugins)
const parsed = await parse(content, config)
return <MarkdownParsedContent> {
...parsed.meta,
body: parsed.body,
_type: 'markdown',
_id
}
}
})
async function importPlugins (plugins: Record<string, false | MarkdownPlugin> = {}) {
const resolvedPlugins = {}
for (const [name, plugin] of Object.entries(plugins)) {
if (plugin) {
resolvedPlugins[name] = {
instance: plugin.instance || await import(/* @vite-ignore */ name).then(m => m.default || m),
...plugin
}
} else {
resolvedPlugins[name] = false
}
}
return resolvedPlugins
}