docs:translate to Chinese

pull/426/head
Sepush 4 years ago
parent 773472d867
commit 051c160e2c

@ -0,0 +1,96 @@
# API 参考
## 辅助方法
下面的方法可以全局导入 `vitepress`,通常用于自定义主题的 Vue 组件。但是,也可以在 `.md` 页面中使用,因为 Markdown 文件被编译为 Vue 单文件组件。
方法以 `use*` 开头表示它是一个 [Vue 3 Composition API](https://v3.vuejs.org/guide/composition-api-introduction.html) 函数,只能在 `setup()``<script setup>` 中使用。
### `useData`
返回页面特定的数据。返回的对象具有以下类型:
```ts
interface VitePressData {
site: Ref<SiteData>
page: Ref<PageData>
theme: Ref<any> // themeConfig from .vitepress/config.js
frontmatter: Ref<PageData['frontmatter']>
title: Ref<string>
description: Ref<string>
lang: Ref<string>
localePath: Ref<string>
}
```
**例子:**
```vue
<script setup>
import { useData } from 'vitepress'
const { theme } = useData()
</script>
<template>
<h1>{{ theme.heroText }}</h1>
</template>
```
### `useRoute`
返回当前路由对象,具有以下类型:
```ts
interface Route {
path: string
data: PageData
component: Component | null
}
```
### `useRouter`
返回 VitePress 路由实例,以便您可以编程地导航到另一个页面。
```ts
interface Router {
route: Route
go: (href?: string) => Promise<void>
}
```
### `withBase`
- **类型**: `(path: string) => string`
将配置的 [`base`](/config/basics.html#base) 附加到给定的 URL 路径。同时参见 [Base URL](/guide/assets.html#base-url)。
## Global Components 全局组件
VitePress 包含一些内置组件,可以用于全局使用。您可以在 Markdown 或自定义主题配置中使用这些组件。
### `<Content/>`
`<Content/>` 组件显示渲染的 Markdown 内容。当创建自己的主题时有用。
```vue
<template>
<h1>Custom Layout!</h1>
<Content />
</template>
```
### `<ClientOnly/>`
`<ClientOnly/>` 组件只在客户端渲染它的插槽。
因为 VitePress 的应用程序在生成静态构建时在 Node.js 中被服务端渲染,所以任何 Vue 的使用必须遵守通用代码要求。简单地说,在 beforeMount 或 mounted 钩子中只访问 Browser / DOM 的 API。
如果您使用或演示不是 SSR 友好的组件(例如包含自定义指令),您可以在 `ClientOnly` 组件之间包装它们。
```html
<ClientOnly>
<NonSSRFriendlyComponent />
</ClientOnly>
```

@ -0,0 +1,54 @@
# 资源管理
所有的 Markdown 文件都会编译成 Vue 组件,并且会被 [Vite](https://github.com/vitejs/vite)。你可以,**并且应该**,使用相对路径来引用资源:
```md
![An image](./image.png)
```
你可以在你的 Markdown 文件中引用静态资源,你的 `*.vue` 组件,样式和纯 CSS 文件,可以使用绝对的公共路径(基于项目根目录)或者相对的路径(基于你的文件系统)。这种方式与 `vue-cli` 或者 webpack 的 `file-loader` 的行为相似。
普通的图片、媒体和字体文件类型会被自动检测并包含为资源。
所有引用的资源,包括使用绝对路径的资源,都会被复制到 dist 文件夹,并且在生产构建中使用哈希文件名。不引用的资源不会被复制。与 `vue-cli` 一样,小于 4kb 的图片资源会被 base64 内联。
所有的 **静态** 路径引用,包括绝对路径,应该基于你的工作目录结构。
## 公共文件
有时候你可能需要提供一些不直接在 Markdown 文件或者主题组件例如favicon 和 PWA 图标)中直接引用。项目根目录下的 `public` 目录可以用作脱离式资源,可以提供不在源代码中引用的静态资源(例如,`robots.txt`),或者保持文件名不变(不使用哈希)。
`public` 目录中的资源会被复制到 dist 目录的根目录中。
请注意,你应该在 `public` 目录中放置的文件使用根绝对路径引用 - 例如,`public/icon.png` 应该在源代码中总是使用 `/icon.png`
## Base URL
如果你的站点被部署到一个非根 URL你需要在 `.vitepress/config.js` 中设置 `base` 选项。例如,如果你计划部署站点到 `https://foo.github.io/bar/`,那么 `base` 应该设置为 `'/bar/'`(它应该总是以斜杠开头和结尾)。
你的所有静态资源路径都会被自动处理,以调整不同的 `base` 配置值。例如,如果你在 Markdown 中有一个绝对路径引用了 `public` 目录下的静态资源:
```md
![An image](/image-inside-public.png)
```
**不** 需要在这种情况下更改 `base` 选项。
但是,如果你正在作为主题组件引用静态资源,例如,图片的 `src` 属性基于主题配置值:
```vue
<img :src="theme.logoPath" />
```
在这种情况下,建议使用 VitePress 提供的 [`withBase` 助手](/guide/api.html#withbase)
```vue
<script setup>
import { withBase, useData } from 'vitepress'
const { theme } = useData()
</script>
<template>
<img :src="withBase(theme.logoPath)" />
</template>
```

@ -0,0 +1,22 @@
# Configuration配置
不需要任何配置,页面只有一个标题,用户无法导航到站点。要自定义站点,首先创建一个`.vitepress`目录在你的文档目录中。这里是放所有VitePress-specific文件的位置。你的项目结构可能像这样
```bash
.
├─ docs
│ ├─ .vitepress
│ │ └─ config.js
│ └─ index.md
└─ package.json
```
配置VitePress站点的最重要的文件是`.vitepress/config.js`它应该导出一个JavaScript对象
```js
module.exports = {
title: 'Hello VitePress',
description: 'Just playing around.'
}
```
查看[配置参考](/config/basics)以获取完整的选项列表。

@ -0,0 +1,267 @@
---
sidebarDepth: 3
---
# 部署
下面的指南基于一些共享假设:
- 你正在将你的文档放置在你的项目的`docs`目录中;
- 你正在使用默认的构建输出位置(`.vitepress/dist`);
- VitePress作为一个本地依赖安装在你的项目中并且你已经设置了以下npm脚本:
```json
{
"scripts": {
"docs:build": "vitepress build docs",
"docs:serve": "vitepress serve docs"
}
}
```
## 构建文档
你可以运行`yarn docs:build`命令来构建文档。
```bash
$ yarn docs:build
```
默认情况下,构建输出将被放置在`.vitepress/dist`。你可以将这个`dist`文件夹部署到任何你喜欢的平台。
### 在本地测试文档
一旦你构建了文档,你可以通过运行`yarn docs:serve`命令来在本地测试它们。
```bash
$ yarn docs:build
$ yarn docs:serve
```
`serve`命令将启动本地静态web服务器它将从`.vitepress/dist`目录中提供文件。它是一个简单的方式来检查你的本地环境中的生产构建是否正常。
你可以通过传递`--port`标志作为参数来配置服务器的端口。
```json
{
"scripts": {
"docs:serve": "vitepress serve docs --port 8080"
}
}
```
现在,`docs:serve`方法将在http://localhost:8080启动服务器。
## GitHub Pages
1. 设置正确的`base`在`docs/.vitepress/config.js`。
如果你正在部署到`https://<USERNAME>.github.io/`,你可以忽略`base`,它默认为`'/'`。
如果你正在部署到`https://<USERNAME>.github.io/<REPO>/`,例如你的仓库在`https://github.com/<USERNAME>/<REPO>`,那么设置`base`为`'/<REPO>/'`。
2. 在你的项目中,创建`deploy.sh`,并且使用以下内容(突出显示的几行未作适当的注释),然后运行它来部署:
```bash{13,20,23}
#!/usr/bin/env sh
# abort on errors
set -e
# build
npm run docs:build
# navigate into the build output directory
cd docs/.vitepress/dist
# if you are deploying to a custom domain
# echo 'www.example.com' > CNAME
git init
git add -A
git commit -m 'deploy'
# if you are deploying to https://<USERNAME>.github.io
# git push -f git@github.com:<USERNAME>/<USERNAME>.github.io.git master
# if you are deploying to https://<USERNAME>.github.io/<REPO>
# git push -f git@github.com:<USERNAME>/<REPO>.git master:gh-pages
cd -
```
::: Tip
你也可以在你的CI设置中运行上面的脚本来启用每次推送的自动部署。
:::
### GitHub Pages and Travis CI
1. 在`docs/.vitepress/config.js`设置正确的`base`。
如果你正在部署到`https://<USERNAME or GROUP>.github.io/`,你可以忽略`base`,它默认为`'/'`。
如果你正在部署到`https://<USERNAME or GROUP>.github.io/<REPO>/`,例如你的仓库在`https://github.com/<USERNAME>/<REPO>`,然后设置`base`为`'/<REPO>/'`。
2. 在项目的根目录中创建一个名为`travis.yml`的文件。
3. 在本地运行`yarn`或`npm install`,并且提交生成的锁文件(这是`yarn.lock`或`package-lock.json`)。
4. 使用GitHub Pages部署提供者模板并且根据[Travis CI文档](https://docs.travis-ci.com/user/deployment/pages/)。
```yaml
language: node_js
node_js:
- lts/*
install:
- yarn install # npm ci
script:
- yarn docs:build # npm run docs:build
deploy:
provider: pages
skip_cleanup: true
local_dir: docs/.vitepress/dist
# A token generated on GitHub allowing Travis to push code on you repository.
# Set in the Travis settings page of your repository, as a secure variable.
github_token: $GITHUB_TOKEN
keep_history: true
on:
branch: master
```
## GitLab Pages and GitLab CI
1. 在`docs/.vitepress/config.js`中设置正确的`base`。
如果你正在部署到`https://<USERNAME or GROUP>.gitlab.io/`,你可以忽略`base`,它默认为`'/'`。
如果你正在部署到`https://<USERNAME or GROUP>.gitlab.io/<REPO>/`,例如你的仓库在`https://gitlab.com/<USERNAME>/<REPO>`,那么设置`base`为`'/<REPO>/'`。
2. 在`docs/.vitepress/config.js`为`public`中设置`dest`。
3. 在项目的根目录中创建一个名为`gitlab-ci.yml`的文件,并且使用以下内容。这将在你对内容的更改时自动构建并部署你的网站:
```yaml
image: node:10.22.0
pages:
cache:
paths:
- node_modules/
script:
- yarn install # npm install
- yarn docs:build # npm run docs:build
artifacts:
paths:
- public
only:
- master
```
## Netlify
1. 在[Netlify](https://netlify.com),使用以下设置创建一个新的项目:
- **Build Command:** `vitepress build docs` or `yarn docs:build` or `npm run docs:build`
- **Publish directory:** `docs/.vitepress/dist`
2. 点击部署按钮。
## Google Firebase
1. 确保你已经安装了[firebase-tools](https://www.npmjs.com/package/firebase-tools)。
2. 在项目的根目录中创建`firebase.json`和`firebaserc`,并且使用以下内容:
`firebase.json`:
```json
{
"hosting": {
"public": "./docs/.vitepress/dist",
"ignore": []
}
}
```
`.firebaserc`:
```js
{
"projects": {
"default": "<YOUR_FIREBASE_ID>"
}
}
```
3. 在运行`yarn docs:build`或`npm run docs:build`后,使用`firebase deploy`命令部署。
## Surge
1. 如果你还没有安装[surge](https://www.npmjs.com/package/surge), 那么先安装。
2. 运行`yarn docs:build`或`npm run docs:build`。
3. 通过输入`surge docs/.vitepress/dist`来部署到surge。
You can also deploy to a [custom domain](https://surge.sh/help/adding-a-custom-domain) by adding `surge docs/.vitepress/dist yourdomain.com`.
你也可以通过添加`surge docs/.vitepress/dist yourdomain.com`来部署到[自定义域名](https://surge.sh/help/adding-a-custom-domain)。
## Heroku
1. 安装[Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli)。
2. 创建一个Heroku账户请[注册](https://signup.heroku.com)。
3. 运行`heroku login`并填写Heroku的账户信息
```bash
$ heroku login
```
4. 在项目的根目录中创建一个名为`static.json`的文件,并且使用以下内容:
`static.json`:
```json
{
"root": "./docs/.vitepress/dist"
}
```
这是你的网站的配置,更多信息请参考[heroku-buildpack-static](https://github.com/heroku/heroku-buildpack-static)。
5. 设置你的Heroku git remote:
```bash
# version change
$ git init
$ git add .
$ git commit -m "My site ready for deployment."
# creates a new app with a specified name
$ heroku apps:create example
# set buildpack for static sites
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-static.git
```
6. 部署你的网站:
```bash
# publish site
$ git push heroku master
# opens a browser to view the Dashboard version of Heroku CI
$ heroku open
```
## Vercel
为了部署VitePress网站使用[Vercel for Git](https://vercel.com/docs/git)请确保它已经被推送到Git仓库。
访问https://vercel.com/import/git 并使用你的Git选择GitHub、GitLab或BitBucket导入项目。使用向导来选择项目根目录并使用`yarn docs:build`或`npm run docs:build`来覆盖构建步骤,输出目录为`./docs/.vitepress/dist`。
![Override Vercel Configuration](../images/vercel-configuration.png)
在项目被导入后所有后续的推送分支都会生成预览部署并且所有在生产分支通常是“main”上的更改都会生成生产部署。
一旦部署完成你将会得到一个可以查看你的网站的URL例如https://vitepress.vercel.app

@ -0,0 +1,118 @@
---
sidebarDepth: 2
---
# Differences from VuePress 与 VuePress 的差异
VitePress 和 VuePress 有不同的设计目标。两个项目都共享相似的配置命名规范。VitePress 要求最少的功能,其他功能由主题推出。而 VuePress 有更多开箱即用的功能,或由其系统环境的插件推出。
::: tip
如果你正在使用 VuePress那么不需要迁移到 VitePress。两个项目都会并存在可预见的未来。
:::
::: warning
注意:这是一个早期的 WIP目前焦点在于使 Vite 变稳定和功能完整。不建议对涉及重大事情使用。
:::
如果你决定移动你的项目到 VitePress这是你需要考虑的 VuePress v1.7.1 的差异列表。
## General
- Missing
- 不支持 YAML 和 TOML 的站点配置文件格式。只支持 `.vitepress/config.js` 文件
- [插件](https://vuepress.vuejs.org/plugin/) 支持,功能由主题实现
- [permalink 支持](https://vuepress.vuejs.org/guide/permalinks.html)
- `.vitepress/templates`
- `.vitepress/components` 中的组件 [不会自动注册为全局组件](https://vuepress.vuejs.org/)
- Differences
- [公共文件](https://vuepress.vuejs.org/guide/assets.html#public-files),直接拷贝到 dist 根目录的文件,从 `.vitepress/public/` 移动到 `public/`
- [样式](https://vuepress.vuejs.org/config/#styling) `.vitepress/styles/index.styl``.vitepress/styles/palette.styl` 不支持。请参阅 [自定义 CSS](/guide/theming.html#customizing-css)。
- [应用级增强](https://vuepress.vuejs.org/guide/basic-config.html#app-level-enhancements) API应用级增强 `.vitepress/enhanceApp.js` 现在在 `.vitepress/theme/index.js` 中完成。请参阅 [扩展默认主题](/guide/theming.html#extending-the-default-theme)。
## Markdown
- Missing
- 支持 [toml 在frontmatter](https://vuepress.vuejs.org/guide/frontmatter.html#alternative-frontmatter-formats)
- [details block](https://vuepress.vuejs.org/guide/markdown.html#custom-containers)
- [markdown slots](https://vuepress.vuejs.org/guide/markdown-slot.html)
- `~` 前缀来显式指定一个 url 是一个 [webpack 模块请求](https://vuepress.vuejs.org/guide/assets.html#relative-urls)
## 站点配置
- Missing
- `temp`
- `dest`
- [`theme` from a dependency](https://vuepress.vuejs.org/theme/using-a-theme.html#using-a-theme-from-a-dependency)
- `permalink`
- [`port`](https://vuepress.vuejs.org/config/#port)
- [`shouldPrefetch`](https://vuepress.vuejs.org/config/#shouldprefetch)
- [`cache`](https://vuepress.vuejs.org/config/#cache)
- [`extraWatchFiles`](https://vuepress.vuejs.org/config/#extrawatchfiles)
- [`patterns`](https://vuepress.vuejs.org/config/#patterns)
- [`plugins`](https://vuepress.vuejs.org/config/#pluggable)
- [`markdown.pageSuffix`](https://vuepress.vuejs.org/config/#markdown-pagesuffix)
- [`markdown.slugify`](https://vuepress.vuejs.org/config/#markdown-slugify)
- [`markdown.plugins`](https://vuepress.vuejs.org/config/#markdown-plugins)
- [`markdown.extractHeaders`](https://vuepress.vuejs.org/config/#markdown-extractheaders)
- `markdown.extendMarkdown` to `markdown.config`
- `configureWebpack`, `chainWebpack`, `postcss`, `Stylus`, `scss`, `Sass`, `less` configs
- [`evergreen`](https://vuepress.vuejs.org/config/#evergreen)
## 默认主题配置
- Missing
- [`smoothScroll`](https://vuepress.vuejs.org/theme/default-theme-config.html#smooth-scrolling)
- [`displayAllHeaders`](https://vuepress.vuejs.org/theme/default-theme-config.html#displaying-header-links-of-all-pages)
- [`activeHeaderLinks`](https://vuepress.vuejs.org/theme/default-theme-config.html#active-header-links)
- `sidebarDepth` and `initialOpenGroupIndex` for [sidebar groups](https://vuepress.vuejs.org/theme/default-theme-config.html#sidebar-groups)
- Differences
- `searchMaxSuggestions` is `search.maxSuggestions`
- `algolia` is `search.algolia`
- `searchPlaceholder` is `search.placeholder`
## 默认主题
- Missing
- [`<code-group>` and `<code-block>`](https://vuepress.vuejs.org/theme/default-theme-config.html#code-groups-and-code-blocks)
## 全局计算属性
- Missing
- `$lang`
- `$localePath`
## 前言预定义变量
- Missing
- `description`
- [`meta`](https://vuepress.vuejs.org/guide/frontmatter.html#meta)
- [`metaTitle`](https://vuepress.vuejs.org/guide/frontmatter.html#predefined-variables)
- `lang`
- [`layout`](https://vuepress.vuejs.org/guide/frontmatter.html#layout)
- [`permalink`](https://vuepress.vuejs.org/guide/frontmatter.html#predefined-variables)
- [`canonicalUrl`](https://vuepress.vuejs.org/guide/frontmatter.html#predefined-variables)
## 前言预定义变量
- Missing
- `prev`, `next`
- [`search`](https://vuepress.vuejs.org/guide/frontmatter.html#search)
- [`tags`](https://vuepress.vuejs.org/guide/frontmatter.html#tags)
- [`pageClass`](https://vuepress.vuejs.org/theme/default-theme-config.html#custom-page-class)
- [`layout`](https://vuepress.vuejs.org/theme/default-theme-config.html#custom-layout-for-specific-pages)
## 站点数据
- Missing
- [`pages`](https://vuepress.vuejs.org/theme/writing-a-theme.html#site-and-page-metadata)
## 页面数据
- Missing
- `key`
- `path`
- `regularPath`
## 全局组件
- Missing
- [`<Badge>`](https://vuepress.vuejs.org/guide/using-vue.html#badge)

@ -0,0 +1,90 @@
# 前言
任何包含了YAML前言块的Markdown文件都会被[gray-matter](https://github.com/jonschlinkert/gray-matter)处理。前言必须在Markdown文件的顶部并且必须是有效的YAML集。例如
```md
---
title: Docs with VitePress
editLink: true
---
```
在三杠线之间,你可以设置[预定义变量](#predefined-variables),或者创建自己的变量。这些变量可以通过特殊的<code>$frontmatter</code>变量来使用。
这里是一如何在你的Markdown文件中使用它的例子
```md
---
title: Docs with VitePress
editLink: true
---
# {{ $frontmatter.title }}
导读内容
```
## 可选的前言格式
VitePress也支持JSON前言格式开始和结束在花括号中
```json
---
{
"title": "Blogging Like a Hacker",
"editLink": true
}
---
```
## 可预定义的变量
### 题目
- Type: `string`
- Default: `h1_title || siteData.title`
当前页面的标题。
### 标题
- Type: `array`
- Default: `undefined`
指定要注入的额外的head标签
```yaml
---
head:
- - meta
- name: description
content: hello
- - meta
- name: keywords
content: super duper SEO
---
```
### 导航栏
- Type: `boolean`
- Default: `undefined`
你可以在特定页面上禁用导航栏,使用`navbar: false`
### sidebar 侧边栏
- Type: `boolean|'auto'`
- Default: `undefined`
你可以在特定页面上决定显示侧边栏,使用`sidebar: auto`或者禁用它,使用`sidebar: false`
### editLink 编辑链接
- Type: `boolean`
- Default: `undefined`
定义这个页面是否包含编辑链接。

@ -0,0 +1,50 @@
# Getting Started起步
这一节将帮助你从零开始建立一个基于VitePress的文档站点。如果你已经有一个现有的项目并且希望将文档保存在项目中请从第三步开始。
- **Step. 1:** 创建并切换到一个新的目录
```bash
$ mkdir vitepress-starter && cd vitepress-starter
```
- **Step. 2:** 使用你喜欢的包管理器初始化
```bash
$ yarn init
```
- **Step. 3:** 下载VitePress到本地
```bash
$ yarn add --dev vitepress
```
- **Step. 4:** 创建第一个文档
```bash
$ mkdir docs && echo '# Hello VitePress' > docs/index.md
```
- **Step. 5:** 添加一些脚本到`package.json`
```json
{
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:serve": "vitepress serve docs"
}
}
```
- **Step. 6:** 启动本地服务器
```bash
$ yarn docs:dev
```
http://localhost:3000.VitePress会在http://localhost:3000上启动一个热重载开发服务器。
现在你应该已经有一个基本的但是功能的VitePress文档站点。
当你的文档站点开始成型,请阅读[部署指南](./deploy)。

@ -0,0 +1,33 @@
# Global Component
VitePress comes with few built-in component that can be used globally. You may use these components in your markdown or your custom theme configuration.
## Content
The `Content` component displays the rendered markdown contents. Useful [when creating your own theme](https://vitepress.vuejs.org/guide/customization.html).
```vue
<template>
<h1>Custom Layout!</h1>
<Content />
</template>
```
## ClientOnly
The `ClientOnly` component renderes its slot only at client side.
Because VitePress applications are server-rendered in Node.js when generating static builds, any Vue usage must conform to the universal code requirements. In short, make sure to only access Browser / DOM APIs in beforeMount or mounted hooks.
If you are using or demoing components that are not SSR-friendly (for example, contain custom directives), you can wrap them inside the `ClientOnly` component.
```html
<ClientOnly>
<NonSSRFriendlyComponent />
</ClientOnly>
```
## OutboundLink
The indicator `OutboundLink` is used to denote external links. In VitePress, this component has been followed by every external link.

@ -0,0 +1,426 @@
---
sidebarDepth: 3
---
# Markdown 扩展
## 头部锚点
头部自动生成锚点,锚点的渲染可以通过`markdown.anchor`配置。
## 链接
### 内部链接
内部链接转换为路由链接以便SPA导航。同时每个子目录中的`index.md`都会自动转换为`index.html`对应的URL为`/`。
例如,给定以下目录结构:
```
.
├─ index.md
├─ foo
│ ├─ index.md
│ ├─ one.md
│ └─ two.md
└─ bar
├─ index.md
├─ three.md
└─ four.md
```
如果你在`foo/one.md`
```md
[Home](/) <!-- sends the user to the root index.md -->
[foo](/foo/) <!-- sends the user to index.html of directory foo -->
[foo heading](./#heading) <!-- anchors user to a heading in the foo index file -->
[bar - three](../bar/three) <!-- you can omit extention -->
[bar - three](../bar/three.md) <!-- you can append .md -->
[bar - four](../bar/four.html) <!-- or you can append .html -->
```
### 页面后缀
默认情况下,页面和内部链接会默认以`.html`后缀生成。
### 外部链接
外部链接会自动获得`target="_blank" rel="noopener noreferrer"`
- [vuejs.org](https://vuejs.org)
- [VitePress on GitHub](https://github.com/vuejs/vitepress)
## 页面配置
[YAML frontmatter](https://jekyllrb.com/docs/frontmatter/) is supported out of the box:
[YAML frontmatter](https://jekyllrb.com/docs/frontmatter/)是默认支持的。
```yaml
---
title: Blogging Like a Hacker
lang: en-US
---
```
这些数据将会在页面的其他部分和主题组件中可用。
更多细节,请参阅[Frontmatter](./frontmatter.md)。
## GitHub风格表格
**输入**
```
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
```
**输出**
| Tables | Are | Cool |
| ------------- | :-----------: | -----: |
| col 3 is | right-aligned | \$1600 |
| col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 |
## Emoji :tada:
**输入**
```
:tada: :100:
```
**输出**
:tada: :100:
可用的[所有表情符号](https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.json)
## 目录
**输入**
```
[[toc]]
```
**输出**
[[toc]]
目录的渲染可以通过`markdown.toc`配置。
## 定制容器
定制容器可以通过其类型、标题和内容来定义。
### 默认标题
**输入**
```md
::: tip
This is a tip
:::
::: info
This is an info box
:::
::: warning
This is a warning
:::
::: danger
This is a dangerous warning
:::
```
**输出**
::: tip
This is a tip
:::
::: info
This is an info box
:::
::: warning
This is a warning
:::
::: danger
This is a dangerous warning
:::
### 自定义标题
**输入**
```md
::: danger STOP
Danger zone, do not proceed
:::
```
**输出**
::: danger STOP
Danger zone, do not proceed
:::
## 代码块语法高亮
VitePress使用[Prism](https://prismjs.com/)来高亮Markdown代码块中的语法使用彩色文本。Prism支持许多程序语言。你只需要在代码块的开头添加有效的语言别名即可。
**输入**
````
```js
export default {
name: 'MyComponent',
// ...
}
```
````
**输出**
```js
export default {
name: 'MyComponent'
// ...
}
```
**输入**
````
```html
<ul>
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
</li>
</ul>
```
````
**输出**
```html
<ul>
<li v-for="todo in todos" :key="todo.id">{{ todo.text }}</li>
</ul>
```
Prism上有一个有效[多种语言的列表](https://prismjs.com/#languages-list)
## 代码块行高亮
**输入**
````
```js{4}
export default {
data () {
return {
msg: 'Highlighted!'
}
}
}
```
````
**输出**
```js{4}
export default {
data () {
return {
msg: 'Highlighted!'
}
}
}
```
除了单行,你还可以指定多行、范围或者两者。
- 范围:例如`{5-8}``{3-10}``{10-17}`
- 多个单行:例如`{4,7,9}`
- 范围和单行:例如`{4,7-13,16,23-27,40}`
**输入**
````
```js{1,4,6-7}
export default { // Highlighted
data () {
return {
msg: `Highlighted!
This line isn't highlighted,
but this and the next 2 are.`,
motd: 'VitePress is awesome',
lorem: 'ipsum',
}
}
}
```
````
**输出**
```js{1,4,6-8}
export default { // Highlighted
data () {
return {
msg: `Highlighted!
This line isn't highlighted,
but this and the next 2 are.`,
motd: 'VitePress is awesome',
lorem: 'ipsum',
}
}
}
```
## 行号
你可以通过配置来启用每个代码块的行号:
```js
module.exports = {
markdown: {
lineNumbers: true
}
}
```
- Demo:
<picture>
<source srcset="../images/line-numbers-mobile.gif" media="(max-width: 719px)">
<img class="line-numbers-mobile-snap" src="../images/line-numbers-mobile.gif" alt="Image">
</picture>
<picture>
<source srcset="../images/line-numbers-desktop.png" media="(min-width: 720px)">
<img class="line-numbers-desktop-snap" src="../images/line-numbers-desktop.png" alt="Image">
</picture>
<style>
.line-numbers-mobile-snap {
margin: 0 -1.5rem;
width: 100vw;
max-width: none !important;
}
.line-numbers-desktop-snap {
display: none;
}
@media (min-width: 720px) {
.line-numbers-mobile-snap {
display: none;
}
.line-numbers-desktop-snap {
display: block;
}
}
</style>
## 导入代码片段
你可以通过以下语法来导入代码片段:
```md
<<< @/filepath
```
它也支持[行高亮](#line-highlighting-in-code-blocks)
```md
<<< @/filepath{highlightLines}
```
**输入**
```md
<<< @/snippets/snippet.js{2}
```
**代码文件**
<!--lint disable strong-marker-->
<<< @/snippets/snippet.js
<!--lint enable strong-marker-->
**输出**
<!--lint disable strong-marker-->
<<< @/snippets/snippet.js{2}
<!--lint enable strong-marker-->
::: tip
`@`的值对应于根路径。默认情况下是VitePress项目根路径除非配置了`srcDir`。
:::
你也可以使用[VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding)来只包含对应的代码文件的部分。你可以在`#`后面提供一个自定义的region名称默认是`snippet`
**输入**
```md
<<< @/snippets/snippet-with-region.js{1}
```
**代码文件**
<!--lint disable strong-marker-->
<<< @/snippets/snippet-with-region.js
<!--lint enable strong-marker-->
**输出**
<!--lint disable strong-marker-->
<<< @/snippets/snippet-with-region.js#snippet{1}
<!--lint enable strong-marker-->
## 高级配置选项
VitePress使用[markdown-it](https://github.com/markdown-it/markdown-it) 作为Markdown渲染器。大部分的扩展都是通过自定义插件实现的。你可以通过在`.vitepress/config.js`中的`markdown`选项来自定义`markdown-it`实例:
```js
const anchor = require('markdown-it-anchor')
module.exports = {
markdown: {
// options for markdown-it-anchor
// https://github.com/valeriangalliat/markdown-it-anchor#permalinks
anchor: {
permalink: anchor.permalink.headerLink()
},
// options for markdown-it-table-of-contents
toc: { includeLevel: [1, 2] },
config: (md) => {
// use more markdown-it plugins!
md.use(require('markdown-it-xxx'))
}
}
}
```

@ -0,0 +1,157 @@
# 主题
## 使用自定义主题
你可以通过添加 `.vitepress/theme/index.js` 文件来启用自定义主题。
```bash
.
├─ docs
│ ├─ .vitepress
│ │ ├─ theme
│ │ │ └─ index.js
│ │ └─ config.js
│ └─ index.md
└─ package.json
```
一个 VitePress 自定义主题是一个包含三个属性的对象,它定义如下:
```ts
interface Theme {
Layout: Component // Vue 3 component
NotFound?: Component
enhanceApp?: (ctx: EnhanceAppContext) => void
}
interface EnhanceAppContext {
app: App // Vue 3 app instance
router: Router // VitePress router instance
siteData: Ref<SiteData>
}
```
主题入口文件应该将主题作为其默认导出:
```js
// .vitepress/theme/index.js
import Layout from './Layout.vue'
export default {
Layout,
NotFound: () => 'custom 404', // <- this is a Vue 3 functional component
enhanceApp({ app, router, siteData }) {
// app is the Vue 3 app instance from `createApp()`. router is VitePress'
// custom router. `siteData`` is a `ref`` of current site-level metadata.
}
}
```
...`Layout` 组件可以像这样:
```vue
<!-- .vitepress/theme/Layout.vue -->
<template>
<h1>Custom Layout!</h1>
<Content /><!-- this is where markdown content will be rendered -->
</template>
```
默认导出是自定义主题的唯一约定。在自定义主题中,它类似于普通的 Vite + Vue 3 应用。请注意,自定义主题也需要 [SSR-兼容](/guide/using-vue.html#browser-api-access-restrictions)。
要分发一个主题,只需要将对象从您的包入口导出。要从自定义主题入口中导入并重新导出它:
```js
// .vitepress/theme/index.js
import Theme from 'awesome-vitepress-theme'
export default Theme
```
## 扩展默认主题
如果你想扩展并且定制默认主题,你可以从 `vitepress/theme` 中导入它并在自定义主题入口中增强它。这里有一些常见的定制化的例子:
### 注册全局组件
```js
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
export default {
...DefaultTheme,
enhanceApp({ app }) {
// register global components
app.component('MyGlobalComponent' /* ... */)
}
}
```
因为我们使用 Vite所以你也可以使用 Vite 的 [glob 导入特性](https://vitejs.dev/guide/features.html#glob-import) 来自动注册一个目录下的组件。
### 自定义 CSS
默认主题 CSS 可以通过覆盖根级 CSS 变量来定制:
```js
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import './custom.css'
export default DefaultTheme
```
```css
/* .vitepress/theme/custom.css */
:root {
--c-brand: #646cff;
--c-brand-light: #747bff;
}
```
参考 [默认主题 CSS 变量](https://github.com/vuejs/vitepress/blob/master/src/client/theme-default/styles/vars.css) 可以被覆盖。
### 布局插槽
默认主题的 `<Layout/>` 组件有几个插槽可以用来在页面的某些位置注入组件。这里是注入一个组件到侧边栏的顶部的例子:
```js
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import MyLayout from './MyLayout.vue'
export default {
...DefaultTheme,
// override the Layout with a wrapper component that injects the slots
Layout: MyLayout
}
```
```vue
<!--.vitepress/theme/MyLayout.vue-->
<script setup>
import DefaultTheme from 'vitepress/theme'
const { Layout } = DefaultTheme
</script>
<template>
<Layout>
<template #sidebar-top>
My custom sidebar top content
</template>
</Layout>
</template>
```
默认主题布局中的所有插槽:
- `navbar-search`
- `sidebar-top`
- `sidebar-bottom`
- `page-top-ads`
- `page-top`
- `page-bottom`
- `page-bottom-ads`
- Only when `home: true` is enabled via frontmatter:
- `home-hero`
- `home-features`
- `home-footer`

@ -0,0 +1,261 @@
---
sidebarDepth: 3
---
# 在 Markdown 中使用 Vue
在VitePress中每个Markdown文件都会被编译为HTML然后再通过Vue单文件组件来处理。这意味着你可以在Markdown中使用任何Vue功能包括动态模板使用Vue组件或者通过添加`<script>`Vue
同样重要的是VitePress使用Vue 3的编译器来自动检测和优化Markdown的纯静态部分。静态内容会被优化为单个占位符节点并从页面的JavaScript载荷中删除。它们也会在客户端渲染时被跳过。简而言之只为页面上的动态部分消耗资源。
## Templating
### Interpolation
每个Markdown文件都会被编译为HTML然后再通过Vue组件传递给Vite处理流程。这意味着你可以在文本中使用Vue-style插值
**输入**
```md
{{ 1 + 1 }}
```
**输出**
<div class="language-text"><pre><code>{{ 1 + 1 }}</code></pre></div>
### 指令
指令也可以生效:
**输入**
```md
<span v-for="i in 3">{{ i }} </span>
```
**输出**
<div class="language-text"><pre><code><span v-for="i in 3">{{ i }} </span></code></pre></div>
### 取得站点和页面数据
你可以在`<script>`使[`useData`](/guide/api.html#usedata)
**输入**
```md
<script setup>
import { useData } from 'vitepress'
const { page } = useData()
</script>
<pre>{{ page }}</pre>
```
**输出**
```json
{
"path": "/using-vue.html",
"title": "Using Vue in Markdown",
"frontmatter": {}
}
```
## 逃逸
默认情况下,标记代码块被自动包裹在`v-pre`。要在行内代码片段或纯文本中显示mustache或Vue特定语法你需要在段落中包裹`v-pre`自定义容器。
**输入**
```md
::: v-pre
`{{ This will be displayed as-is }}`
:::
```
**输出**
::: v-pre
`{{ This will be displayed as-is }}`
:::
## 使用组件
当你需要更高的灵活性时VitePress允许你通过使用你自己的Vue组件来扩展你的作者工具箱。
### 在markdown中导入组件
如果你的组件只在几处使用,那么推荐的方式是在使用它的文件中导入它们。
```md
<script setup>
import CustomComponent from '../components/CustomComponent.vue'
</script>
# 文档
这是一个使用自定义组件的.md文件
<CustomComponent />
## 更多文档
...
```
### 为主题注册全局组件
如果组件将被在文档中跨页面使用那么它们可以在主题或扩展默认VitePress主题中注册为全局组件。查看[主题指南](./theming.md)以获取更多信息。
在`.vitepress/theme/index.js`中,`enhanceApp`函数接收Vue`app`实例因此你可以像在普通Vue应用中一样[注册组件](https://v3.vuejs.org/guide/component-registration.html#component-registration)。
```js
import DefaultTheme from 'vitepress/theme'
export default {
...DefaultTheme,
enhanceApp({ app }) {
app.component('VueClickAwayExample', VueClickAwayExample)
}
}
```
之后你的markdown文件中组件可以在插入在内容之间
```md
# Vue Click Away
<VueClickAwayExample />
```
::: warning 重点
确保自定义组件的名称包含一个连字符或是使用首字母大写的单词。否则,它将被视为一个行内元素,并将被包裹在`<p>`标签中,这将导致渲染不匹配,因为`<p>`不允许块元素放在它里面。
:::
### 使用组件在标题中 <ComponentInHeader />
你可以在标题中使用Vue组件但请注意以下语法的区别
| Markdown | Output HTML | Parsed Header |
| ------------------------------------------------------- | ----------------------------------------- | ------------- |
| <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/>` |
HTML被`<code>`包裹的将被显示为-是只有不被包裹的HTML将被Vue解析。
::: 提示
输出的HTML是通过[markdown-it](https://github.com/markdown-it/markdown-it)而解析的标题是由VitePress处理并且用于侧边栏和文档标题
:::
## 使用CSS预处理器
VitePress有[内置支持](https://vitejs.dev/guide/features.html#css-pre-processors)的CSS预处理器`.scss`, `.sass`, `.less`, `.styl`和`.stylus`文件。没有必要安装Vite特定的插件但对应的预处理器本身必须安装
```
# .scss and .sass
npm install -D sass
# .less
npm install -D less
# .styl and .stylus
npm install -D stylus
```
然后你可以在Markdown和主题组件中使用以下
```vue
<style lang="sass">
.title
font-size: 20px
</style>
```
## 代码和样式的提升
有时你可能需要在当前页面应用一些JavaScript或CSS。在这些情况下你可以直接在Markdown文件中写入根级别的`<script>``<style>`Vue`<script>``<style>`使
<p class="demo" :class="$style.example"></p>
<style module>
.example {
color: #41b883;
}
</style>
<script>
import ComponentInHeader from '../components/ComponentInHeader.vue'
export default {
props: ['slot-key'],
components: { ComponentInHeader },
mounted () {
document.querySelector(`.${this.$style.example}`)
.textContent = 'This is rendered by inline script and styled by inline CSS'
}
}
</script>
## 内置组件
VitePress提供了内置的Vue组件如`ClientOnly`和`OutboundLink`,查看[全局组件引导](./global-component.md)以获取更多信息。
**参阅:**
- [在Headers中使用组件](#using-components-in-headers)
## 使用浏览器API的限制
因为VitePress应用在生成静态构建时在Node.js中服务端渲染所以任何Vue使用必须遵循[通用代码要求](https://ssr.vuejs.org/en/universal.html)。简单地说,在`beforeMount`或`mounted`钩子中只能访问浏览器/DOM API。
如果你使用或演示不是SSR-友好的组件(例如包含自定义指令),你可以在内置的`<ClientOnly>`组件中包裹它们:
```md
<ClientOnly>
<NonSSRFriendlyComponent/>
</ClientOnly>
```
注意,这不是修复组件或库**在导入时**访问浏览器API的问题。要在导入时假设浏览器环境你需要在适当的生命周期钩子中动态导入它们
```vue
<script>
export default {
mounted() {
import('./lib-that-access-window-on-import').then((module) => {
// use code
})
}
}
</script>
```
If your module `export default` a Vue component, you can register it dynamically:
```vue
<template>
<component v-if="dynamicComponent" :is="dynamicComponent"></component>
</template>
<script>
export default {
data() {
return {
dynamicComponent: null
}
},
mounted() {
import('./lib-that-access-window-on-import').then((module) => {
this.dynamicComponent = module.default
})
}
}
</script>
```
**参阅:**
- [Vue.js > Dynamic Components](https://v3.vuejs.org/guide/component-dynamic-async.html)

@ -0,0 +1,54 @@
---
sidebarDepth: 2
---
# 什么是VitePress?
::: warning WARNING
VitePress 当前处于 0.x 状态,已经适合于默认的文档使用,但是配置和主题 API 可能会在小版本中更改。
:::
VitePress 是一个基于 [Vite](https://github.com/vitejs/vite) 的 [VuePress](https://vuepress.vuejs.org/)' 兄弟项目。
## 动机
我们爱 VuePress v1但是基于 Webpack 的构建,在一个简单的文档站点上,启动开发服务器需要的时间变得无法忍受。即使 HMR 更新也需要一秒钟才能在浏览器中反映出来!
根本原因是 VuePress v1 是基于 Webpack 的一个应用。即使只有两个页面,它也是一个完整的 Webpack 项目(包括所有主题源文件)被编译。更糟糕的是当项目有很多页面时,在服务器显示能任何东西之前每个页面都必须先完全编译!
顺便说一下Vite 很好地解决了这些问题:瞬间的服务器启动,一次性编译只编译正在被服务的页面,以及非常快的 HMR。另外在VuePress v1中我还注意到一些额外的设计问题但由于需要大量的重构所以一直没有时间去解决。
现在,有了 Vite 和 Vue 3 是时候重新考虑一下“Vue 驱动的静态站点生成器”可以是什么样子了。
## 在VuePress v1之上的改进
和 VuePress v1 相比有几个改进的地方...
### 使用Vue 3
利用了 Vue 3 的模板静态分析来将静态内容尽可能的字符串化。静态内容被发送为字符串字面量而不是 JavaScript 渲染函数代码,因此 JS 加载 _更加_ 节省,并且渲染速度更快。
请注意在应用优化的同时仍然允许用户在markdown内容中自由混合Vue组件--编译器自动为你做静态/动态分离,你永远不需要考虑这个问题。
### 底层使用Vite
- 更快的开发服务器启动
- 更快的热更新
- 更快的构建(内部实现使用 Rollup
### 更轻量的页面
- Vue 3 tree-shaking + Rollup 分割代码
- 不会在每次请求中发送每个页面的元数据。这将页面重量与总页数分离。只会发送当前页面的元数据。客户端导航将同时获取新页面的组件和元数据。
- 不使用`vue-router`因为VitePress的需求非常简单和具体 - 使用一个简单的自定义路由器低于200 LOC来代替。
- (WIP) i18n 地区的数据应该按需获取。
## 其他的差异
Vitepress 更为专业和更少的配置VitePress旨在缩减当前VuePress中的复杂性并从其极简主义的根源重新开始。
VitePress 面向未来的VitePress 只支持支持原生 ES 模块导入的浏览器。它建议使用原生的 JavaScript 不转换,和使用 CSS 变量来进行主题设计。
## 未来是否会成为下一个 VuePress
我们已经有了[vuepress-next](https://github.com/vuepress/vuepress-next), 它会成为 Vuepress 的下一个大版本。它还会比 VuePress v1 有更加多的改进,并且现在也支持 Vite 。
VitePress 不兼容当前 VuePress 社区系统主题和插件。总的想法是VitePress将有一个大幅减少的主题API倾向于JavaScript API而不是文件布局惯例并且可能没有插件所有定制都在主题中完成
Loading…
Cancel
Save