Skip to content

Commit b4ff6a7

Browse files
committed
refactor: remove temp page files and load page component via bundler
1 parent 8a90ce9 commit b4ff6a7

31 files changed

+319
-345
lines changed

e2e/docs/.vuepress/components/ComponentForMarkdownImport.vue

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div class="component-for-markdown-import-bar">
3+
<p>component for markdown import bar</p>
4+
</div>
5+
</template>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div class="component-for-markdown-import-foo">
3+
<p>component for markdown import foo</p>
4+
</div>
5+
</template>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="dangling-markdown-file">
2+
3+
dangling markdown file
4+
5+
</div>

e2e/docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
foo
22

33
## Home H2
4+
5+
demo

e2e/docs/markdown/vue-components.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<ComponentForMarkdownGlobal />
22

3-
<ComponentForMarkdownImport />
3+
<ComponentForMarkdownImportFoo />
4+
5+
<ComponentForMarkdownImportBar />
46

57
<script setup>
6-
// TODO: relative path import?
7-
import ComponentForMarkdownImport from '@source/.vuepress/components/ComponentForMarkdownImport.vue';
8+
// import via alias
9+
import ComponentForMarkdownImportFoo from '@source/.vuepress/components/ComponentForMarkdownImportFoo.vue';
10+
11+
// import via relative path
12+
import ComponentForMarkdownImportBar from '../.vuepress/components/ComponentForMarkdownImportBar.vue';
813
</script>

e2e/tests/markdown/vue-components.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ test('should render vue components correctly', async ({ page }) => {
66
await expect(page.locator('.component-for-markdown-global p')).toHaveText(
77
'component for markdown global',
88
)
9-
await expect(page.locator('.component-for-markdown-import p')).toHaveText(
10-
'component for markdown import',
9+
await expect(page.locator('.component-for-markdown-import-foo p')).toHaveText(
10+
'component for markdown import foo',
11+
)
12+
await expect(page.locator('.component-for-markdown-import-bar p')).toHaveText(
13+
'component for markdown import bar',
1114
)
1215
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './vuepressBuildPlugin.js'
22
export * from './vuepressConfigPlugin.js'
33
export * from './vuepressDevPlugin.js'
4+
export * from './vuepressMarkdownPlugin.js'
45
export * from './vuepressUserConfigPlugin.js'
56
export * from './vuepressVuePlugin.js'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { App } from '@vuepress/core'
2+
import { parsePageContent, renderPageSfcBlocksToVue } from '@vuepress/core'
3+
import { path } from '@vuepress/utils'
4+
import type { Plugin } from 'vite'
5+
6+
/**
7+
* Handle markdown transformation
8+
*/
9+
export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
10+
name: 'vuepress:markdown',
11+
12+
enforce: 'pre',
13+
14+
transform(code, id) {
15+
if (!id.endsWith('.md')) return
16+
17+
// get the matched page by file path (id)
18+
const page = app.pagesMap[id]
19+
20+
// if the page content is not changed, render it to vue component directly
21+
if (page?.content === code) {
22+
return renderPageSfcBlocksToVue(page.sfcBlocks)
23+
}
24+
25+
// parse the markdown content to sfc blocks and render it to vue component
26+
const { sfcBlocks } = parsePageContent({
27+
app,
28+
content: code,
29+
filePath: id,
30+
filePathRelative: path.relative(app.dir.source(), id),
31+
options: {},
32+
})
33+
return renderPageSfcBlocksToVue(sfcBlocks)
34+
},
35+
36+
async handleHotUpdate(ctx) {
37+
if (!ctx.file.endsWith('.md')) return
38+
39+
// read the source code
40+
const code = await ctx.read()
41+
42+
// parse the content to sfc blocks
43+
const { sfcBlocks } = parsePageContent({
44+
app,
45+
content: code,
46+
filePath: ctx.file,
47+
filePathRelative: path.relative(app.dir.source(), ctx.file),
48+
options: {},
49+
})
50+
51+
ctx.read = () => renderPageSfcBlocksToVue(sfcBlocks)
52+
},
53+
})

packages/bundler-vite/src/plugins/vuepressVuePlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export const vuepressVuePlugin = ({
1111
options: ViteBundlerOptions
1212
}): Plugin =>
1313
vuePlugin({
14+
include: [/\.vue$/, /\.md$/],
1415
...options.vuePluginOptions,
1516
})

0 commit comments

Comments
 (0)