1
1
import type { App } from '@vuepress/core'
2
- import { parsePageContent , renderPageSfcBlocksToVue } from '@vuepress/core'
3
- import { path } from '@vuepress/utils'
2
+ import { createPage , renderPageToVue } from '@vuepress/core'
4
3
import type { Plugin } from 'vite'
5
4
6
5
/**
@@ -11,26 +10,23 @@ export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
11
10
12
11
enforce : 'pre' ,
13
12
14
- transform ( code , id ) {
13
+ async transform ( code , id ) {
15
14
if ( ! id . endsWith ( '.md' ) ) return
16
15
17
16
// get the matched page by file path (id)
18
17
const page = app . pagesMap [ id ]
19
18
20
19
// if the page content is not changed, render it to vue component directly
21
20
if ( page ?. content === code ) {
22
- return renderPageSfcBlocksToVue ( page . sfcBlocks )
21
+ return renderPageToVue ( page )
23
22
}
24
23
25
- // parse the markdown content to sfc blocks and render it to vue component
26
- const { sfcBlocks } = parsePageContent ( {
27
- app,
24
+ // create a new page with the new content
25
+ const newPage = await createPage ( app , {
28
26
content : code ,
29
27
filePath : id ,
30
- filePathRelative : path . relative ( app . dir . source ( ) , id ) ,
31
- options : { } ,
32
28
} )
33
- return renderPageSfcBlocksToVue ( sfcBlocks )
29
+ return renderPageToVue ( newPage )
34
30
} ,
35
31
36
32
async handleHotUpdate ( ctx ) {
@@ -39,15 +35,12 @@ export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
39
35
// read the source code
40
36
const code = await ctx . read ( )
41
37
42
- // parse the content to sfc blocks
43
- const { sfcBlocks } = parsePageContent ( {
44
- app,
38
+ // create a new page with the new content
39
+ const newPage = await createPage ( app , {
45
40
content : code ,
46
41
filePath : ctx . file ,
47
- filePathRelative : path . relative ( app . dir . source ( ) , ctx . file ) ,
48
- options : { } ,
49
42
} )
50
43
51
- ctx . read = ( ) => renderPageSfcBlocksToVue ( sfcBlocks )
44
+ ctx . read = ( ) => renderPageToVue ( newPage )
52
45
} ,
53
46
} )
0 commit comments