基础使用
基础使用
1、安装
$ mkdir vitepress-starter && cd vitepress-starter
$ mkdir docs && echo '# Hello VitePress' > docs/index.md
$ yarn init
# 创建项目
$ yarn add --dev vitepress vue
# 用依赖
yarn add --dev types@node
2、package.json 初始化
添加"scripts"
字段
{
...
"scripts": {
"docs:dev": "vitepress dev docs", // 开发
"docs:build": "vitepress build docs", // 默认的输出目录将在 ./vitepress/dist
"docs:serve": "vitepress serve docs" // 测试
"docs:serve": "vitepress serve docs --port 8080" // 测试
},
...
}
3、启动项目
$ yarn docs:dev
# 默认端口
http://localhost:5173
4、添加其他页面
.
├─ docs
│ ├─ getting-started.md
│ └─ index.md
└─ package.json
5、添加配置文件
.
├─ docs
│ ├─ .vitepress # 这个是默认的配置目录
│ │ └─ config.js|.ts # 全局配置文件
│ └─ index.md
└─ package.json
// config.js
export default {
title: 'VitePress',
base:"路由子路径",
description: 'Just playing around.'
}
如果您的网站要在子目录 ( ) 中提供服务,那么您
https://example.com/subdir/
必须'/subdir/'
在.base
docs/.vitepress/config.js
示例:如果您使用 Github(或 GitLab)页面并部署到
user.github.io/repo/
,则将您的设置base
为/repo/
.
项目结构
.
├─ docs
│ ├─ .vitepress # 这个是默认的配置目录
│ │ └─ config.js|.ts # 全局配置文件
│ ├─ getting-started.md # md文件
│ └─ index.md # 首页
└─ package.json
配置使用
全局配置
配置文件:./.vitepress/config.js|ts
字段名称 | 类型:默认值 | 说明 |
---|---|---|
title | string:"VitePress" | |
titleTemplate | string|false:"xxxxxxxxxxxx" | 主题名称,最终渲染在title前面: `VitePress |
lang | string | 对应标签 <html lang="en-US"/> |
head | HeadConfig[]:[] | 将会渲染成<head> 内的标签 |
description | string | 将会渲染成<meta> 标签 |
base | string:"/" | 是否存在子路径 |
appearance | boolean:true | 是否启用“暗模式”。 |
ignoreDeadLinks | boolean:false | 设置为true时,如果遇到无效路径也会照常渲染 |
lastUpdated | boolean:false | 显示页面文件对应的git commit更新时间 |
markdown | MarkdownOption | md的解析器相关配置,默认采用markdown-it |
outDir | string:./.vitepress/dist | 构建文件目录 |
cleanUrls | enum<str> :disabled|without-subfolders|with-subfolders | 路径去掉.html 后缀 |
themeConfig | ThemeOptions | 主题配置 |
主题配置 - themeConfig
更准确的说是整个页面的布局配置
配置位置:./.vitepress/config.themeConfig
// ./.vitepress/config.ts
import { ThemeOptions } from vitepress
const themeConfig:ThemeOptions = {}
export default themeConfig
字段名称 | 类型:默认值 | 说明 |
---|---|---|
logo | ThemeableImage:"/logo.svg" | logo图片地址,可以是图片会或者svg |
siteTitle | string|boolen:"false" | 导航的title |
nav | NavItem | 导航列表 |
sidebar | Sidebar | 侧边栏列表 |
outlineTitle | string | 侧边栏的标题 |
socialLinks | SocialLink[] | 社交链接,github|gitee|twitter|qq等等 |
footer | Footer | 配置页脚 |
editLink | EditLink | 在线编辑,这里可以绑定一个匹配式https://github.com/vuejs/vitepress/edit/main/docs/:path |
lastUpdatedText | string:"Last updated" | 最后更新的文字描述前缀 |
carbonAds | CarbonAds | 广告 |
docFooter | DocFooter | 上一个和下一个链接上方的文本 |
页面配置
页面配置可以覆盖任何全局配置和主题配置,拥有最高的优先权,在自定义页面时非常有用
这些配置位于每个页面的md文件中,以三横杠包裹
---
title: Docs with VitePress
editLink: true
---
也可以在md内容中,通过 $变量+{{模板语法}}
的形式动态调用
{{ $frontmatter.title }}
字段名称 | 类型:默认值 | 说明 |
---|---|---|
title | string:"vitepress" | 页面标题 |
titleTemplate | string|boolean | 于config.titleTemplate 等价 |
description | string | 于config.description 等价 |
head | HeadConfig[] | 于config.head 等价 |
lastUpdated | boolean:true | 是否显示当前页面md文件的最后更新时间,等价于config.lastUpdated |
layout | string: `doc | home |
hero | Hero | layout 该选项仅在设置为home 时生效,对home 的一些具体配置 |
features | Feature[] | layout 该选项仅在设置为home 时生效,对home 的一些具体配置 |
aside | boolean:true | 当前页面是否展示右侧组件(目录) |
MD解析器配置
VitePress 使用markdown-it作为 Markdown 渲染器。在config里面也可以通过引入markdown-it的一些实例进行操作
const anchor = require('markdown-it-anchor')
module.exports = {
markdown: {
// options for markdown-it-anchor
// https://github.com/valeriangalliat/markdown-it-anchor#usage
anchor: {
permalink: anchor.permalink.headerLink()
},
// options for @mdit-vue/plugin-toc
// https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-toc#options
toc: { level: [1, 2] },
config: (md) => {
// use more markdown-it plugins!
md.use(require('markdown-it-xxx'), opts)
}
}
}
注意事项
如果使用 @docsearch/js 和pnpm,需要在pacakage.json中加入以下配置
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"@algolia/client-search"
]
}
}
github-workflows
.github/workflows/vitepress-deploy.yml
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn
- run: yarn install --frozen-lockfile
- name: Build
run: yarn docs:build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/.vitepress/dist
请替换对应的分支名称。例如,如果您要构建的分支是
master
,那么您应该在上面的文件中main
替换为。master
Markdown
使用md语法的跳转功能,以相对路径的方式编写,最终会被html正确的编译
[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 -->
YAML frontmatter
支持yaml配置内置到md文件内
---
title: Blogging Like a Hacker
lang: en-US
---
在三点虚线之间,您可以设置预定义变量,甚至可以创建自己的自定义变量。这些变量可以通过特殊$frontmatter
变量使用。
这是一个如何在 Markdown 文件中使用它的示例:
---
title: Docs with VitePress
editLink: true
---
# {{ $frontmatter.title }}
Guide content
JSON frontmatter 语法
---
{
"title": "Blogging Like a Hacker",
"editLink": true
}
---