-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
41 lines (37 loc) · 1.14 KB
/
index.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
39
40
41
import type { Plugin } from "vite"
import { createFilter } from "@rollup/pluginutils"
import { createMarkdownProcessor } from "./markdown"
import type { Options } from "./options"
import { resolveOptions } from "./options"
export type { Options }
/**
* Creates vite-plugin-svelte-md
*/
export default function (options: Options = {}): Plugin {
const resolvedOptions = resolveOptions(options)
const mdToSvelte = createMarkdownProcessor(resolvedOptions)
const filter = createFilter(
resolvedOptions.include || /\.md$/,
resolvedOptions.exclude,
)
return {
name: "vite-plugin-svelte-md",
enforce: "pre",
transform(raw, id) {
if (!filter(id)) return undefined
try {
return mdToSvelte(id, raw)
} catch (e: any) {
this.error(e)
}
return undefined
},
handleHotUpdate(ctx) {
if (!filter(ctx.file)) return
const defaultRead = ctx.read
ctx.read = async function () {
return mdToSvelte(ctx.file, await defaultRead())
}
},
}
}