本文初步介绍vuepress的安装和使用。
初试
环境
安装 yarn,并添加国内源:
1 | npm install -g yarn |
初始化
创建并进入一个新目录
1 | mkdir vuepress-starter && cd vuepress-starter |
初始化:
1 | yarn init |
注:在 Windows 下用 cmd 执行。用 git bash 会报错:
1 | error An unexpected error occurred: "Can't answer a question unless a user TTY". |
生成如下 package.json
1 | { |
注:可以只使用 cmd 生成 json 文件,或手动写 json 文件。再用回 git bash。
运行
(官方当前推荐)本地安装 vuepress:
1 | yarn add -D vuepress |
会生成 node_modules目录(通过 yarn 安装的插件、主题等在此目录)。
创建一篇文档:
1 | mkdir docs && echo '# Hello VuePress' > docs/README.md |
在 package.json 中添加 scripts,以便执行:
1 | "scripts": { |
启动服务器:
1 | yarn dev |
即可用浏览器访问本地 8080 端口,观看效果。
如需构建静态页面,执行:
1 | yarn build |
注:构建后在 docs 下有 .vuepress/dist目录,dist 即为静态网站文件所在目录。将其放到其它 web 服务器的资源目录中,即为网站。
**注意:官方使用 docs 目录作为工程目录,为了不让目录过深,也可以不使用,此时vuepress dev docs
改为vuepress dev
即可。
进阶
创建目录和文件:
1 | mkdir -p docs/.vuepress docs/.vuepress/public |
注:.vuepress 目录为 vuepress 的配置根目录。所有的配置文件均在其下。public 为公共访问目录,可存放 icon、logo等资源,config.js 是配置文件,非常重要。
修改 docs/README.md 文件:
1 | --- |
config.js 文件:
1 | module.exports = { |
注:favicon.ico、logo.png、hero.png等在 public 目录。
主题
获取默认主题:
1 | yarn vuepress eject |
在当前目录的 .vuepress 有 theme 目录,其下即为默认主题所有文件。
将 theme 目录放到 docs/.vuepress
目录下。
扩展
文章加密插件:
1 | yarn add -D @oak-tree-house/vuepress-plugin-encrypt |
参考:
https://www.imaegoo.com/2020/vuepress-encrypt/ https://szp15.com/2020/02/04/plugin-encrypt-doc/
还不成功
知识点
路径路由默认使用目录的 README.md 文件。
markdown 有扩展功能。
vuepress 大部分文件都支持热更新,但有的配置可能不会,如 config.js,需要重启服务。当修改 markdown 文档时,浏览器会自动刷新。
主题可继承,从默认主题继承。
执行vuepress
无命令,使用yarn vuepress
代替。
markdown 小记录:
1 | 站内跳转,/表示从根目录开始 |
内嵌 vue 代码:
1 | // 循环,输出1 2 3 |
问题及解决
支持中文名称图片。
安装:
1 | yarn add markdown-it-disable-url-encode |
在.vuepress/config.js
添加:
1 | module.exports = { |
另一个主题的安装
下载新版本node http://npm.taobao.org/mirrors/node/v16.2.0/,双击安装之。
在某目录执行:
1 | yarn create vuepress-theme-hope vue-hope |
一路回车即可。进入vue-hope
目录,执行:
1 | yarn install |
注:该主题依赖的node版本必须高于13.7。 修改后的package.json:
1 | { |
常用命令:
1 | 启动服务: |
参考:https://vuepress-theme-hope.github.io/zh/guide/
资源
参考:
快速上手:https://vuepress.vuejs.org/zh/guide/
主题:https://vuepress.vuejs.org/zh/theme/
资源(主题、插件):https://github.com/vuepress/awesome-vuepress
默认主题:https://github.com/vuejs/vuepress 仓库 packages/@vuepress
目录
主题:
https://github.com/xuzhongpeng/vuepress-theme-reform
https://github.com/vuepress-reco/vuepress-theme-reco
https://github.com/xugaoyi/vuepress-theme-vdoing
查看 markdown 中支持语法的语言名称:https://prismjs.com/#languages-list
李迟 2021.4.30