fix(code style) /docs/zh/guide

pull/2249/head
vanch 3 years ago
parent 68bfa301c9
commit 420d04b118

@ -8,7 +8,7 @@ outline: deep
将 VitePress 连接到 CMS 主要围绕 [动态路由](./routing#dynamic-routes) 而展开。在继续之前,请确保了解它的工作原理。
由于每个 CMS 的工作方式都不同,因此我们只能提供一个通用的工作流,你需要根据具体情况进行调整。
由于每个 CMS 的工作方式都不同,因此我们只能提供一个通用的工作流,你需要根据具体情况进行调整。
1. 如果你的 CMS 需要身份验证,请创建一个 `.env` 文件来存储你的 API 令牌并加载它:
@ -27,7 +27,7 @@ outline: deep
// 使用相应的 CMS 客户端库 (如果需要的话)
const data = await (await fetch('https://my-cms-api', {
headers: {
// 如果需要token 的话请在这里填写
// 如果需要 token 的话请在这里填写
}
})).json()
@ -53,4 +53,4 @@ outline: deep
## 整合指南 {#integration-guides}
如果你已经写了一篇关于如何将 VitePress 与特定 CMS 集成的指南,请点击下面的“在 GitHub 上编辑此页面”链接将它提交到这里!
如果你已经写了一篇关于如何将 VitePress 与特定 CMS 集成的指南,请点击下面的 “在 GitHub 上编辑此页面” 链接将它提交到这里!

@ -13,7 +13,7 @@ VitePress 提供了一个叫做**数据加载器**的功能,它允许你加载
export default {
load() {
return {
data: 'hello'
hello: 'world'
}
}
}
@ -23,7 +23,7 @@ export default {
然后,你可以在 `.md` 页面和 `.vue` 组件中使用 `data` 命名导出从该文件中导入数据:
```html
```vue
<script setup>
import { data } from './example.data.js'
</script>
@ -31,11 +31,11 @@ import { data } from './example.data.js'
<pre>{{ data }}</pre>
```
输出:
输出
```json
{
"data": "hello"
"hello": "world"
}
```
@ -70,7 +70,7 @@ export default {
// watchedFiles will be an array of absolute paths of the matched files.
// generate an array of blog post metadata that can be used to render
// a list in the theme layout
return watchedFiles.map(file => {
return watchedFiles.map((file) => {
return parse(fs.readFileSync(file, 'utf-8'), {
columns: true,
skip_empty_lines: true
@ -99,7 +99,8 @@ export default createContentLoader('posts/*.md', /* options */)
```ts
interface ContentData {
// mapped absolute URL for the page. e.g. /posts/hello.html
// mapped URL for the page. e.g. /posts/hello.html (does not include base)
// manually iterate or use custom `transform` to normalize the paths
url: string
// frontmatter data of the page
frontmatter: Record<string, any>
@ -147,9 +148,9 @@ export default createContentLoader('posts/*.md', {
// the final result is what will be shipped to the client.
return rawData.sort((a, b) => {
return +new Date(b.frontmatter.date) - +new Date(a.frontmatter.date)
}).map(page => {
page.src // raw markdown source
page.html // rendered full page HTML
}).map((page) => {
page.src // raw markdown source
page.html // rendered full page HTML
page.excerpt // rendered excerpt HTML (content above first `---`)
return {/* ... */}
})
@ -171,6 +172,47 @@ export default {
}
```
**类型**
```ts
interface ContentOptions<T = ContentData[]> {
/**
* Include src?
* @default false
*/
includeSrc?: boolean
/**
* Render src to HTML and include in data?
* @default false
*/
render?: boolean
/**
* If `boolean`, whether to parse and include excerpt? (rendered as HTML)
*
* If `function`, control how the excerpt is extracted from the content.
*
* If `string`, define a custom separator to be used for extracting the
* excerpt. Default separator is `---` if `excerpt` is `true`.
*
* @see https://github.com/jonschlinkert/gray-matter#optionsexcerpt
* @see https://github.com/jonschlinkert/gray-matter#optionsexcerpt_separator
*
* @default false
*/
excerpt?:
| boolean
| ((file: { data: { [key: string]: any }; content: string; excerpt?: string }, options?: any) => void)
| string
/**
* 转换数据。请注意,如果从组件或 Markdown 文件导入,数据将以 JSON 形式内联到客户端包中。
*/
transform?: (data: ContentData[]) => T | Promise<T>
}
```
## 为数据加载器导出类型 {#typed-data-loaders}
当使用 TypeScript 时,你可以像这样为加载器和 `data` 导出类型:
@ -193,3 +235,13 @@ export default defineLoader({
}
})
```
## 配置 {#configuration}
要获取加载器中的配置信息,可以使用如下代码:
```ts
import type { SiteConfig } from 'vitepress'
const config: SiteConfig = (globalThis as any).VITEPRESS_CONFIG
```

@ -12,46 +12,46 @@ outline: deep
```json
{
"scripts": {
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
}
"scripts": {
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
}
}
```
## 本地构建与测试 {#build-and-test-locally}
- 你可以运行以下命令来构建文档:
1. 你可以运行以下命令来构建文档:
```sh
yarn docs:build
```
```sh
$ npm run docs:build
```
- 构建文档后,通过运行以下命令在本地预览它:
2. 构建文档后,通过运行以下命令在本地预览它:
```sh
yarn docs:preview
```
```sh
$ npm run docs:preview
```
`preview` 命令将启动一个本地静态 Web 服务器`http://localhost:4173`,该服务器以 `.vitepress/dist` 作为源文件。这是检查生产版本在本地环境中是否正常的一种简单方法。
- 你可以通过传递`--port`作为参数来配置服务器的端口。
3. 你可以通过传递`--port`作为参数来配置服务器的端口。
```json
{
"scripts": {
"docs:preview": "vitepress preview docs --port 8080"
}
}
```
```json
{
"scripts": {
"docs:preview": "vitepress preview docs --port 8080"
}
}
```
现在`docs:preview`方法将在`http://localhost:8080`启动服务器。
现在 `docs:preview` 方法将在 `http://localhost:8080` 启动服务器。
## 设定 public 根目录 {#setting-a-public-base-path}
默认情况下,我们假设站点将部署在域名 `/`)的根路径上。如果你的网站将在子路径中提供服务,例如 `https://mywebsite.com/blog/`,则需要在 VitePress 配置中将 [`base`](../reference/site-config#base)选项设置为 `'/blog/'`
**例:**如果你使用的是 Github或 GitLab页面并部署到 `user.github.io/repo/`,请将你的 `base` 设置为 `/repo/`
**例:** 如果你使用的是 Github或 GitLab页面并部署到 `user.github.io/repo/`,请将你的 `base` 设置为 `/repo/`
## HTTP 缓存标头 {#http-cache-headers}
@ -65,7 +65,7 @@ outline: deep
Cache-Control: max-age=31536000,immutable
```
:::details Netlify 示例 `_headers` 文件
::: details Netlify 示例 `_headers` 文件
```
/assets/*
@ -79,21 +79,21 @@ Cache-Control: max-age=31536000,immutable
:::
:::details Vercel 配置示例 `vercel.json`
::: details Vercel 配置示例 `vercel.json`
```json
{
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=31536000, immutable"
}
]
}
]
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=31536000, immutable"
}
]
}
]
}
```
@ -111,100 +111,107 @@ Cache-Control: max-age=31536000,immutable
- **构建命令:** `npm run docs:build`
- **输出目录:** `docs/.vitepress/dist`
- **node 版本:** `16` (或更高版本,默认情况下通常为 14 或 16但在 Cloudflare 页面上,默认值仍然是 12因此你可能需要[更改该版本](https://developers.cloudflare.com/pages/platform/build-configuration/))
::: warning 警告
- **node 版本:** `18` (或更高版本)
::: warning
不要为 HTML 代码启用 _Auto Minify_ 等选项。它将从输出中删除对 Vue 有意义的注释。如果被删除,你可能会看到 hydration mismatch 错误。
:::
### GitHub Pages
1. 在你的 theme 配置文件中, `docs/.vitepress/config.js`, 设置 `base` 为 GitHub 仓库的名称。如果你打算把站点部署到 `https://foo.github.io/bar/`,那你就需要把 `base` 设置为 `'/bar/'`。它始终以 `/` 开头结尾。
2. 在项目目录 `.github/workflows` 下创建一个名为 `deploy.yml` 的文件,包含以下内容:
1. 在项目的 `.github/workflows` 目录中创建一个名为 `deploy.yml` 的文件,其中包含这样的内容:
```yaml
name: Deploy
# 用于构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程
#
name: Deploy VitePress site to Pages
on:
workflow_dispatch: {}
# 针对 `main` 分支的推送上运行。
# 如果你使用 `master` 作为默认分支,请将其更改为 `master`
push:
branches:
- main
branches: [main]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: pages
cancel-in-progress: false
jobs:
deploy:
# Build job
build:
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
fetch-depth: 0 # Not needed if lastUpdated is not enabled
# - uses: pnpm/action-setup@v2 # Uncomment this if you're using pnpm
# - uses: oven-sh/setup-bun@v1 # Uncomment this if you're using Bun
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
cache: npm
- run: npm ci
- name: Build
run: npm run docs:build
- uses: actions/configure-pages@v2
- uses: actions/upload-pages-artifact@v1
node-version: 18
cache: npm # or pnpm / yarn
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Install dependencies
run: npm ci # or pnpm install / yarn install / bun install
- name: Build with VitePress
run: |
npm run docs:build # or pnpm docs:build / yarn docs:build / bun run docs:build
touch docs/.vitepress/dist/.nojekyll
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: docs/.vitepress/dist
- name: Deploy
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v2
```
::: tip 提示
请替换相应的分支名称。比如你要建的分支是 `master` ,那么你要把上面文件中的 `main` 换成 `master`
::: warning
确保 VitePress 中的 `base` 选项配置正确。有关更多详细信息,请参阅[设置 base 路径](#setting-a-public-base-path)
:::
3. 在仓库设置中找到 `Pages` 选项,在 `Build and deployment` 下的 `Source` 中选择 `GitHub Actions`
4. 现在提交你的代码并将其推送到 `main` 分支。
5. 等待 Actions 完成。
6. 在仓库设置中找到 `Pages` 选项,点击 `Visit site` 就可以看到你的网站。现在,你的文档将在你每次推送时自动部署。
2. 在存储库设置中的 “Pages” 菜单项下,选择 “Build and deployment > Source > GitHub Actions”。
3. 将更改推送到 `main` 分支并等待 GitHub Actions 工作流完成。你应该看到你的站点部署到 `https://<username>.github.io/[repository]/``https://<custom-domain>/`,这取决于您的设置。你的网站将在每次推送到 `main` 分支时自动部署。
### GitLab Pages
1. `docs/.vitepress/config.js` 中的 `outDir` 设置为 `../public`
1. 如果你想部署到 `https://<username> .gitlab.io/<repository> /`,将 VitePress 配置中的 `outDir` 设置为 `../public`。将 `base` 选项配置为 `'/<repository>/'`
2. 在 `docs/.vitepress/config.js` 配置文件中,将 `base` 属性设置为 GitLab 存储库的名称。如果计划将站点部署到 `https://foo.gitlab.io/bar/`,则应将 `base` 设置为 `'/bar/'`。它应始终以 `/`开头和结尾。
2. 在项目的根目录中创建一个名为 `.gitlab-ci.yml` 的文件,其中包含以下内容。每当您更改内容时,这都会构建和部署您的网站:
3. 使用以下内容在项目的根目录中创建一个名为 `.gitlab-ci.yml` 的文件。每当你更改内容时,会自动构建和部署你的站点:
```yaml
image: node:16
pages:
cache:
paths:
- node_modules/
script:
- npm install
- npm run docs:build
artifacts:
paths:
- public
only:
- main
```
4. 或者,如果要使用 _alpine_ 版本的 node则必须手动安装 `git`。在这种情况下,上面的代码修改为:
```yaml
image: node:16-alpine
image: node:18
pages:
cache:
paths:
- node_modules/
before_script:
- apk add git
script:
# - apk add git # Uncomment this if you're using small docker images like alpine and have lastUpdated enabled
- npm install
- npm run docs:build
artifacts:
@ -232,10 +239,10 @@ Cache-Control: max-age=31536000,immutable
```json
{
"hosting": {
"public": "docs/.vitepress/dist",
"ignore": []
}
"hosting": {
"public": "docs/.vitepress/dist",
"ignore": []
}
}
```
@ -243,13 +250,13 @@ Cache-Control: max-age=31536000,immutable
```json
{
"projects": {
"default": "<YOUR_FIREBASE_ID>"
}
"projects": {
"default": "<YOUR_FIREBASE_ID>"
}
}
```
2. 运行 `yarn docs:build` 后,运行此命令进行部署:
2. 运行 `npm run docs:build` 后,运行此命令进行部署:
```sh
firebase deploy
@ -257,7 +264,7 @@ Cache-Control: max-age=31536000,immutable
### Surge
1. 运行 `yarn docs:build` 后,运行此命令进行部署:
1. 运行 `npm run docs:build` 后,运行此命令进行部署:
```sh
npx surge docs/.vitepress/dist
@ -271,7 +278,7 @@ Cache-Control: max-age=31536000,immutable
```json
{
"root": "docs/.vitepress/dist"
"root": "docs/.vitepress/dist"
}
```

@ -62,7 +62,7 @@ export default DefaultTheme
}
```
:::warning
::: warning
如果你在使用像是[团队页](/reference/default-theme-team-page)这样的组件,请确保也在从 `vitepress/theme-without-fonts` 中导入它们!
:::
@ -109,7 +109,6 @@ export default {
```
如果你使用 TypeScript:
```ts
// .vitepress/theme/index.ts
import type { Theme } from 'vitepress'
@ -181,6 +180,8 @@ export default {
默认主题布局的全部可用插槽如下:
- 当 `layout: 'doc'` (默认) 在 frontmatter 中被启用时:
- `doc-top`
- `doc-bottom`
- `doc-footer-before`
- `doc-before`
- `doc-after`
@ -199,6 +200,9 @@ export default {
- `home-hero-after`
- `home-features-before`
- `home-features-after`
- 当 `layout: 'page'` 在 frontmatter 中被启用时:
- `page-top`
- `page-bottom`
- 当未找到页面 (404) 时:
- `not-found`
- 总是启用:
@ -211,6 +215,101 @@ export default {
- `nav-screen-content-before`
- `nav-screen-content-after`
### 关于外观切换 {#on-appearance-toggle}
You can extend the default theme to provide a custom transition when the color mode is toggled. An example:
你可以扩展默认主题以在切换颜色模式时提供自定义过渡动画。一个例子:
```vue
<!-- .vitepress/theme/Layout.vue -->
<script setup lang="ts">
import { useData } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import { nextTick, provide } from 'vue'
const { isDark } = useData()
const enableTransitions = () =>
'startViewTransition' in document &&
window.matchMedia('(prefers-reduced-motion: no-preference)').matches
provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => {
if (!enableTransitions()) {
isDark.value = !isDark.value
return
}
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
`circle(${Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y)
)}px at ${x}px ${y}px)`
]
await document.startViewTransition(async () => {
isDark.value = !isDark.value
await nextTick()
}).ready
document.documentElement.animate(
{ clipPath: isDark.value ? clipPath.reverse() : clipPath },
{
duration: 300,
easing: 'ease-in',
pseudoElement: `::view-transition-${isDark.value ? 'old' : 'new'}(root)`
}
)
})
</script>
<template>
<DefaultTheme.Layout />
</template>
<style>
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}
::view-transition-old(root),
.dark::view-transition-new(root) {
z-index: 1;
}
::view-transition-new(root),
.dark::view-transition-old(root) {
z-index: 9999;
}
.VPSwitchAppearance {
width: 22px !important;
}
.VPSwitchAppearance .check {
transform: none !important;
}
</style>
```
结果(**谨慎使用!**:闪烁的颜色、突然的移动、高亮度):
<details>
<summary>Demo</summary>
![Appearance Toggle Transition Demo](/appearance-toggle-transition.webp)
</details>
有关视图过渡动画的更多详细信息,请参阅 [Chrome 文档](https://developer.chrome.com/docs/web-platform/view-transitions/)。
### 路由切换时 {#on-route-change}
即将到来。
## 重写内部组件 {#overriding-internal-components}
你可以使用 Vite 的 [aliases](https://vitejs.dev/config/shared-options.html#resolve-alias) 来用你的自定义组件替换默认主题的组件:

@ -27,7 +27,7 @@ title: Docs with VitePress
editLink: true
---
# {{ $frontmatter.title }} {#{{-$frontmatter.title-}}}
# {{ $frontmatter.title }}
Guide content
```

@ -42,7 +42,8 @@ $ bun add -D vitepress
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"@algolia/client-search"
"@algolia/client-search",
"search-insights"
]
}
}
@ -66,7 +67,11 @@ $ npx vitepress init
```
```sh [pnpm]
$ pnpm exec vitepress init
$ pnpm dlx vitepress init
```
```sh [bun]
$ bunx vitepress init
```
:::
@ -120,6 +125,7 @@ export default {
```
还可以通过 `themeConfig` 选项配置主题的行为。有关所有配置选项的完整详细信息,请参见[配置参考](../reference/site-config)。
### 源文件 {#source-files}
`.vitepress` 目录之外的 Markdown 文件被视为**源文件**。
@ -128,7 +134,7 @@ VitePress 使用 **基于文件的路由**:每个 `.md` 文件将在相同的
VitePress 还提供了生成简洁 URL、重写路径和动态生成页面的能力。这些将在[路由指南](./routing)中进行介绍。
## 运行 {#up-and-running}
## 启动并运行 {#up-and-running}
该工具还应该将以下 npm 脚本注入到 `package.json` 中:
@ -160,6 +166,10 @@ $ pnpm run docs:dev
$ yarn docs:dev
```
```sh [bun]
$ bun run docs:dev
```
:::
除了 npm 脚本,还可以直接调用 VitePress
@ -174,6 +184,10 @@ $ npx vitepress dev docs
$ pnpm exec vitepress dev docs
```
```sh [bun]
$ bunx vitepress dev docs
```
:::
更多的命令行用法请参见 [CLI 参考](../reference/cli)。

@ -17,7 +17,7 @@ docs/
import { defineConfig } from 'vitepress'
export default defineConfig({
// shared properties and other top-level stuff...
// 共享属性和其他外层内容......
locales: {
root: {
@ -26,10 +26,10 @@ export default defineConfig({
},
fr: {
label: 'French',
lang: 'fr', // optional, will be added as `lang` attribute on `html` tag
link: '/fr/guide' // default /fr/ -- shows on navbar translations menu, can be external
lang: 'fr', // 可选,将作为 `lang` 属性添加到 `html` 标签上
link: '/fr/guide' // default /fr/ -- 显示在导航栏翻译菜单上,可以是外部的链接
// other locale specific properties...
// 其他 locale 特定属性...
}
}
})

@ -1,20 +1,20 @@
# Markdown 展 {#markdown-extensions}
# Markdown 展 {#markdown-extensions}
VitePress 带有内置的 Markdown 展。
VitePress 带有内置的 Markdown 展。
## 标题锚点 {#header-anchors}
标题会自动应用锚点。可以使用 `markdown.anchor` 选项配置锚点的渲染。
### Custom anchors
### 自定义锚点 {#custom-anchors}
To specify a custom anchor tag for a heading instead of using the auto-generated one, add a suffix to the heading:
要为标题指定自定义锚点而不是使用自动生成的锚点,请向标题添加后缀:
```
# Using custom anchors {#my-anchor}
# 使用自定义锚点 {#my-anchor}
```
This allows you to link to the heading as `#my-anchor` instead of the default `#using-custom-anchors`.
这允许你将标题链接为 `#my-anchor`,而不是默认的 `#使用自定义锚点`
## 链接 {#links}
@ -96,7 +96,7 @@ lang: en-US
| col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 |
## Emoji :tada:
## Emoji :tada:
**输入**
@ -178,16 +178,16 @@ This is a details block.
### 自定义标题 {#custom-title}
可以通过在容器的“类型”之后附加文本来设置自定义标题。
可以通过在容器的 "type" 之后附加文本来设置自定义标题。
**输入**
````md
::: danger STOP
Danger zone, do not proceed
危险区域,请勿继续
:::
::: details Click me to view the code
::: details 点我查看代码
```js
console.log('Hello, VitePress!')
```
@ -197,15 +197,34 @@ console.log('Hello, VitePress!')
**输出**
::: danger STOP
Danger zone, do not proceed
危险区域,请勿继续
:::
::: details Click me to view the code
::: details 点我查看代码
```js
console.log('Hello, VitePress!')
```
:::
此外,你可以通过在站点配置中添加以下内容来全局设置自定义标题,如果不是用英语书写,这会很有帮助:
```ts
// config.ts
export default defineConfig({
// ...
markdown: {
container: {
tipLabel: '提示',
warningLabel: '警告',
dangerLabel: '危险',
infoLabel: '信息',
detailsLabel: '详细信息'
}
}
// ...
})
```
### `raw`
这是一个特殊的容器,可以用来防止与 VitePress 的样式和路由冲突。这在记录组件库时特别有用。你可能还想查看 [whyframe](https://whyframe.dev/docs/integrations/vitepress) 以获得更好的隔离。
@ -220,32 +239,29 @@ Wraps in a <div class="vp-raw">
`vp-raw` class 也可以直接用于元素。样式隔离目前是可选的:
::: details
- 使用你喜欢的包管理器来安装需要的依赖项:
```sh
$ npm install -D postcss postcss-prefix-selector
$ npm add -D postcss
```
- 创建 `docs/.postcssrc.cjs` 并将以下内容
```js
module.exports = {
plugins: {
'postcss-prefix-selector': {
prefix: ':not(:where(.vp-raw *))',
includeFiles: [/vp-doc\.css/],
transform(prefix, _selector) {
const [selector, pseudo = ''] = _selector.split(/(:\S*)$/)
return selector + prefix + pseudo
}
}
}
import { postcssIsolateStyles } from 'vitepress'
export default {
plugins: [postcssIsolateStyles()]
}
```
:::
It uses [`postcss-prefix-selector`](https://github.com/postcss/postcss-load-config) under the hood. You can pass its options like this:
```js
postcssIsolateStyles({
includeFiles: [/vp-doc\.css/] // defaults to /base\.css/
})
```
## 代码块中的语法高亮 {#syntax-highlighting-in-code-blocks}
@ -521,6 +537,12 @@ const line3 = 'This is line 3'
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts:line-numbers=2 {1}
// line-numbers is enabled and start from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'
```
````
**输出**
@ -537,6 +559,12 @@ const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts:line-numbers=2 {1}
// line-numbers is enabled and start from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'
```
## 导入代码片段 {#import-code-snippets}
你可以通过下面的语法来从现有文件中导入代码片段:
@ -563,7 +591,7 @@ const line3 = 'This is line 3'
**输出**
<<< @/snippets/snippet.js{2}
<<< @/snippets/snippet.js
::: tip
`@` 的值对应于源代码根目录,默认情况下是 VitePress 项目根目录,除非配置了 `srcDir`。或者你也可以从相对路径导入:
@ -714,14 +742,14 @@ export default config
<!--@include: ./parts/basics.md-->
```
**另一个文件** (`parts/basics.md`)
**部分文件** (`parts/basics.md`)
```md
Some getting started stuff.
一些入门的东西。
### Configuration {#configuration}
Can be created using `.foorc.json`.
可以使用 `.foorc.json` 创建。
```
**等价代码**
@ -731,16 +759,16 @@ Can be created using `.foorc.json`.
## Basics {#basics}
Some getting started stuff.
一些入门的东西。
### Configuration {#configuration}
Can be created using `.foorc.json`.
可以使用 `.foorc.json` 创建。
```
It also supports selecting a line range:
它还支持选择行范围:
**Input**
**输入**
```md
# Docs
@ -753,14 +781,14 @@ It also supports selecting a line range:
**Part file** (`parts/basics.md`)
```md
Some getting started stuff.
一些入门的东西。
### Configuration
Can be created using `.foorc.json`.
可以使用 `.foorc.json` 创建。
```
**Equivalent code**
**等价代码**
```md
# Docs
@ -769,15 +797,60 @@ Can be created using `.foorc.json`.
### Configuration
Can be created using `.foorc.json`.
可以使用 `.foorc.json` 创建。
```
The format of the selected line range can be: `{3,}`, `{,10}`, `{1,10}`
所选行范围的格式可以是: `{3,}``{,10}`、`{1,10}`
::: warning
如果你指定的文件不存在,这将不会产生错误。因此,在使用这个功能的时候请保证内容按预期呈现。
:::
## 数学方程 {#math-equations}
This is currently opt-in. 要启用它, 你需要安装 `markdown-it-mathjax3`,在配置文件中设置`markdown.math` 为 `true`
```sh
npm add -D markdown-it-mathjax3
```
```ts
// .vitepress/config.ts
export default {
markdown: {
math: true
}
}
```
**输入**
```md
当 $a \ne 0$, $(ax^2 + bx + c = 0)$ 有两个解,它们是
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
**Maxwell's 方程组:**
| 方程 | 描述 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
```
**输出**
当 $a \ne 0$$(ax^2 + bx + c = 0)$ 有两个解,它们是
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
**Maxwell's 方程组:**
| 方程 | 描述 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
## 高级配置 {#advanced-configuration}
VitePress 使用 [markdown-it](https://github.com/markdown-it/markdown-it) 作为 Markdown 渲染器。上面提到的很多拓展功能都是通过自定义插件实现的。你可以使用 `.vitepress/config.js` 中的 `markdown` 选项来进一步自定义 `markdown-it` 实例。

@ -4,7 +4,7 @@
## 应用配置 {#app-config}
- The internationalization feature is not yet implemented.
- 国际化功能尚未实现。
## 主题配置 {#theme-config}

@ -10,7 +10,7 @@
### 图片 {#images}
与 VuePress 不同在使用静态图片时VitePress 会根据你的配置自动处理这些 `base`(./asset-handling#base-url)。
与 VuePress 不同在使用静态图片时VitePress 会根据你的配置自动处理这些 [`base`](./asset-handling#base-url)。
因此,现在你可以在没有 `img` 标签的情况下渲染图像。
@ -19,7 +19,7 @@
+ ![foo](/foo.png)
```
::: warning 警告
::: warning
对于动态图像,你仍然需要 `withBase`,如 [Base URL](./asset-handling#base-url) 中所示。
:::

@ -15,7 +15,7 @@ document.querySelector('h1').addEventListener('click', () => {
})
</script>
# Hello {#hello}
# Hello
```
`<script client>` 是 VitePress 独有的功能,而不是 Vue 的功能。它可以在 `.md``.vue` 文件中使用,但只能在 MPA 模式下使用。所有主题组件中的客户端脚本将被打包在一起,而特定页面的客户端脚本将会分开处理。

@ -85,12 +85,10 @@ src/getting-started.md --> /getting-started.html
```md
<!-- 正确做法 -->
[Getting Started](./getting-started)
[Getting Started](../guide/getting-started)
<!-- 不正确做法 -->
[Getting Started](./getting-started.md)
[Getting Started](./getting-started.html)
```
@ -181,10 +179,10 @@ packages/pkg-b/src/pkg-b-docs.md --> /pkg-b/index.html
```ts
// .vitepress/config.js
export default {
rewrites: {
'packages/pkg-a/src/pkg-a-docs.md': 'pkg-a/index.md',
'packages/pkg-b/src/pkg-b-docs.md': 'pkg-b/index.md',
},
rewrites: {
'packages/pkg-a/src/pkg-a-docs.md': 'pkg-a/index.md',
'packages/pkg-b/src/pkg-b-docs.md': 'pkg-b/index.md'
}
}
```
@ -192,22 +190,21 @@ export default {
```ts
export default {
rewrites: {
'packages/:pkg/src/(.*)': ':pkg/index.md',
},
rewrites: {
'packages/:pkg/src/(.*)': ':pkg/index.md'
}
}
```
重写路径是使用 `path-to-regexp` 包编译的 - 请参阅[其文档](https://github.com/pillarjs/path-to-regexp#parameters)以获取更高级的语法。
:::warning 开启重写功能时使用相对链接
::: warning 开启重写功能时使用相对链接
启用重写后,**相对链接应基于重写的路径**。例如,为了创建从 `packages/pkg-a/src/pkg-a-code.md``packages/pkg-b/src/pkg-b-code.md` 的相对链接,你应该使用:
```md
[Link to PKG B](../pkg-b/pkg-b-code)
```
:::
## 动态路由 {#dynamic-routes}
@ -232,9 +229,12 @@ export default {
```js
// packages/[pkg].paths.js
export default {
paths() {
return [{ params: { pkg: 'foo' } }, { params: { pkg: 'bar' } }]
},
paths() {
return [
{ params: { pkg: 'foo' }},
{ params: { pkg: 'bar' }}
]
}
}
```
@ -264,12 +264,12 @@ export default {
```js
export default {
paths: () => [
{ params: { pkg: 'foo', version: '1.0.0' } },
{ params: { pkg: 'foo', version: '2.0.0' } },
{ params: { pkg: 'bar', version: '1.0.0' } },
{ params: { pkg: 'bar', version: '2.0.0' } },
],
paths: () => [
{ params: { pkg: 'foo', version: '1.0.0' }},
{ params: { pkg: 'foo', version: '2.0.0' }},
{ params: { pkg: 'bar', version: '1.0.0' }},
{ params: { pkg: 'bar', version: '2.0.0' }}
]
}
```
@ -292,12 +292,15 @@ export default {
```js
import fs from 'fs'
export default {
paths() {
return fs.readdirSync('packages').map((pkg) => {
return { params: { pkg } }
})
},
paths() {
return fs
.readdirSync('packages')
.map((pkg) => {
return { params: { pkg }}
})
}
}
```
@ -305,17 +308,18 @@ export default {
```js
export default {
async paths() {
const pkgs = await (await fetch('https://my-api.com/packages')).json()
return pkgs.map((pkg) => {
return {
params: {
pkg: pkg.name,
version: pkg.version,
},
}
})
},
async paths() {
const pkgs = await (await fetch('https://my-api.com/packages')).json()
return pkgs.map((pkg) => {
return {
params: {
pkg: pkg.name,
version: pkg.version
}
}
})
}
}
```
@ -333,8 +337,10 @@ export default {
```vue
<script setup>
import { useData } from 'vitepress'
// params is a Vue ref
const { params } = useData()
console.log(params.value)
</script>
```
@ -347,16 +353,15 @@ console.log(params.value)
```js
export default {
paths() {
async paths() {
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
return posts.map((post) => {
return {
params: { id: post.id },
content: post.content // raw Markdown or HTML
}
})
}
async paths() {
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
return posts.map((post) => {
return {
params: { id: post.id },
content: post.content // raw Markdown or HTML
}
})
}
}
```

@ -1,6 +1,6 @@
# Sitemap Generation
# Sitemap 生成器 {#sitemap-generation}
VitePress comes with out-of-the-box support for generating a `sitemap.xml` file for your site. To enable it, add the following to your `.vitepress/config.js`:
VitePress 提供开箱即用的为你的网站生成 `sitemap.xml` 文件。要启用它,请将以下内容添加到 `.vitepress/config.js` 中:
```ts
import { defineConfig } from 'vitepress'
@ -12,11 +12,11 @@ export default defineConfig({
})
```
To have `<lastmod>` tags in your `sitemap.xml`, you can enable the [`lastUpdated`](../reference/default-theme-last-updated) option.
要在 `sitemap.xml` 中有 `<lastmod>` 标签,你可以启用 [`lastUpdated`](../reference/default-theme-last-updated) 选项。
## Options
## 选项 {#options}
Sitemap support is powered by the [`sitemap`](https://www.npmjs.com/package/sitemap) module. You can pass any options supported by it to the `sitemap` option in your config file. These will be passed directly to the `SitemapStream` constructor. Refer to the [`sitemap` documentation](https://www.npmjs.com/package/sitemap#options-you-can-pass) for more details. Example:
站点地图由 [`sitemap`](https://www.npmjs.com/package/sitemap) 模块提供支持。你可以将其支持的任何选项传递给配置文件中的 `sitemap` 选项。这些将直接传递给 `SitemapStream` 构造函数。有关更多详细信息,请参阅 [`sitemap` 文档](https://www.npmjs.com/package/sitemap#options-you-can-pass)。例如:
```ts
import { defineConfig } from 'vitepress'
@ -31,7 +31,7 @@ export default defineConfig({
## `transformItems` Hook
You can use the `sitemap.transformItems` hook to modify the sitemap items before they are written to the `sitemap.xml` file. This hook is called with an array of sitemap items and expects an array of sitemap items to be returned. Example:
在将站点地图项写入 `sitemap.xml` 文件之前,你可以使用 `sitemap.transformItems` 钩子来修改站点地图项。使用站点地图项数组调用此挂钩,并期望返回站点地图项数组。例子:
```ts
import { defineConfig } from 'vitepress'
@ -40,7 +40,7 @@ export default defineConfig({
sitemap: {
hostname: 'https://example.com',
transformItems: (items) => {
// add new items or modify/filter existing items
// 添加新项目或修改/过滤现有项目
items.push({
url: '/extra-page',
changefreq: 'monthly',

@ -65,19 +65,18 @@ export default {
```
如果你使用 TypeScript:
```ts
// .vitepress/theme/index.ts
import type { Theme } from 'vitepress'
export default {
// ...
async enhanceApp({ app }) {
if (!import.meta.env.SSR) {
const plugin = await import('plugin-that-access-window-on-import')
app.use(plugin)
}
},
// ...
async enhanceApp({ app }) {
if (!import.meta.env.SSR) {
const plugin = await import('plugin-that-access-window-on-import')
app.use(plugin)
}
}
} satisfies Theme
```
@ -99,4 +98,39 @@ const ClientComp = defineClientComponent(() => {
</template>
```
目标组件只会在包装组件的 mounted 钩子中导入。
你还可以将 props/children/slots 传递给目标组件:
```vue
<script setup>
import { ref } from 'vue'
import { defineClientComponent } from 'vitepress'
const clientCompRef = ref(null)
const ClientComp = defineClientComponent(
() => import('component-that-access-window-on-import'),
// args are passed to h() - https://vuejs.org/api/render-function.html#h
[
{
ref: clientCompRef
},
{
default: () => 'default slot',
foo: () => h('div', 'foo'),
bar: () => [h('span', 'one'), h('span', 'two')]
}
],
// callback after the component is loaded, can be async
() => {
console.log(clientCompRef.value)
}
)
</script>
<template>
<ClientComp />
</template>
```
The target component will only be imported in the mounted hook of the wrapper component.

@ -201,7 +201,7 @@ Hello {{ 1 + 1 }}
VitePress [内置支持](https://cn.vitejs.dev/guide/features.html#css-pre-processors) CSS 预处理器:`.scss`、`.sass`、.`less`、`.styl` 和 `.stylus` 文件。无需为它们安装 Vite 专用插件,但必须安装相应的预处理器:
```
# .scss and .sass
# .scss and .sass
npm install -D sass
# .less
@ -242,6 +242,7 @@ Vitepress 目前只有使用 teleport 传送到 body 的 SSG 支持。对于其
<script setup>
import ModalDemo from '../../components/ModalDemo.vue'
import ComponentInHeader from '../../components/ComponentInHeader.vue'
</script>
<style>

@ -4,7 +4,7 @@
使用指定目录作为根目录来启动 VitePress 开发服务器。默认为当前目录。在当前目录下运行时也可以省略 `dev` 命令。
### 用法
### 用法 {#usage}
```sh
# 从当前目录开始,省略 `dev`
@ -14,7 +14,7 @@ vitepress
vitepress dev [root]
```
### 选项
### 选项 {#options}
| 选项 | 说明 |
| --------------- | ------------------------------------------ |
@ -29,13 +29,13 @@ vitepress dev [root]
构建用于生产环境的 VitePress 站点。
### 用法 {#usage}
### 用法 {#usage-1}
```sh
vitepress build [root]
```
### 选项 {#options}
### 选项 {#options-1}
| 选项 | 说明 |
| ------------------------------ | ------------------------------------------------------------------------------------------------- |
@ -50,13 +50,13 @@ vitepress build [root]
在本地预览生产版本。
### 用法 {#usage-1}
### 用法 {#usage-2}
```sh
vitepress preview [root]
```
### 选项 {#options-1}
### 选项 {#options-2}
| 选项 | 说明 |
| --------------- | -------------------------------------- |
@ -67,7 +67,7 @@ vitepress preview [root]
在当前目录中启动[安装向导](../guide/getting-started#setup-wizard)。
### 用法
### 用法 {#usage-3}
```sh
vitepress init

@ -1,4 +1,4 @@
# 标 {#badge}
# 标 {#badge}
徽标可让你为标题添加状态。例如,指定部分的类型或支持的版本可能很有用。
@ -7,21 +7,20 @@
你可以使用全局组件 `Badge`
```html
### Title <Badge type="info" text="default" /> ### Title <Badge type="tip" text="^1.9.0" /> ### Title <Badge type="warning" text="beta" /> ### Title
<Badge type="danger" text="caution" />
### Title <Badge type="info" text="default" />
### Title <Badge type="tip" text="^1.9.0" />
### Title <Badge type="warning" text="beta" />
### Title <Badge type="danger" text="caution" />
```
上面的代码渲染如下:
### Title <Badge type="info" text="default" />
### Title <Badge type="tip" text="^1.9.0" />
### Title <Badge type="warning" text="beta" />
### Title <Badge type="danger" text="caution" />
## 自定义 `children` {#custom-children}
## 自定义子节点 {#custom-children}
`<Badge>` 接受 `子节点`, 这将显示在徽标中。

@ -7,16 +7,16 @@ export default {
themeConfig: {
carbonAds: {
code: 'your-carbon-code',
placement: 'your-carbon-placement',
},
},
placement: 'your-carbon-placement'
}
}
}
```
这些值用于调用 carbon CDN 脚本,如下所示。
```js
;`//cdn.carbonads.com/carbon.js?serve=${code}&placement=${placement}`
`//cdn.carbonads.com/carbon.js?serve=${code}&placement=${placement}`
```
要了解有关 Carbon Ads 配置的更多信息,请访问 [Carbon Ads 网站](https://www.carbonads.net/)。

@ -19,7 +19,7 @@ export default {
**此页面上记录的选项仅适用于默认主题**。不同的主题需要不同的主题配置。使用自定义主题时,主题配置对象将传递给主题,以便主题可以基于它作出不同表现。
## i18nRouting {#i18nrouting}
## i18nRouting {#i18n-routing}
- key: `i18nRouting`
- Type: `boolean`
@ -36,16 +36,19 @@ export default {
```ts
export default {
themeConfig: {
logo: '/logo.svg',
},
logo: '/logo.svg'
}
}
```
```ts
type ThemeableImage = string | { src: string; alt?: string } | { light: string; dark: string; alt?: string }
type ThemeableImage =
| string
| { src: string; alt?: string }
| { light: string; dark: string; alt?: string }
```
## 站点标题开关 {#sitetitle}
## 站点标题开关 {#site-title}
- key: `siteTitle`
- Type: `string | false`
@ -55,8 +58,8 @@ type ThemeableImage = string | { src: string; alt?: string } | { light: string;
```ts
export default {
themeConfig: {
siteTitle: 'Hello World',
},
siteTitle: 'Hello World'
}
}
```
@ -67,7 +70,7 @@ export default {
导航菜单项的配置。你可以在[默认主题: 导航栏](./default-theme-nav#navigation-links) 了解更多详情。
```js
```ts
export default {
themeConfig: {
nav: [
@ -77,11 +80,11 @@ export default {
items: [
{ text: 'Item A', link: '/item-1' },
{ text: 'Item B', link: '/item-2' },
{ text: 'Item C', link: '/item-3' },
],
},
],
},
{ text: 'Item C', link: '/item-3' }
]
}
]
}
}
```
@ -115,7 +118,7 @@ interface NavItemWithChildren {
侧边栏菜单项的配置。你可以在[默认主题: 侧边栏](./default-theme-sidebar) 了解更多详情。
```js
```ts
export default {
themeConfig: {
sidebar: [
@ -169,34 +172,42 @@ export type SidebarItem = {
## 大纲开关 {#aside}
- key: `aside`
- Type: `boolean`
- Type: `boolean | 'left'`
- Default: `true`
- 每个页面可以通过 [frontmatter](./frontmatter-config#aside) 覆写
将此值设置为 `false` 可禁用 aside(大纲) 容器。
将此值设置为 `false` 可禁用 aside(大纲) 容器。\
将此值设置为 `true` 将在页面右侧渲染。\
将此值设置为 `left` 将在页面左侧渲染。
如果你想对所有页面禁用它,你应该使用 `outline: false`
## 大纲层级 {#outline}
- key: `outline`
- Type: `number | [number, number] | 'deep' | false`
- Default: `2`
- 每个页面可以通过 [frontmatter](./frontmatter-config#outline) 覆写
配置在大纲中显示的标题级别。你可以通过传递一个数字来指定一个特定的级别,或者你可以通过传递一个包含下限和上限的元组来提供一个级别范围。当传递等于 `[2, 6]``deep` 时,除 `h1` 外,所有标题级别都显示在轮廓中。设置 `false` 以隐藏轮廓。
- Type: `Outline | Outline['level'] | false`
- 每个页面可以通过 [frontmatter](./frontmatter-config#outline) 覆写层级
## 大纲标题 {#outlinetitle}
将此值设置为 `false` 可禁止渲染大纲容器。更多详情请参考该接口:
- key: `outlineTitle`
- Type: `string`
- Default: `On this page`
可用于自定义右侧边栏的标题(在大纲链接的顶部)。这在用另一种语言编写文档时很有用。
```ts
interface Outline {
/**
* 大纲中显示的标题级别。
* 单个数字表示仅显示该级别的标题。
* 如果传递一个元组,则第一个数字是最小级别,第二个数字是最大级别。
* `'deep'` 和`[2, 6]` 等效, 这意味着 `<h2>``<h6>` 都会展示。
*
* @default 2
*/
level?: number | [number, number] | 'deep'
```js
export default {
themeConfig: {
outlineTitle: 'In hac pagina',
},
/**
* The title to be displayed on the outline.
*
* @default 'On this page'
*/
label?: string
}
```
@ -206,21 +217,23 @@ export default {
你可以定义此选项以在导航栏中展示带有图标的社交帐户链接。
```js
```ts
export default {
themeConfig: {
socialLinks: [
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' },
{ icon: 'twitter', link: '...' },
// You can also add custom icons by passing SVG as string:
// 你还可以通过将 SVG 作为字符串传递来添加自定义图标:
{
icon: {
svg: '<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Dribbble</title><path d="M12...6.38z"/></svg>',
svg: '<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Dribbble</title><path d="M12...6.38z"/></svg>'
},
link: '...',
},
],
},
// 你也可以自定义标签别名以实现无障碍访问(可选但推荐):
ariaLabel: 'cool link'
}
]
}
}
```
@ -228,13 +241,25 @@ export default {
interface SocialLink {
icon: SocialLinkIcon
link: string
ariaLabel?: string
}
type SocialLinkIcon = 'discord' | 'facebook' | 'github' | 'instagram' | 'linkedin' | 'mastodon' | 'slack' | 'twitter' | 'youtube' | { svg: string }
type SocialLinkIcon =
| 'discord'
| 'facebook'
| 'github'
| 'instagram'
| 'linkedin'
| 'mastodon'
| 'slack'
| 'twitter'
| 'youtube'
| { svg: string }
```
## 页脚 {#footer}
- Key: `footer`
- Type: `Footer`
页脚配置。你可以添加 message 和 copyright。由于设计原因仅当页面不包含侧边栏时才会显示页脚。
@ -244,9 +269,9 @@ export default {
themeConfig: {
footer: {
message: 'Released under the MIT License.',
copyright: 'Copyright © 2019-present Evan You',
},
},
copyright: 'Copyright © 2019-present Evan You'
}
}
}
```
@ -257,21 +282,21 @@ export interface Footer {
}
```
## 编辑链接 {#editlink}
## 编辑链接 {#edit-link}
- Type: `EditLink`
- 每个页面可以通过 [frontmatter](./frontmatter-config#editlink) 覆写
编辑链接可让你显示链接以编辑 Git 管理服务(例如 GitHub 或 GitLab上的页面。有关详细信息请参阅 [默认主题:编辑链接](./default-theme-edit-link)。
```js
```ts
export default {
themeConfig: {
editLink: {
pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path',
text: 'Edit this page on GitHub',
},
},
text: 'Edit this page on GitHub'
}
}
}
```
@ -282,18 +307,39 @@ export interface EditLink {
}
```
## 最近更新时间文本 {#lastupdatedtext}
## 最近更新时间 {#last-updated}
- Type: `string`
- Default: `Last updated`
- key: `lastUpdated`
- Type: `LastUpdatedOptions`
显示最近更新时间之前的前缀文本
允许自定义上次更新的文本和日期格式
```ts
export default {
themeConfig: {
lastUpdatedText: 'Updated Date',
},
lastUpdated: {
text: 'Updated at',
formatOptions: {
dateStyle: 'full',
timeStyle: 'medium'
}
}
}
}
```
```ts
export interface LastUpdatedOptions {
/**
* @default 'Last updated'
*/
text?: string
/**
* @default
* { dateStyle: 'short', timeStyle: 'short' }
*/
formatOptions?: Intl.DateTimeFormatOptions & { forceLocale?: boolean }
}
```
@ -322,9 +368,9 @@ export default {
themeConfig: {
carbonAds: {
code: 'your-carbon-code',
placement: 'your-carbon-placement',
},
},
placement: 'your-carbon-placement'
}
}
}
```
@ -337,31 +383,32 @@ export interface CarbonAdsOptions {
Learn more in [Default Theme: Carbon Ads](./default-theme-carbon-ads)
## 文档页脚 {#docFooter}
## 文档页脚 {#doc-footer}
- Key: `docFooter`
- Type: `DocFooter`
可用于自定义出现在上一篇和下一篇链接上方的文本。如果不是用英语编写文档,这很有帮助。
可用于自定义出现在上一页和下一页链接上方的文本。如果不是用英语编写文档,这很有帮助。也可用于全局禁用上一页/下一页链接。如果你想有选择地启用/禁用上一个/下一个链接,可以使用 [frontmatter](./default-theme-prev-next-links)。
```js
```ts
export default {
themeConfig: {
docFooter: {
prev: 'Pagina prior',
next: 'Proxima pagina',
},
},
next: 'Proxima pagina'
}
}
}
```
```ts
export interface DocFooter {
prev?: string
next?: string
prev?: string | false
next?: string | false
}
```
## 暗模式开关标签 {#darkmodeswitchlabel}
## 暗模式开关标签 {#dark-mode-switch-label}
- key: `darkModeSwitchLabel`
- Type: `string`
@ -369,7 +416,7 @@ export interface DocFooter {
可用于自定义深色模式开关标签。此标签仅显示在移动视图中。
## 侧边栏菜单标签 {#sidebarmenulabel}
## 侧边栏菜单标签 {#sidebar-menu-label}
- key: `sidebarMenuLabel`
- Type: `string`
@ -377,7 +424,7 @@ export interface DocFooter {
可用于自定义侧边栏菜单标签。此标签仅显示在移动视图中。
## 返回顶部标签 {#returntotoplabel}
## 返回顶部标签 {#return-totop-label}
- key: `returnToTopLabel`
- Type: `string`
@ -385,10 +432,18 @@ export interface DocFooter {
可用于自定义返回顶部按钮的标签。此标签仅显示在移动视图中。
## 多语言菜单标签 {#langmenulabel}
## 多语言菜单标签 {#lang-menu-label}
- key: `langMenuLabel`
- Type: `string`
- Default: `Change language`
可用于自定义导航栏中语言切换按钮的 aria-label。这仅在你使用 [i18n](../guide/i18n) 时使用。
## 外部链接图标 {#external-link-icon}
- key: `externalLinkIcon`
- Type: `boolean`
- Default: `false`
是否在 Markdown 中的外部链接旁显示外部链接图标。

@ -1,6 +1,6 @@
# 编辑链接 {#edit-link}
## 全局配置 {#site-level-config}
## 站点级配置 {#site-level-config}
编辑链接让你可以显示一个链接,以在 GitHub 或 GitLab 等 Git 管理服务上编辑页面。要启用它,请将 `themeConfig.editLink` 选项添加到你的配置中。
@ -8,9 +8,9 @@
export default {
themeConfig: {
editLink: {
pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path',
},
},
pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path'
}
}
}
```
@ -28,9 +28,9 @@ export default {
} else {
return `https://github.com/acme/monorepo/edit/main/docs/${filePath}`
}
},
},
},
}
}
}
}
```
@ -45,9 +45,9 @@ export default {
themeConfig: {
editLink: {
pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path',
text: 'Edit this page on GitHub',
},
},
text: 'Edit this page on GitHub'
}
}
}
```

@ -7,9 +7,9 @@ export default {
themeConfig: {
footer: {
message: 'Released under the MIT License.',
copyright: 'Copyright © 2019-present Evan You',
},
},
copyright: 'Copyright © 2019-present Evan You'
}
}
}
```
@ -30,14 +30,24 @@ export default {
themeConfig: {
footer: {
message: 'Released under the <a href="https://github.com/vuejs/vitepress/blob/main/LICENSE">MIT License</a>.',
copyright: 'Copyright © 2019-present <a href="https://github.com/yyx990803">Evan You</a>',
},
},
copyright: 'Copyright © 2019-present <a href="https://github.com/yyx990803">Evan You</a>'
}
}
}
```
::: warning
Only inline elements can be used in `message` and `copyright` as they are rendered inside a `<p>` element. If you want to add block elements, consider using [`layout-bottom`](../guide/extending-default-theme#layout-slots) slot instead.
只有内联元素可以在 `message``copyright` 中使用,因为它们渲染在 `<p> ` 元素中。如果您想添加块元素,请考虑使用 [`layout-bottom`](../guide/extending-default-theme#layout-slots) 插槽。
:::
请注意,当[侧边栏](./default-theme-sidebar)可见时,不会显示页脚。
## Frontmatter 配置 {#frontmatter-config}
可以使用 frontmatter 上的 `footer` 选项在单独页面上禁用此功能:
```yaml
---
footer: false
---
```

@ -55,7 +55,10 @@ interface Hero {
actions?: HeroAction[]
}
type ThemeableImage = string | { src: string; alt?: string } | { light: string; dark: string; alt?: string }
type ThemeableImage =
| string
| { src: string; alt?: string }
| { light: string; dark: string; alt?: string }
interface HeroAction {
// Color theme of the button. Defaults to `brand`.
@ -136,6 +139,11 @@ interface Feature {
//
// e.g. `Learn more`, `Visit page`, etc.
linkText?: string
// Link rel attribute for the `link` option.
//
// e.g. `external`
rel?: string
}
type FeatureIcon =

@ -1,7 +1,10 @@
# 最后更新 {#last-updated}
# 最近更新时间 {#last-updated}
最近一条内容的更新时间会显示在页面右下角。要启用它,请将 `lastUpdated` 选项添加到你的配置中。
::: tip 提示
你必须提交 markdown 文件才能看到最近更新时间。
:::
## 全局配置 {#site-level-config}
```js
@ -20,3 +23,4 @@ lastUpdated: false
---
```
另请参阅[默认主题:最近更新时间](./default-theme-config#lastupdated) 了解更多详细信息。主题级别的任何真值也将启用该功能,除非在站点或页面级别明确禁用。

@ -16,19 +16,14 @@ layout: doc
它还提供下面列出的文档特定功能。这些功能仅在此布局中启用。
<!-- - Edit Link
- Prev Next Link
- Outline
- [Carbon Ads](./default-theme-carbon-ads) -->
- [编辑链接](./default-theme-edit-link)
- [上下页文本](./default-theme-prev-next-links)
- [上下页链接](./default-theme-prev-next-links)
- [大纲](./default-theme-config#outline)
- [Carbon Ads](./default-theme-carbon-ads)
## page 布局 {#page-layout}
`page` 被视为“空白页”。Markdown 仍然会被解析,所有的 [Markdown 展](../guide/markdown) 都和 `doc` 布局一样运行,但它没有任何默认样式。
`page` 被视为“空白页”。Markdown 仍然会被解析,所有的 [Markdown 展](../guide/markdown) 都和 `doc` 布局一样运行,但它没有任何默认样式。
`page` 布局将使你可以自行设计所有内容,而不会受 VitePress 主题影响。当你想要创建自己的自定义页面时,这很有用。
@ -62,6 +57,6 @@ export default {
extends: DefaultTheme,
enhanceApp({ app }) {
app.component('foo', Foo)
},
}
}
```

@ -9,8 +9,8 @@ Nav 是显示在页面顶部的导航栏。它包含站点标题、全局菜单
```js
export default {
themeConfig: {
siteTitle: 'My Custom Title',
},
siteTitle: 'My Custom Title'
}
}
```
@ -19,8 +19,8 @@ export default {
```js
export default {
themeConfig: {
logo: '/my-logo.svg',
},
logo: '/my-logo.svg'
}
}
```
@ -30,8 +30,8 @@ export default {
export default {
themeConfig: {
logo: '/my-logo.svg',
siteTitle: false,
},
siteTitle: false
}
}
```
@ -47,9 +47,9 @@ export default {
nav: [
{ text: 'Guide', link: '/guide' },
{ text: 'Config', link: '/config' },
{ text: 'Changelog', link: 'https://github.com/...' },
],
},
{ text: 'Changelog', link: 'https://github.com/...' }
]
}
}
```
@ -67,11 +67,11 @@ export default {
items: [
{ text: 'Item A', link: '/item-1' },
{ text: 'Item B', link: '/item-2' },
{ text: 'Item C', link: '/item-3' },
],
},
],
},
{ text: 'Item C', link: '/item-3' }
]
}
]
}
}
```
@ -92,10 +92,10 @@ export default {
text: 'Section A Title',
items: [
{ text: 'Section A Item A', link: '...' },
{ text: 'Section B Item B', link: '...' },
],
},
],
{ text: 'Section B Item B', link: '...' }
]
}
]
},
{
text: 'Dropdown Menu',
@ -104,13 +104,13 @@ export default {
// 你也可以省略标题
items: [
{ text: 'Section A Item A', link: '...' },
{ text: 'Section B Item B', link: '...' },
],
},
],
},
],
},
{ text: 'Section B Item B', link: '...' }
]
}
]
}
]
}
}
```
@ -127,10 +127,10 @@ export default {
{
text: 'Guide',
link: '/guide',
activeMatch: '/config/',
},
],
},
activeMatch: '/config/'
}
]
}
}
```
@ -150,10 +150,10 @@ export default {
text: 'Merchandise',
link: 'https://www.thegithubshop.com/',
target: '_self',
rel: 'sponsored',
},
],
},
rel: 'sponsored'
}
]
}
}
```

@ -1,15 +1,16 @@
# 上下页链接 {#prev-next-links}
你可以自定义上(下)一篇链接的文本。如果 你想在 上(下)一篇 链接上显示与侧边栏上不同的文本(默认显示侧边栏的文本),这将很有帮助。
你可以自定义上一页和下一页的文本和链接(显示在文档页脚处)。如果你想要的文本与边栏上的文本不同,这会很有帮助。此外,还可以禁用侧边栏中未包含页面的页脚或链接
你可以自定义上(下)一篇链接的文本。如果 你想在 上(下)一篇 链接上显示与侧边栏上不同的文本(默认显示侧边栏的文本),这将很有帮助。你可以自定义上一页和下一页的文本和链接(显示在文档页脚处)。如果你想要的文本与边栏上的文本不同,这会很有帮助。此外,还可以禁用侧边栏中未包含页面的页脚或链接
## prev
## 上一页 {#prev}
- Key: `prev`
- Type: `string | false | { text?: string; link?: string }`
- Details:
指定要在指向上一页的链接上显示的文本/链接。如果你没有在 `frontmatter` 中设置它,文本/链接将从侧边栏配置中推断出来。
指定要在指向上一页的链接上显示的文本/链接。如果你没有在 frontmatter 中设置它,文本/链接将从侧边栏配置中推断出来。
- 示例:
@ -39,6 +40,6 @@
---
```
## next
## 下一页 {#next}
`prev` 相同,但用于下一页。

Loading…
Cancel
Save