-
Notifications
You must be signed in to change notification settings - Fork 922
/
Copy pathvuepressMarkdownPlugin.ts
46 lines (36 loc) · 1.11 KB
/
vuepressMarkdownPlugin.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
42
43
44
45
46
import type { App } from '@vuepress/core'
import { createPage, renderPageToVue } from '@vuepress/core'
import type { Plugin } from 'vite'
/**
* Handle markdown transformation
*/
export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
name: 'vuepress:markdown',
enforce: 'pre',
async transform(code, id) {
if (!id.endsWith('.md')) return
// get the matched page by file path (id)
const page = app.pagesMap[id]
// if the page content is not changed, render it to vue component directly
if (page?.content === code) {
return renderPageToVue(app, page)
}
// create a new page with the new content
const newPage = await createPage(app, {
content: code,
filePath: id,
})
return renderPageToVue(app, newPage)
},
async handleHotUpdate(ctx) {
if (!ctx.file.endsWith('.md')) return
// read the source code
const code = await ctx.read()
// create a new page with the new content
const newPage = await createPage(app, {
content: code,
filePath: ctx.file,
})
ctx.read = () => renderPageToVue(app, newPage)
},
})