Node API
Node API can be imported from vuepress/core.
App
The app instance is available in all hooks of Plugin API.
The BuildApp and DevApp share almost the same properties and methods, except build and dev method.
createBuildApp
- Signature:
const createBuildApp: (config: AppConfig) => BuildApp- Parameters:
| Parameter | Type | Description |
|---|---|---|
| config | AppConfig | Config to create a VuePress app. |
Details:
Create a build mode app instance, which is used for building static files.
Example:
const build = async () => {
const app = createBuildApp({
// ...options
})
// initialize and prepare
await app.init()
await app.prepare()
// build
await app.build()
// process onGenerated hook
await app.pluginApi.hooks.onGenerated.process(app)
}- Also see:
createDevApp
- Signature:
const createDevApp: (config: AppConfig) => DevApp- Parameters:
| Parameter | Type | Description |
|---|---|---|
| config | AppConfig | Config to create a VuePress app. |
Details:
Create a dev mode app instance, which is used for starting a dev server.
Example:
const dev = async () => {
const app = createDevApp({
// ...options
})
// initialize and prepare
await app.init()
await app.prepare()
// start dev server
const closeDevServer = await app.dev()
// set up file watchers
const watchers = []
// restart dev server
const restart = async () => {
await Promise.all([
// close all watchers
...watchers.map((item) => item.close()),
// close current dev server
closeDevServer(),
])
await dev()
}
// process onWatched hook
await app.pluginApi.hooks.onWatched.process(app, watchers, restart)
}- Also see:
App Properties
options
Type:
AppOptionsDetails:
Options of VuePress app.
The options come from the
configargument in createBuildApp / createDevApp, while all optional fields will be filled with a default value.
siteData
Type:
SiteDataDetails:
Site data that set by user, including all the site config, which will be used in client side.
version
Type:
stringDetails:
Version of VuePress app, i.e. version of
@vuepress/corepackage.
env.isBuild
Type:
booleanDetails:
Environment flag used to identify whether the app is running in build mode, i.e. whether a BuildApp instance.
env.isDev
Type:
booleanDetails:
Environment flag used to identify whether the app is running in dev mode, i.e. whether a DevApp instance.
env.isDebug
Type:
booleanDetails:
Environment flag used to identify whether the debug mode is enabled.
markdown
Type:
MarkdownItDetails:
The markdown-it instance used for parsing markdown content.
It is only available in and after onInitialized hook.
pages
Type:
Page[]Details:
The Page object array.
It is only available in and after onInitialized hook.
App Methods
dir
Utils:
dir.cache(): resolve to cache directorydir.temp(): resolve to temp directorydir.source(): resolve to source directorydir.dest(): resolve to dest directorydir.client(): resolve to@vuepress/clientdirectorydir.public(): resolve to public directory
Signature:
type AppDirFunction = (...args: string[]) => stringDetails:
Utils to resolve the absolute file path in corresponding directory.
If you don't provide any arguments, it will return the absolute path of the directory.
Example:
// resolve the absolute file path of the `${sourceDir}/README.md`
const homeSourceFile = app.dir.source('README.md')writeTemp
- Signature:
declare const writeTemp = (file: string, content: string) => Promise<string>- Parameters:
| Parameter | Type | Description |
|---|---|---|
| file | string | Filepath of the temp file that going to be written. Relative to temp directory. |
| content | string | Content of the temp file that going to be written. |
Details:
A method to write temp file.
The written file could be imported via
@tempalias in client files.Example:
export default {
// write temp file in onPrepared hook
async onPrepared() {
await app.writeTemp('foo.js', "export const foo = 'bar'")
},
}// import temp file in client code
import { foo } from '@temp/foo'init
- Signature:
declare const init = () => Promise<void>Details:
Initialize VuePress app.
Also see:
prepare
- Signature:
declare const prepare = () => Promise<void>Details:
Prepare client temp files.
Also see:
build
- Signature:
declare const build = () => Promise<void>Details:
Generate static site files.
This method is only available in
BuildApp.Also see:
dev
- Signature:
declare const dev = () => Promise<() => Promise<void>>Details:
Start dev server.
This method is only available in
DevApp.Also see:
Page
createPage
- Signature:
const createPage: (app: App, options: PageOptions) => Promise<Page>- Parameters:
| Parameter | Type | Description |
|---|---|---|
| app | App | The VuePress app instance. |
| options | PageOptions | Options to create VuePress page. |
Details:
Create a VuePress page object.
Example:
import { createPage } from 'vuepress/core'
export default {
// create an extra page in onInitialized hook
async onInitialized(app) {
app.pages.push(
await createPage(app, {
path: '/foo.html',
frontmatter: {
layout: 'Layout',
},
content: `\
# Foo Page
Hello, world.
`,
}),
)
},
}Page Properties
path
Type:
stringDetails:
Route path of the page.
Also see:
title
Type:
stringDetails:
Title of the page.
Also see:
lang
Type:
stringDetails:
Language of the page.
Example:
'en-US''zh-CN'
Also see:
frontmatter
Type:
PageFrontmatterDetails:
Frontmatter of the page.
Also see:
headers
- Type:
PageHeader[]
interface PageHeader {
level: number
title: string
slug: string
children: PageHeader[]
}Details:
Headers of the page.
Also see:
data
- Type:
PageData
interface PageData {
path: string
title: string
lang: string
frontmatter: PageFrontmatter
}Details:
Data of the page.
Page data would be available in client side.
Also see:
content
Type:
stringDetails:
Raw content of the page.
contentRendered
Type:
stringDetails:
Rendered content of the page.
date
Type:
stringDetails:
Date of the page, in 'yyyy-MM-dd' format.
Example:
'0000-00-00''2021-08-16'
Also see:
deps
Type:
string[]Details:
Dependencies of the page.
For example, if users import code snippet in the page, the absolute file path of the imported file would be added to
deps.Also see:
links
- Type:
MarkdownLink[]
interface MarkdownLink {
raw: string
relative: string
absolute: string
}Details:
Links included in the page content.
markdownEnv
Type:
Record<string, unknown>Details:
The
envobject when parsing markdown content with markdown-it.Some markdown-it plugins may store extra information inside this object, and you can make use of them for advanced customization.
Notice that some other page properties are also extracted from the original
envobject. Those properties have already been removed frompage.markdownEnv.Also see:
pathInferred
Type:
string | nullDetails:
Route path of the page that inferred from file path.
By default, the route path is inferred from the relative file path of the Markdown source file. However, users may explicitly set the route path, e.g. permalink, which would be used as the final route path of the page. So we keep the inferred path as a page property in case you may need it.
It would be
nullif the page does not come from a Markdown source file.Example:
'/''/foo.html'
Also see:
pathLocale
Type:
stringDetails:
Locale prefix of the page route path.
It is inferred from the relative file path of the Markdown source file and the key of
localesoption in user config.Example:
'/''/en/''/zh/'
Also see:
permalink
Type:
string | nullDetails:
Permalink of the page.
Also see:
routeMeta
Type:
Record<string, unknown>Details:
Custom data to be attached to the page route.
Also see:
What's the difference between route meta and page data?
Both route meta and page data is available in client side. However, route meta is attached to the page route record, so the route meta of all pages would be loaded at once when users enter your site. In the contrast, page data is saved in separated files, which would be loaded only when users enter the corresponding page.
Therefore, it's not recommended to store large amounts of info into route meta, otherwise the initial loading speed will be affected a lot when your site has a large number of pages.
sfcBlocks
Type:
MarkdownSfcBlocksDetails:
Extracted vue SFC blocks of the page.
Also see:
slug
Type:
stringDetails:
Slug of the page.
It is inferred from the filename of the Markdown source file.
filePath
Type:
string | nullDetails:
Absolute path of the Markdown source file of the page.
It would be
nullif the page does not come from a Markdown source file.
filePathRelative
Type:
string | nullDetails:
Relative path of the Markdown source file of the page.
It would be
nullif the page does not come from a Markdown source file.
