-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove temp page files and load page component via bundler
- Loading branch information
Showing
31 changed files
with
319 additions
and
345 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
e2e/docs/.vuepress/components/ComponentForMarkdownImportBar.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<div class="component-for-markdown-import-bar"> | ||
<p>component for markdown import bar</p> | ||
</div> | ||
</template> |
5 changes: 5 additions & 0 deletions
5
e2e/docs/.vuepress/components/ComponentForMarkdownImportFoo.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<div class="component-for-markdown-import-foo"> | ||
<p>component for markdown import foo</p> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="dangling-markdown-file"> | ||
|
||
dangling markdown file | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
foo | ||
|
||
## Home H2 | ||
|
||
demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
<ComponentForMarkdownGlobal /> | ||
|
||
<ComponentForMarkdownImport /> | ||
<ComponentForMarkdownImportFoo /> | ||
|
||
<ComponentForMarkdownImportBar /> | ||
|
||
<script setup> | ||
// TODO: relative path import? | ||
import ComponentForMarkdownImport from '@source/.vuepress/components/ComponentForMarkdownImport.vue'; | ||
// import via alias | ||
import ComponentForMarkdownImportFoo from '@source/.vuepress/components/ComponentForMarkdownImportFoo.vue'; | ||
|
||
// import via relative path | ||
import ComponentForMarkdownImportBar from '../.vuepress/components/ComponentForMarkdownImportBar.vue'; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './vuepressBuildPlugin.js' | ||
export * from './vuepressConfigPlugin.js' | ||
export * from './vuepressDevPlugin.js' | ||
export * from './vuepressMarkdownPlugin.js' | ||
export * from './vuepressUserConfigPlugin.js' | ||
export * from './vuepressVuePlugin.js' |
53 changes: 53 additions & 0 deletions
53
packages/bundler-vite/src/plugins/vuepressMarkdownPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { App } from '@vuepress/core' | ||
import { parsePageContent, renderPageSfcBlocksToVue } from '@vuepress/core' | ||
import { path } from '@vuepress/utils' | ||
import type { Plugin } from 'vite' | ||
|
||
/** | ||
* Handle markdown transformation | ||
*/ | ||
export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({ | ||
name: 'vuepress:markdown', | ||
|
||
enforce: 'pre', | ||
|
||
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 renderPageSfcBlocksToVue(page.sfcBlocks) | ||
} | ||
|
||
// parse the markdown content to sfc blocks and render it to vue component | ||
const { sfcBlocks } = parsePageContent({ | ||
app, | ||
content: code, | ||
filePath: id, | ||
filePathRelative: path.relative(app.dir.source(), id), | ||
options: {}, | ||
}) | ||
return renderPageSfcBlocksToVue(sfcBlocks) | ||
}, | ||
|
||
async handleHotUpdate(ctx) { | ||
if (!ctx.file.endsWith('.md')) return | ||
|
||
// read the source code | ||
const code = await ctx.read() | ||
|
||
// parse the content to sfc blocks | ||
const { sfcBlocks } = parsePageContent({ | ||
app, | ||
content: code, | ||
filePath: ctx.file, | ||
filePathRelative: path.relative(app.dir.source(), ctx.file), | ||
options: {}, | ||
}) | ||
|
||
ctx.read = () => renderPageSfcBlocksToVue(sfcBlocks) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/bundler-webpack/src/loaders/vuepressMarkdownLoader.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const loader = require('./vuepressMarkdownLoader.js') | ||
|
||
module.exports = loader.vuepressMarkdownLoader |
37 changes: 37 additions & 0 deletions
37
packages/bundler-webpack/src/loaders/vuepressMarkdownLoader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { App } from '@vuepress/core' | ||
import type { LoaderDefinitionFunction } from 'webpack' | ||
|
||
export interface VuepressMarkdownLoaderOptions { | ||
app: App | ||
} | ||
|
||
/** | ||
* A webpack loader to transform markdown content to vue component | ||
*/ | ||
export const vuepressMarkdownLoader: LoaderDefinitionFunction<VuepressMarkdownLoaderOptions> = | ||
async function vuepressMarkdownLoader(source) { | ||
// import esm dependencies | ||
const [{ parsePageContent, renderPageSfcBlocksToVue }, { path }] = | ||
await Promise.all([import('@vuepress/core'), import('@vuepress/utils')]) | ||
|
||
// get app instance from loader options | ||
const { app } = this.getOptions() | ||
|
||
// get the matched page by file path | ||
const page = app.pagesMap[this.resourcePath] | ||
|
||
// if the page content is not changed, render it to vue component directly | ||
if (page?.content === source) { | ||
return renderPageSfcBlocksToVue(page.sfcBlocks) | ||
} | ||
|
||
// parse the markdown content to sfc blocks and render it to vue component | ||
const { sfcBlocks } = parsePageContent({ | ||
app, | ||
content: source, | ||
filePath: this.resourcePath, | ||
filePathRelative: path.relative(app.dir.source(), this.resourcePath), | ||
options: {}, | ||
}) | ||
return renderPageSfcBlocksToVue(sfcBlocks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.