pull/1593/head
Xavi Lee 3 years ago
parent 62be3a3506
commit cfdd3e01bd

@ -2,18 +2,18 @@
## 在线尝试 {#try-it-online} ## 在线尝试 {#try-it-online}
You can try VitePress directly in your browser on [StackBlitz](https://vitepress.new). 可以直接在 [StackBlitz](https://vitepress.new) 上进行在线尝试。
## 安装 {#installation} ## 安装 {#installation}
### 前置知识 {#prerequisites} ### 前置知识 {#prerequisites}
- [Node.js](https://nodejs.org/) version 16 or higher. - [Node.js](https://nodejs.org/) 16 及以上版本。
- Terminal for accessing VitePress via its command line interface (CLI). - 通过命令行界面 (CLI) 访问 VitePress 的终端。
- Text Editor with [Markdown](https://en.wikipedia.org/wiki/Markdown) syntax support. - 支持 [Markdown](https://en.wikipedia.org/wiki/Markdown) 语法的编辑器。
- [VSCode](https://code.visualstudio.com/) is recommended, along with the [official Vue extension](https://marketplace.visualstudio.com/items?itemName=Vue.volar). - 推荐 [VSCode](https://code.visualstudio.com/) 及其[官方 Vue 扩展](https://marketplace.visualstudio.com/items?itemName=Vue.volar)。
VitePress can be used on its own, or be installed into an existing project. In both cases, you can install it with: VitePress 可以单独使用,也可以安装到现有项目中。在这两种情况下,都可以使用以下方式安装它:
::: code-group ::: code-group
@ -31,8 +31,8 @@ $ yarn add -D vitepress
::: :::
::: details Getting missing peer deps warnings? ::: details 遇到了 missing peer deps 警告?
If using PNPM, you will notice a missing peer warning for `@docsearch/js`. This does not prevent VitePress from working. If you wish to suppress this warning, add the following to your `package.json`: 如果使用 PNPM会注意到对 `@docsearch/js` 的 missing peer deps 警告。这不会影响 VitePress 运行。如果希望禁止显示此警告,请将以下内容添加到 `package.json`
```json ```json
"pnpm": { "pnpm": {
@ -48,7 +48,7 @@ If using PNPM, you will notice a missing peer warning for `@docsearch/js`. This
### 安装向导 {#setup-wizard} ### 安装向导 {#setup-wizard}
VitePress ships with a command line setup wizard that will help you scaffold a basic project. After installation, start the wizard by running: VitePress 附带一个命令行设置向导,可以帮助您构建一个基本项目。安装后,通过运行以下命令启动向导:
::: code-group ::: code-group
@ -62,21 +62,21 @@ $ pnpm exec vitepress init
::: :::
You will be greeted with a few simple questions: 你将需要回答几个简单的问题:
<p> <p>
<img src="./vitepress-init.png" alt="vitepress init screenshot" style="border-radius:8px"> <img src="./vitepress-init.png" alt="vitepress init screenshot" style="border-radius:8px">
</p> </p>
:::tip Vue as Peer Dependency :::tip Vue 作为
If you intend to perform customization that uses Vue components or APIs, you should also explicitly install `vue` as a peer dependency. 如果打算使用 Vue 组件或 API 进行自定义,还应该明确地将 `vue` 安装为 peer dependency。
::: :::
## 文件结构 {#file-structure} ## 文件结构 {#file-structure}
If you are building a standalone VitePress site, you can scaffold the site in your current directory (`./`). However, if you are installing VitePress in an existing project alongside other source code, it is recommended to scaffold the site in a nested directory (e.g. `./docs`) so that it is separate from the rest of the project. 如果你正在构建一个独立的 VitePress 站点,你可以在当前目录 (`./`) 中搭建站点。但是,如果在现有项目中与其他源代码一起安装 VitePress建议将站点搭建在嵌套目录 (例如 `./docs`) 中,以便它与项目的其余部分分开。
Assuming you chose to scaffold the VitePress project in `./docs`, the generated file structure should look like this: 假设你选择在 `./docs` 中搭建 VitePress 项目,生成的文件结构应该是这样的:
``` ```
. .
@ -89,15 +89,15 @@ Assuming you chose to scaffold the VitePress project in `./docs`, the generated
└─ package.json └─ package.json
``` ```
The `docs` directory is considered the **project root** of the VitePress site. The `.vitepress` directory is a reserved location for VitePress' config file, dev server cache, build output, and optional theme customization code. `docs` 目录作为 VitePress 站点的项目**根目录**。`.vitepress` 目录是 VitePress 配置文件、开发服务器缓存、构建输出和可选主题自定义代码的位置。
:::tip :::tip
By default, VitePress stores its dev server cache in `.vitepress/cache`, and the production build output in `.vitepress/dist`. If using Git, you should add them to your `.gitignore` file. These locations can also be [configured](../reference/site-config#outdir). 默认情况下VitePress 将其开发服务器缓存存储在 `.vitepress/cache` 中,并将生产构建输出存储在 `.vitepress/dist` 中。如果使用 Git应该将它们添加到 `.gitignore` 文件中。也可以手动[配置](../reference/site-config#outdir)这些位置。
::: :::
### 配置文件 {#the-config-file} ### 配置文件 {#the-config-file}
The config file (`.vitepress/config.js`) allows you to customize various aspects of your VitePress site, with the most basic options being the title and description of the site: 配置文件 (`.vitepress/config.js`) 让你能够自定义 VitePress 站点的各个方面,最基本的选项是站点的标题和描述:
```js ```js
// .vitepress/config.js // .vitepress/config.js
@ -112,19 +112,18 @@ export default {
} }
``` ```
You can also configure the behavior of the theme via the `themeConfig` option. Consult the [Config Reference](../reference/site-config) for full details on all config options. 还可以通过 `themeConfig` 选项配置主题的行为。有关所有配置选项的完整详细信息,请参见[配置参考](../reference/site-config)。
### 源文件 {#source-files} ### 源文件 {#source-files}
Markdown files outside the `.vitepress` directory are considered **source files**. `.vitepress` 目录之外的 Markdown 文件被视为**源文件**。
VitePress uses **file-based routing**: each `.md` file is compiled into a corresponding `.html` file with the same path. For example, `index.md` will be compiled into `index.html`, and can be visited at the root path `/` of the resulting VitePress site. VitePress 使用 **基于文件的路由**:每个 `.md` 文件将在相同的路径被编译成为 `.html` 文件。例如,`index.md` 将会被编译成 `index.html`,可以在生成的 VitePress 站点的根路径 `/` 进行访问。
VitePress also provides the ability to generate clean URLs, rewrite paths, and dynamically generate pages. These will be covered in the [Routing Guide](./routing). VitePress 还提供了生成简洁 URL、重写路径和动态生成页面的能力。这些将在[路由指南](./routing)中进行介绍。
## 运行 {#up-and-running} ## 运行 {#up-and-running}
The tool should have also injected the following npm scripts to your `package.json` if you allowed it to do so during the setup process: 该工具还应该将以下 npm 脚本注入到 `package.json` 中:
```json ```json
{ {
@ -138,7 +137,7 @@ The tool should have also injected the following npm scripts to your `package.js
} }
``` ```
The `docs:dev` script will start a local dev server with instant hot updates. Run it with the following command: `docs:dev` 脚本将启动具有即时热更新的本地开发服务器。使用以下命令运行它:
::: code-group ::: code-group
@ -156,7 +155,7 @@ $ yarn docs:dev
::: :::
Instead of npm scripts, you can also invoke VitePress directly with: 除了 npm 脚本,还可以直接调用 VitePress
::: code-group ::: code-group
@ -170,18 +169,18 @@ $ pnpm exec vitepress dev docs
::: :::
More command line usage is documented in the [CLI Reference](../reference/cli). 更多的命令行用法请参见 [CLI 参考](../reference/cli)。
The dev server should be running at `http://localhost:5173`. Visit the URL in your browser to see your new site in action! 开发服务应该会运行在 `http://localhost:5173`上。在浏览器中访问 URL 以查看新站点的运行情况吧!
## 下一步 {#what-s-next} ## 下一步 {#what-s-next}
- To better understand how markdown files are mapped to generated HTML, proceed to the [Routing Guide](./routing). - 想要进一步了解 Markdown 文件是怎么映射到对应的 HTML请继续阅读[路由指南](./routing)。
- To discover more about what you can do on the page, such as writing markdown content or using Vue Component, refer to the "Writing" section of the guide. A great place to start would be to learn about [Markdown Extensions](./markdown). - 要了解有关可以在页面上执行的操作的更多信息,例如编写 Markdown 内容或使用 Vue 组件,请参见指南的“编写”部分。一个很好的起点是了解 [Markdown 扩展](./markdown)。
- To explore the features provided by the default documentation theme, check out the [Default Theme Config Reference](../reference/default-theme-config). - 要探索默认文档主题提供的功能,请查看[默认主题配置参考](../reference/default-theme-config)。
- If you want to further customize the appearance of your site, explore how to either [Extend the Default Theme](./extending-default-theme) or [Build a Custom Theme](./custom-theme). - 如果想进一步自定义站点的外观,参见[扩展默认主题](./extending-default-theme)或者[构建自定义主题](./custom-theme)。
- Once your documentation site takes shape, make sure to read the [Deployment Guide](./deploy). - 文档成形以后,务必阅读[部署指南](./deploy)。

@ -1,20 +1,20 @@
# Markdown 扩展 {#markdown-extensions} # Markdown 扩展 {#markdown-extensions}
VitePress comes with built in Markdown Extensions. VitePress 带有内置的 Markdown 扩展。
## 标题锚点 {#header-anchors} ## 标题锚点 {#header-anchors}
Headers automatically get anchor links applied. Rendering of anchors can be configured using the `markdown.anchor` option. 标题会自动应用锚点。可以使用 `markdown.anchor` 选项配置锚点的渲染。
## 链接 {#links} ## 链接 {#links}
Both internal and external links gets special treatments. 内部和外部链接都会被特殊处理。
### 内部链接 {#internal-links} ### 内部链接 {#internal-links}
Internal links are converted to router link for SPA navigation. Also, every `index.md` contained in each sub-directory will automatically be converted to `index.html`, with corresponding URL `/`. 内部链接将转换为单页导航的路由链接。此外,子目录中包含的每个 `index.md` 都会自动转换为 `index.html`,并带有相应的 URL `/`
For example, given the following directory structure: 例如,给定以下目录结构:
``` ```
. .
@ -29,7 +29,7 @@ For example, given the following directory structure:
└─ four.md └─ four.md
``` ```
And providing you are in `foo/one.md`: 假设你现在处于 `foo/one.md` 文件中:
```md ```md
[Home](/) <!-- sends the user to the root index.md --> [Home](/) <!-- sends the user to the root index.md -->
@ -40,20 +40,20 @@ And providing you are in `foo/one.md`:
[bar - four](../bar/four.html) <!-- or you can append .html --> [bar - four](../bar/four.html) <!-- or you can append .html -->
``` ```
### Page Suffix {#page-suffix} ### 页面后缀 {#page-suffix}
Pages and internal links get generated with the `.html` suffix by default. 默认情况下,生成的页面和内部链接带有 `.html` 后缀。
### External Links {#external-links} ### 外部链接 {#external-links}
Outbound links automatically get `target="_blank" rel="noreferrer"`: 外部链接带有 `target="_blank" rel="noreferrer"`
- [vuejs.org](https://vuejs.org) - [vuejs.org](https://vuejs.org)
- [VitePress on GitHub](https://github.com/vuejs/vitepress) - [VitePress on GitHub](https://github.com/vuejs/vitepress)
## Frontmatter {#frontmatter} ## Frontmatter {#frontmatter}
[YAML frontmatter](https://jekyllrb.com/docs/front-matter/) is supported out of the box: [YAML 格式的 frontmatter](https://jekyllrb.com/docs/front-matter/) 开箱即用:
```yaml ```yaml
--- ---
@ -62,13 +62,13 @@ lang: en-US
--- ---
``` ```
This data will be available to the rest of the page, along with all custom and theming components. 此数据将可用于页面的其余部分,以及所有自定义和主题组件。
For more details, see [Frontmatter](../reference/frontmatter-config). 更多信息,参见 [Frontmatter](../reference/frontmatter-config)。
## GitHub-Style Tables {#github-style-tables} ## GitHub 风格的表格 {#github-style-tables}
**Input** **输入**
``` ```
| Tables | Are | Cool | | Tables | Are | Cool |
@ -78,7 +78,7 @@ For more details, see [Frontmatter](../reference/frontmatter-config).
| zebra stripes | are neat | $1 | | zebra stripes | are neat | $1 |
``` ```
**Output** **输出**
| Tables | Are | Cool | | Tables | Are | Cool |
| ------------- | :-----------: | -----: | | ------------- | :-----------: | -----: |
@ -86,41 +86,41 @@ For more details, see [Frontmatter](../reference/frontmatter-config).
| col 2 is | centered | \$12 | | col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 | | zebra stripes | are neat | \$1 |
## Emoji :tada: {#emoji-:tada:} ## Emoji :tada:
**Input** **输入**
``` ```
:tada: :100: :tada: :100:
``` ```
**Output** **输出**
:tada: :100: :tada: :100:
A [list of all emojis](https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.json) is available. 这里你可以找到[所有支持的 emoji 列表](https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.json)。
## Table of Contents {#table-of-contents} ## 目录表 (TOC) {#table-of-contents}
**Input** **输入**
``` ```
[[toc]] [[toc]]
``` ```
**Output** **输出**
[[toc]] [[toc]]
Rendering of the TOC can be configured using the `markdown.toc` option. 可以使用 `markdown.toc` 选项配置 TOC 的呈现效果。
## Custom Containers {#custom-containers} ## 自定义容器 {#custom-containers}
Custom containers can be defined by their types, titles, and contents. 自定义容器可以通过它们的类型、标题和内容来定义。
### Default Title {#default-title} ### 默认标题 {#default-title}
**Input** **输入**
```md ```md
::: info ::: info
@ -144,7 +144,7 @@ This is a details block.
::: :::
``` ```
**Output** **输出**
::: info ::: info
This is an info box. This is an info box.
@ -166,11 +166,11 @@ This is a dangerous warning.
This is a details block. This is a details block.
::: :::
### Custom Title {#custom-title} ### 自定义标题 {#custom-title}
You may set custom title by appending the text right after the "type" of the container. 可以通过在容器的“类型”之后附加文本来设置自定义标题。
**Input** **输入**
````md ````md
::: danger STOP ::: danger STOP
@ -184,7 +184,7 @@ console.log('Hello, VitePress!')
::: :::
```` ````
**Output** **输出**
::: danger STOP ::: danger STOP
Danger zone, do not proceed Danger zone, do not proceed
@ -196,10 +196,9 @@ console.log('Hello, VitePress!')
``` ```
::: :::
### `raw` {#`raw`} ### `raw`
This is a special container that can be used to prevent style and router conflicts with VitePress. This is especially useful when you're documenting component libraries. You might also wanna check out [whyframe](https://whyframe.dev/docs/integrations/vitepress) for better isolation.
这是一个特殊的容器,可以用来防止与 VitePress 的样式和路由冲突。这在记录组件库时特别有用。你可能还想查看 [whyframe](https://whyframe.dev/docs/integrations/vitepress) 以获得更好的隔离。
**Syntax** **Syntax**
```md ```md
@ -208,17 +207,17 @@ Wraps in a <div class="vp-raw">
::: :::
``` ```
`vp-raw` class can be directly used on elements too. Style isolation is currently opt-in: `vp-raw` class 也可以直接用于元素。样式隔离目前是可选的:
::: details ::: details
- Install required deps with your preferred package manager: - 使用你喜欢的包管理器来安装需要的依赖项:
```sh ```sh
$ npm install -D postcss postcss-prefix-selector $ npm install -D postcss postcss-prefix-selector
``` ```
- Create a file named `docs/.postcssrc.cjs` and add this to it: - 创建 `docs/.postcssrc.cjs` 并将以下内容
```js ```js
module.exports = { module.exports = {
@ -237,11 +236,11 @@ Wraps in a <div class="vp-raw">
::: :::
## Syntax Highlighting in Code Blocks {#syntax-highlighting-in-code-blocks} ## 代码块中的语法高亮 {#syntax-highlighting-in-code-blocks}
VitePress uses [Shiki](https://shiki.matsu.io/) to highlight language syntax in Markdown code blocks, using coloured text. Shiki supports a wide variety of programming languages. All you need to do is append a valid language alias to the beginning backticks for the code block: VitePress 使用 [Shiki](https://shiki.matsu.io/) 在 Markdown 代码块中使用彩色文本实现语法高亮。Shiki 支持多种编程语言。你需要做的就是将有效的语言别名附加到代码块的开头:
**Input** **输入**
```` ````
```js ```js
@ -262,7 +261,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js ```js
export default { export default {
@ -279,13 +278,13 @@ export default {
</ul> </ul>
``` ```
A [list of valid languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md) is available on Shiki's repository. 在 Shiki 的代码仓库中,可以找到[合法的编程语言列表](https://github.com/shikijs/shiki/blob/main/docs/languages.md)。
You may also customize syntax highlight theme in app config. Please see [`markdown` options](../reference/site-config#markdown) for more details. 还可以全局配置中自定义语法高亮主题。有关详细信息,参见 [`markdown` 选项](../reference/site-config#markdown)得到更多信息。
## Line Highlighting in Code Blocks {#line-highlighting-in-code-blocks} ## 在代码块中实现行高亮 {#line-highlighting-in-code-blocks}
**Input** **输入**
```` ````
```js{4} ```js{4}
@ -299,7 +298,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js{4} ```js{4}
export default { export default {
@ -311,13 +310,13 @@ export default {
} }
``` ```
In addition to a single line, you can also specify multiple single lines, ranges, or both: 除了单行之外,还可以指定多个单行、多行,或两者均指定:
- Line ranges: for example `{5-8}`, `{3-10}`, `{10-17}` - 多行:例如 `{5-8}`、`{3-10}`、`{10-17}`
- Multiple single lines: for example `{4,7,9}` - 多个单行:例如 `{4,7,9}`
- Line ranges and single lines: for example `{4,7-13,16,23-27,40}` - 多行与单行:例如 `{4,7-13,16,23-27,40}`
**Input** **输入**
```` ````
```js{1,4,6-8} ```js{1,4,6-8}
@ -335,7 +334,7 @@ export default { // Highlighted
``` ```
```` ````
**Output** **输出**
```js{1,4,6-8} ```js{1,4,6-8}
export default { // Highlighted export default { // Highlighted
@ -351,9 +350,9 @@ export default { // Highlighted
} }
``` ```
Alternatively, it's possible to highlight directly in the line by using the `// [!code hl]` comment. 也可以使用 `// [!code hl]` 注释实现行高亮。
**Input** **输入**
```` ````
```js ```js
@ -367,7 +366,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js ```js
export default { export default {
@ -379,15 +378,15 @@ export default {
} }
``` ```
## Focus in Code Blocks {#focus-in-code-blocks} ## 代码块中聚焦 {#focus-in-code-blocks}
Adding the `// [!code focus]` comment on a line will focus it and blur the other parts of the code. 在某一行上添加 `// [!code focus]` 注释将聚焦它并模糊代码的其他部分。
Additionally, you can define a number of lines to focus using `// [!code focus:<lines>]`. 此外,可以使用 `// [!code focus:<lines>]` 定义要聚焦的行数。
**Input** **输入**
Note that only one space is required after `!code`, here are two to prevent processing. `!code` 后面只需要一个空格,为了展示原始的代码而不被实际渲染,这里有两个空格:
```` ````
```js ```js
@ -401,7 +400,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js ```js
export default { export default {
@ -413,11 +412,11 @@ export default {
} }
``` ```
## Colored Diffs in Code Blocks {#colored-diffs-in-code-blocks} ## 代码块中的颜色差异 {#colored-diffs-in-code-blocks}
Adding the `// [!code --]` or `// [!code ++]` comments on a line will create a diff of that line, while keeping the colors of the codeblock. Adding the `// [!code --]` or `// [!code ++]` comments on a line will create a diff of that line, while keeping the colors of the codeblock.
**Input** **输入**
Note that only one space is required after `!code`, here are two to prevent processing. Note that only one space is required after `!code`, here are two to prevent processing.
@ -434,7 +433,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js ```js
export default { export default {
@ -451,7 +450,7 @@ export default {
Adding the `// [!code warning]` or `// [!code error]` comments on a line will color it accordingly. Adding the `// [!code warning]` or `// [!code error]` comments on a line will color it accordingly.
**Input** **输入**
Note that only one space is required after `!code`, here are two to prevent processing. Note that only one space is required after `!code`, here are two to prevent processing.
@ -468,7 +467,7 @@ export default {
``` ```
```` ````
**Output** **输出**
```js ```js
export default { export default {
@ -497,7 +496,7 @@ Please see [`markdown` options](../reference/site-config#markdown) for more deta
You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config. You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config.
**Input** **输入**
````md ````md
```ts {1} ```ts {1}
@ -513,7 +512,7 @@ const line3 = 'This is line 3'
``` ```
```` ````
**Output** **输出**
```ts {1} ```ts {1}
// line-numbers is disabled by default // line-numbers is disabled by default
@ -541,7 +540,7 @@ It also supports [line highlighting](#line-highlighting-in-code-blocks):
<<< @/filepath{highlightLines} <<< @/filepath{highlightLines}
``` ```
**Input** **输入**
```md ```md
<<< @/snippets/snippet.js{2} <<< @/snippets/snippet.js{2}
@ -551,7 +550,7 @@ It also supports [line highlighting](#line-highlighting-in-code-blocks):
<<< @/snippets/snippet.js <<< @/snippets/snippet.js
**Output** **输出**
<<< @/snippets/snippet.js{2} <<< @/snippets/snippet.js{2}
@ -561,7 +560,7 @@ The value of `@` corresponds to the source root. By default it's the VitePress p
You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding) to only include the corresponding part of the code file. You can provide a custom region name after a `#` following the filepath: You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding) to only include the corresponding part of the code file. You can provide a custom region name after a `#` following the filepath:
**Input** **输入**
```md ```md
<<< @/snippets/snippet-with-region.js#snippet{1} <<< @/snippets/snippet-with-region.js#snippet{1}
@ -571,7 +570,7 @@ You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/co
<<< @/snippets/snippet-with-region.js <<< @/snippets/snippet-with-region.js
**Output** **输出**
<<< @/snippets/snippet-with-region.js#snippet{1} <<< @/snippets/snippet-with-region.js#snippet{1}
@ -595,7 +594,7 @@ This is helpful if source language cannot be inferred from your file extension.
You can group multiple code blocks like this: You can group multiple code blocks like this:
**Input** **输入**
````md ````md
::: code-group ::: code-group
@ -624,7 +623,7 @@ export default config
::: :::
```` ````
**Output** **输出**
::: code-group ::: code-group
@ -653,7 +652,7 @@ export default config
You can also [import snippets](#import-code-snippets) in code groups: You can also [import snippets](#import-code-snippets) in code groups:
**Input** **输入**
```md ```md
::: code-group ::: code-group
@ -669,7 +668,7 @@ You can also [import snippets](#import-code-snippets) in code groups:
::: :::
``` ```
**Output** **输出**
::: code-group ::: code-group
@ -683,7 +682,7 @@ You can also [import snippets](#import-code-snippets) in code groups:
You can include a markdown file in another markdown file like this: You can include a markdown file in another markdown file like this:
**Input** **输入**
```md ```md
# Docs {#docs} # Docs {#docs}

@ -247,7 +247,7 @@ export default {
} }
``` ```
**Output** **输出**
``` ```
. .

@ -1,46 +1,46 @@
# 在 Markdown 使用 Vue {#using-vue-in-markdown} # 在 Markdown 使用 Vue {#using-vue-in-markdown}
In VitePress, each Markdown file is compiled into HTML and then processed as a [Vue Single-File Component](https://vuejs.org/guide/scaling-up/sfc.html). This means you can use any Vue features inside the Markdown, including dynamic templating, using Vue components, or arbitrary in-page Vue component logic by adding a `<script>` tag. 在 VitePress 中,每个 Markdown 文件都被编译成 HTML而且将其作为 [Vue 单文件组件](https://cn.vuejs.org/guide/scaling-up/sfc.html)处理。这意味着可以在 Markdown 中使用任何 Vue 功能,包括动态模板、使用 Vue 组件或通过添加 `<script>` 标签为页面的 Vue 组件添加逻辑。
It's worth noting that VitePress leverages Vue's compiler to automatically detect and optimize the purely static parts of the Markdown content. Static contents are optimized into single placeholder nodes and eliminated from the page's JavaScript payload for initial visits. They are also skipped during client-side hydration. In short, you only pay for the dynamic parts on any given page. 值得注意的是VitePress 利用 Vue 的编译器自动检测和优化 Markdown 内容的纯静态部分。静态内容被优化为单个占位符节点,并从页面的 JavaScript 负载中删除以供初始访问。在客户端激活期间也会跳过它们。简而言之,你只需注意任何给定页面上的动态部分。
:::tip SSR Compatibility :::tip SSR 兼容性
All Vue usage needs to be SSR-compatible. See [SSR Compatibility](./ssr-compat) for details and common workarounds. 所有的 Vue 用法都需要兼容 SSR。参见 [SSR 兼容性](./ssr-compat)获得更多信息和常见的解决方案。
::: :::
## 模板化 {#templating} ## 模板化 {#templating}
### 插值语法 {#interpolation} ### 插值语法 {#interpolation}
Each Markdown file is first compiled into HTML and then passed on as a Vue component to the Vite process pipeline. This means you can use Vue-style interpolation in text: 每个 Markdown 文件首先被编译成 HTML然后作为 Vue 组件传递给 Vite 流程管道。这意味着可以在文本中使用 Vue 的插值语法:
**Input** **输入**
```md ```md
{{ 1 + 1 }} {{ 1 + 1 }}
``` ```
**Output** **输出**
<div class="language-text"><pre><code>{{ 1 + 1 }}</code></pre></div> <div class="language-text"><pre><code>{{ 1 + 1 }}</code></pre></div>
### Directives {#directives} ### 指令 {#directives}
Directives also work (note that by design, raw HTML is also valid in Markdown): 也可以使用指令 (请注意,根据设计,原始 HTML 在 Markdown 中也有效):
**Input** **输入**
```html ```html
<span v-for="i in 3">{{ i }}</span> <span v-for="i in 3">{{ i }}</span>
``` ```
**Output** **输出**
<div class="language-text"><pre><code><span v-for="i in 3">{{ i }} </span></code></pre></div> <div class="language-text"><pre><code><span v-for="i in 3">{{ i }} </span></code></pre></div>
## `<script>` and `<style>` {#`<script>`-and-`<style>`} ## `<script>` `<style>` {#script-and-style}
Root-level `<script>` and `<style>` tags in Markdown files work just like they do in Vue SFCs, including `<script setup>`, `<style module>`, etc. The main difference here is that there is no `<template>` tag: all other root-level content is Markdown. Also note that all tags should be placed **after** the frontmatter: Markdown 文件中的根级 `<script>``<style>` 标签与 Vue SFC 中的一样,包括 `<script setup>`、`<style module>` 等。这里的主要区别是没有 `<template>` 标签:所有其他根级内容都是 Markdown。另请注意所有标签都应放在 frontmatter **之后**
```html ```html
--- ---
@ -67,13 +67,13 @@ The count is: {{ count }}
</style> </style>
``` ```
:::warning Avoid `<style scoped>` in Markdown :::warning 避免在 Markdown 中使用 `<style scoped>`
When used in Markdown, `<style scoped>` requires adding special attributes to every element on the current page, which will significantly bloat the page size. `<style module>` is preferred when locally-scoped styling is needed in a page. 在 Markdown 中使用时,`<style scoped>` 需要为当前页面的每个元素添加特殊属性,这将显著增加页面的大小。当我们需要局部范围的样式时 `<style module>` 是首选。
::: :::
You also have access to VitePress' runtime APIs such as the [`useData` helper](../reference/runtime-api#usedata), which provides access to current page's metadata: 你还可以访问 VitePress 的运行时 API例如 [`useData` 辅助函数](../reference/runtime-api#usedata),它提供了当前页面的元数据:
**Input** **输入**
```html ```html
<script setup> <script setup>
@ -85,7 +85,7 @@ const { page } = useData()
<pre>{{ page }}</pre> <pre>{{ page }}</pre>
``` ```
**Output** **输出**
```json ```json
{ {
@ -98,11 +98,11 @@ const { page } = useData()
## 使用组件 {#using-components} ## 使用组件 {#using-components}
You can import and use Vue components directly in Markdown files. 你可以直接在 Markdown 文件中导入和使用 Vue 组件。
### 在 Markdown 中导入组件 {#importing-in-markdown} ### 在 Markdown 中导入组件 {#importing-in-markdown}
If a component is only used by a few pages, it's recommended to explicitly import them where they are used. This allows them to be properly code-split and only loaded when the relevant pages are shown: 如果一个组件只被几个页面使用,建议在使用它们的地方显式导入它们。这使它们可以正确地进行代码拆分,并且仅在显示相关页面时才加载:
```md ```md
<script setup> <script setup>
@ -122,45 +122,45 @@ This is a .md using a custom component
### 注册全局组件 {#registering-components-globally} ### 注册全局组件 {#registering-components-globally}
If a component is going to be used on most of the pages, they can be registered globally by customizing the Vue app instance. See relevant section in [Extending Default Theme](./extending-default-theme#registering-global-components) for an example. 如果一个组件要在大多数页面上使用,可以通过自定义 Vue 实例来全局注册它们。有关示例,请参见[扩展默认主题](./extending-default-theme#registering-global-components)中的相关部分。
::: warning IMPORTANT ::: warning 重要
Make sure a custom component's name either contains a hyphen or is in PascalCase. Otherwise, it will be treated as an inline element and wrapped inside a `<p>` tag, which will lead to hydration mismatch because `<p>` does not allow block elements to be placed inside it. 确保自定义组件的名称包含连字符或采用 PascalCase。否则它将被视为内联元素并包裹在 `<p>` 标签内,这将导致激活不匹配,因为 `<p>` 不允许将块元素放置在其中。
::: :::
### 在标题中使用组件 <ComponentInHeader /> {#using-components-in-headers} ### 在标题中使用组件 <ComponentInHeader /> {#using-components-in-headers}
You can use Vue components in the headers, but note the difference between the following syntaxes: 可以在标题中使用 Vue 组件,但请注意以下语法之间的区别:
| Markdown | Output HTML | Parsed Header | | Markdown | 输出的 HTML | 被解析的标题 |
| ------------------------------------------------------- | ----------------------------------------- | ------------- | | ------------------------------------------------------- | ----------------------------------------- | ------------- |
| <pre v-pre><code> # text &lt;Tag/&gt; </code></pre> | `<h1>text <Tag/></h1>` | `text` | | <pre v-pre><code> # text &lt;Tag/&gt; </code></pre> | `<h1>text <Tag/></h1>` | `text` |
| <pre v-pre><code> # text \`&lt;Tag/&gt;\` </code></pre> | `<h1>text <code>&lt;Tag/&gt;</code></h1>` | `text <Tag/>` | | <pre v-pre><code> # text \`&lt;Tag/&gt;\` </code></pre> | `<h1>text <code>&lt;Tag/&gt;</code></h1>` | `text <Tag/>` |
The HTML wrapped by `<code>` will be displayed as-is; only the HTML that is **not** wrapped will be parsed by Vue. `<code>` 包裹的 HTML 将按原样显示,只有未包裹的 HTML 才会被 Vue 解析。
::: tip ::: tip
The output HTML is accomplished by [Markdown-it](https://github.com/Markdown-it/Markdown-it), while the parsed headers are handled by VitePress (and used for both the sidebar and document title). 输出 HTML 由 [Markdown-it](https://github.com/Markdown-it/Markdown-it) 完成,而解析的标题由 VitePress 处理 (并用于侧边栏和文档标题)。
::: :::
## 转义 {#escaping} ## 转义 {#escaping}
You can escape Vue interpolations by wrapping them in a `<span>` or other elements with the `v-pre` directive: 可以通过使用 `v-pre` 指令将它们包裹在 `<span>` 或其他元素中来转义 Vue 插值:
**Input** **输入**
```md ```md
This <span v-pre>{{ will be displayed as-is }}</span> This <span v-pre>{{ will be displayed as-is }}</span>
``` ```
**Output** **输出**
<div class="escape-demo"> <div class="escape-demo">
<p>This <span v-pre>{{ will be displayed as-is }}</span></p> <p>This <span v-pre>{{ will be displayed as-is }}</span></p>
</div> </div>
Alternatively, you can wrap the entire paragraph in a `v-pre` custom container: 也可以将整个段落包装在 `v-pre` 自定义容器中:
```md ```md
::: v-pre ::: v-pre
@ -168,7 +168,7 @@ Alternatively, you can wrap the entire paragraph in a `v-pre` custom container:
::: :::
``` ```
**Output** **输出**
<div class="escape-demo"> <div class="escape-demo">
@ -180,9 +180,9 @@ Alternatively, you can wrap the entire paragraph in a `v-pre` custom container:
## 代码块中不转义 {#unescape-in-code-blocks} ## 代码块中不转义 {#unescape-in-code-blocks}
By default, all fenced code blocks are automatically wrapped with `v-pre`, so no Vue syntax will be processd inside. To enable Vue-style interpolation inside fences, you can append the language with the `-vue` suffix, e.g. `js-vue`: 默认情况下,代码块是受到保护的,都会自动使用 `v-pre` 包装,因此内部不会处理任何 Vue 语法。要在代码块内启用 Vue 插值语法,可以在代码语言后附加 `-vue` 后缀,例如 `js-vue`
**Input** **输入**
````md ````md
```js-vue ```js-vue
@ -190,7 +190,7 @@ Hello {{ 1 + 1 }}
``` ```
```` ````
**Output** **输出**
```js-vue ```js-vue
Hello {{ 1 + 1 }} Hello {{ 1 + 1 }}
@ -198,20 +198,20 @@ Hello {{ 1 + 1 }}
## 使用 CSS 预处理器 {#using-css-pre-processors} ## 使用 CSS 预处理器 {#using-css-pre-processors}
VitePress has [built-in support](https://vitejs.dev/guide/features.html#css-pre-processors) for CSS pre-processors: `.scss`, `.sass`, `.less`, `.styl` and `.stylus` files. There is no need to install Vite-specific plugins for them, but the corresponding pre-processor itself must be installed: VitePress [内置支持](https://cn.vitejs.dev/guide/features.html#css-pre-processors) CSS 预处理器:`.scss`、`.sass`、.`less`、`.styl` 和 `.stylus` 文件。无需为它们安装 Vite 专用插件,但必须安装相应的预处理器:
``` ```
# .scss and .sass {#.scss-and-.sass} # .scss and .sass
npm install -D sass npm install -D sass
# .less {#.less} # .less
npm install -D less npm install -D less
# .styl and .stylus {#.styl-and-.stylus} # .styl and .stylus
npm install -D stylus npm install -D stylus
``` ```
Then you can use the following in Markdown and theme components: 然后你可以在 Markdown 和主题组件中使用以下内容:
```vue ```vue
<style lang="sass"> <style lang="sass">
@ -220,9 +220,9 @@ Then you can use the following in Markdown and theme components:
</style> </style>
``` ```
## 传递组件内容 {#using-teleports} ## 使用 teleport 传递组件内容 {#using-teleports}
Vitepress currently has SSG support for teleports to body only. For other targets, you can wrap them inside the built-in `<ClientOnly>` component or inject the teleport markup into the correct location in your final page HTML through [`postRender` hook](../reference/site-config#postrender). Vitepress 目前只有使用 teleport 传送到 body 的 SSG 支持。对于其他地方,可以将它们包裹在内置的 `<ClientOnly>` 组件中,或者通过 [postRender 钩子](../reference/site-config#postrender)将 teleport 标签注入到最终页面 HTML 中的正确位置。
<ModalDemo /> <ModalDemo />

@ -1,57 +1,57 @@
# VitePress 是什么? {#what-is-vitepress} # VitePress 是什么? {#what-is-vitepress}
VitePress is a [Static Site Generator](https://en.wikipedia.org/wiki/Static_site_generator) (SSG) designed for building fast, content-centric websites. In a nutshell, VitePress takes your source content written in [Markdown](https://en.wikipedia.org/wiki/Markdown), applies a theme to it, and generates static HTML pages that can be easily deployed anywhere. VitePress 是一个[静态站点生成器](https://en.wikipedia.org/wiki/Static_site_generator) (SSG)专为构建快速、以内容为中心的网站而设计。简而言之VitePress 获取用 Markdown 编写的源内容,对其应用主题,并生成可以轻松部署到任何地方的静态 HTML 页面。
<div class="tip custom-block" style="padding-top: 8px"> <div class="tip custom-block" style="padding-top: 8px">
Just want to try it out? Skip to the [Quickstart](./getting-started). 只是想尝试一下?跳到[快速开始](./getting-started)。
</div> </div>
## 使用场景 {#use-cases} ## 使用场景 {#use-cases}
- **Documentation** - **文档**
VitePress ships with a default theme designed for technical documentation, especially those that need to embed interactive demos. It powers this page you are reading right now, along with the documentation for [Vite](https://vitejs.dev/), [Pinia](https://pinia.vuejs.org/), [VueUse](https://vueuse.org/), [Mermaid](https://mermaid.js.org/), [Wikimedia Codex](https://doc.wikimedia.org/codex/latest/), and many more. VitePress 附带一个专为技术文档设计的默认主题,尤其是那些需要嵌入交互式演示的主题。它支持你正在阅读的这个页面,以及 [Vite](https://vitejs.dev/)、[Pinia](https://pinia.vuejs.org/)、[VueUse](https://vueuse.org/)、[Mermaid](https://mermaid.js.org/)、[Wikimedia Codex](https://doc.wikimedia.org/codex/latest/) 等文档。
The [official Vue.js documentation](https://vuejs.org/) is also based on VitePress, but uses a custom theme shared between multiple translations. [Vue.js 官方文档](https://vuejs.org/) 也是基于 VitePress 的。但是为了可以在不同的翻译文档之间共享,它自定义了自己的主题
- **Blogs, Portfolios, and Marketing Sites** - **博客、档案和营销网站**
VitePress supports [fully customized themes](./custom-theme), with the developer experience of a standard Vite + Vue application. Being built on Vite also means you can directly leverage Vite plugins from its rich ecosystem. In addition, VitePress provides flexible APIs to [load data](./data-loading) (local or remote) and [dynamically generate routes](./routing#dynamic-routes). You can use it to build almost anything as long as the data can be determined at build time. VitePress 支持[完全的自定义主题](./custom-theme),具有标准 Vite + Vue 应用程序的开发体验。基于 Vite 构建还意味着可以直接利用其丰富生态系统中的 Vite 插件。此外VitePress 提供了灵活的 API 来[加载数据](./data-loading) (本地或远程),也可以[动态生成路由](./routing#dynamic-routes)。只要可以在构建时确定数据,就可以使用它来构建几乎任何东西。
The official [Vue.js blog](https://blog.vuejs.org/) is a simple blog that generates its index page based on local content. [Vue.js 官方博客](https://blog.vuejs.org/) 是一个简单的博客页面,它根据本地内容生成其索引页面。
## 开发体验 {#developer-experience} ## 开发体验 {#developer-experience}
VitePress aims to provide a great Developer Experience (DX) when working with Markdown content. VitePress 旨在使用 Markdown 生成内容时提供出色的开发体验。
- **[Vite-Powered:](https://vitejs.dev/)** instant server start, with edits always instantly reflected (<100ms) without page reload. - **[Vite 驱动:](https://cn.vitejs.dev/)**即时服务器启动,始终立即反映(<100ms)编辑变化,无需重新加载页面。
- **[Built-in Markdown Extensions:](./markdown)** Frontmatter, tables, syntax highlighting... you name it. Specifically, VitePress provides many advanced features for working with code blocks, making it ideal for highly technical documentation. - **[内置 Markdown 扩展:](./markdown)** FrontmatterFrontmatter、表格、语法高亮……应有尽有。具体来说VitePress 提供了许多用于处理代码块的高级功能,使其真正成为技术文档的理想选择。
- **[Vue-Enhanced Markdown](./using-vue):** each Markdown page is also a Vue [Single-File Component](https://vuejs.org/guide/scaling-up/sfc.html), thanks to Vue template's 100% syntax compatibility with HTML. You can embed interactivity in your static content using Vue templating features or imported Vue components. - **[Vue 增强的 Markdown](./using-vue)** 每个 Markdown 页面都是 Vue [单文件组件](https://cn.vuejs.org/guide/scaling-up/sfc.html),这要归功于 Vue 模板与 HTML 的 100% 语法兼容性。可以使用 Vue 模板语法或导入的 Vue 组件在静态内容中嵌入交互性。
## 性能 {#performance} ## 性能 {#performance}
Unlike many traditional SSGs, a website generated by VitePress is in fact a [Single Page Application](https://en.wikipedia.org/wiki/Single-page_application) (SPA). 与许多传统的 SSG 不同VitePress 生成的网站实际上是一个[单页应用程序](https://en.wikipedia.org/wiki/Single-page_application) (SPA)。
- **Fast Initial Load** - **快速初始加载**
The initial visit to any page will be served the static, pre-rendered HTML for blazing fast loading speed and optimal SEO. The page then loads a JavaScript bundle that turns the page into a Vue SPA ("hydration"). The hydration process is extremely fast: on [PageSpeed Insights](https://pagespeed.web.dev/report?url=https%3A%2F%2Fvitepress.dev%2F), typical VitePress sites achieve near-perfect performance scores even on low-end mobile devices with a slow network. 对任何页面的初次访问都将提供静态的、预呈现的 HTML以实现极快的加载速度和最佳的 SEO。然后页面加载一个 JavaScript 包,将页面变成 Vue SPA (这被称为“激活”)。激活是非常快的:在 [PageSpeed Insights](https://pagespeed.web.dev/report?url=https%3A%2F%2Fvitepress.dev%2F) 上,典型的 VitePress 站点即使在网络速度较慢的低端移动设备上也能获得近乎完美的性能分数。
- **Fast Post-load Navigation** - **加载完成后可以快速切换**
More importantly, the SPA model leads to better user experience **after** the initial load. Subsequent navigation within the site will no longer cause a full page reload. Instead, the incoming page's content will be fetched and dynamically updated. VitePress also automatically pre-fetches page chunks for links that are within viewport. In most cases, post-load navigation will feel instant. 更重要的是SPA 模型在首次加载后能够提升用户体验。用户在网站内导航时不会再触发整个页面的刷新。而是通过获取并动态更新新页面的内容来实现切换。VitePress还会自动预加载视口范围内链接对应的页面片段。这样一来大部分情况下用户在加载完成后就能立即浏览新页面。
- **Interactivity Without Penalty** - **高效的交互**
To be able to hydrate the dynamic Vue parts embedded inside static Markdown, each Markdown page is processed as a Vue component and compiled into JavaScript. This may sound inefficient, but the Vue compiler is smart enough to separate the static and dynamic parts, minimizing both the hydration cost and payload size. For the initial page load, the static parts are automatically eliminated from the JavaScript payload and skipped during hydration. 为了能够嵌入静态 Markdown 中的动态 Vue 部分,每个 Markdown 页面都被处理为 Vue 组件并编译成 JavaScript。这听起来可能效率低下但 Vue 编译器足够聪明,可以将静态和动态部分分开,从而最大限度地减少激活成本和有效负载大小。对于初始页面加载,静态部分会自动从 JavaScript 有效负载中删除,并在激活期间跳过。
## VuePress 怎么样 {#what-about-vuepress} ## VuePress 又是什么 {#what-about-vuepress}
VitePress is the spiritual successor of VuePress. The original VuePress was based on Vue 2 and webpack. With Vue 3 and Vite under the hood, VitePress provides significantly better DX, better production performance, a more polished default theme, and a more flexible customization API. VitePress 灵感来源于 VuePress。最初的 VuePress 基于 Vue 2 和 webpack。借助 Vue 3 和 ViteVitePress 提供了更好的开发体验、更好的生产性能、更精美的默认主题和更灵活的自定义 API。
The API difference between VitePress and VuePress mostly lies in theming and customization. If you are using VuePress 1 with the default theme, it should be relatively straightforward to migrate to VitePress. VitePress 和 VuePress 之间的 API 区别主要在于主题和定制。如果您使用的是带有默认主题的 VuePress 1迁移到 VitePress 应该相对简单。
There has also been effort invested into VuePress 2, which also supports Vue 3 and Vite with more compatibility with VuePress 1. However, maintaining two SSGs in parallel isn't sustainable, so the Vue team has decided to focus on VitePress as the main recommended SSG in the long run. VuePress 2 也投入了精力,它也支持 Vue 3 和 Vite与 VuePress 1 的兼容性更好。但是,并行维护两个 SSG 是难以持续的,因此 Vue 团队决定将重点放在 VitePress作为长期的主要 SSG 选择推荐。

Loading…
Cancel
Save