前言

最近完成了 TypeScript 最新官方文档 Hanbook 的翻译,一共十四篇,堪称国内的最好 TypeScript4 入门教程之一为了方便大家阅读,我用 VuePress + Github Pages 搭建了博客,最终的博客地址如下:

VuePress 自然不用多说,基于 Vue 的静态网站生成器,风格简约,配置也比较简单。之所以不使用 VitePress,是因为想使用现有的主题, 而 VitePress 不兼容当前 VuePress 的生态系统,至于为什么不选择 VuePress@next,考虑到还处于 Beta 阶段,等稳定后再开始迁移。

1. 本地搭建

快速开始同 VuePress 官网

  1. 创建并进入一个新目录

    1. // 文件名自定义
    2. mkdir vuepress-starter && cd vuepress-starter
  2. 使用你喜欢的包管理器进行初始化

    1. yarn init # npm init
  3. 将 VuePress 安装为本地依赖

    1. yarn add -D vuepress # npm install -D vuepress
  4. 创建你的第一篇文档,VuePress 会以 docs 为文档根目录,所以这个 README.md 相当于主页:

    1. mkdir docs && echo '# Hello VuePress' > docs/README.md
  5. 在 package.json 中添加一些 scripts

    1. {
    2. "scripts": {
    3. "docs:dev": "vuepress dev docs",
    4. "docs:build": "vuepress build docs"
    5. }
    6. }
  6. 在本地启动服务器

    1. yarn docs:dev # npm run docs:dev

    VuePress 会在 http://localhost:8080 (opens new window) 启动一个热重载的开发服务器。

2. 基础配置

在文档目录下创建一个 .vuepress 目录,所有 VuePress 相关的文件都会被放在这里。此时你的项目结构可能是这样:

  1. .
  2. ├─ docs
  3. ├─ README.md
  4. └─ .vuepress
  5. └─ config.js
  6. └─ package.json

.vuepress 文件夹下添加 config.js,配置网站的标题和描述,方便 SEO:

  1. module.exports = {
  2. title: 'TypeScript4 文档',
  3. description: 'TypeScript4 最新官方文档翻译'
  4. }

此时界面类似于:
image.png

3. 添加导航栏

我们现在在页首的右上角添加导航栏,修改 config.js:

  1. module.exports = {
  2. title: '...',
  3. description: '...',
  4. themeConfig: {
  5. nav: [
  6. { text: '首页', link: '/' },
  7. {
  8. text: '冴羽的 JavaScript 博客',
  9. items: [
  10. { text: 'Github', link: 'https://github.com/mqyqingfeng' },
  11. { text: '掘金', link: 'https://juejin.cn/user/712139234359182/posts' }
  12. ]
  13. }
  14. ]
  15. }
  16. }

效果如下:
image.png
更多的配置参考 VuePress 导航栏

4. 添加侧边栏

现在我们添加一些 md 文档,目前文档的目录如下:

  1. .
  2. ├─ docs
  3. ├─ README.md
  4. └─ .vuepress
  5. └─ config.js
  6. | └─ handbook
  7. | └─ ConditionalTypes.md
  8. | └─ Generics.md
  9. └─ package.json

我们在 config.js 配置如下:

  1. module.exports = {
  2. themeConfig: {
  3. nav: [...],
  4. sidebar: [
  5. {
  6. title: '欢迎学习',
  7. path: '/',
  8. collapsable: false, // 不折叠
  9. children: [
  10. { title: "学前必读", path: "/" }
  11. ]
  12. },
  13. {
  14. title: "基础学习",
  15. path: '/handbook/ConditionalTypes',
  16. collapsable: false, // 不折叠
  17. children: [
  18. { title: "条件类型", path: "/handbook/ConditionalTypes" },
  19. { title: "泛型", path: "/handbook/Generics" }
  20. ],
  21. }
  22. ]
  23. }
  24. }

对应的效果如下:
image.png

5. 更换主题

现在基本的目录和导航功能已经实现,但如果我还想要加载 loading、切换动画、模式切换(暗黑模式)、返回顶部、评论等功能呢,为了简化开发成本,我们可以直接使用主题,这里使用的主题是 vuepress-theme-rec

现在我们安装 vuepress-theme-reco:

  1. npm install vuepress-theme-reco --save-dev
  2. # or
  3. yarn add vuepress-theme-reco

然后在 config.js 里引用该主题:

  1. module.exports = {
  2. // ...
  3. theme: 'reco'
  4. // ...
  5. }

刷新一下页面,我们会发现一些细节的改变,比如加载时的 loading 动画、以及支持切换暗黑模式:
image.png

6. 添加文章信息

但我们也会发现,像条件类型这一篇文章,条件类型(Conditional Types) 竟然出现了两遍,这是因为这个主题自动提取了第一个大标题作为本文的标题,我们可以在每篇文章的 md 文件中添加一些信息修改:

  1. ---
  2. title: 条件类型
  3. author: 冴羽
  4. date: '2021-12-12'
  5. ---

此时文章的效果如下:
image.png

但如果你不想要标题、作者、时间这些信息呢,我们可以在样式里隐藏,这个稍后会讲到。

7. 设置语言

注意,上图的文章时间,我们写入的格式为 2021-12-12 ,但是显示的是 12/12/2021,这是因为 VuePress 默认的 lang 为 en-US,我们修改一下 config.js:

  1. module.exports = {
  2. // ...
  3. locales: {
  4. '/': {
  5. lang: 'zh-CN'
  6. }
  7. },
  8. // ...
  9. }

可以发现时间换了一种展示方式:
image.png

8. 开启目录结构

在原本的主题里,我们发现每篇文章的目录结构出现在左侧:
image.png

而 vuepress-theme-reco 将原有的侧边栏的中的多级标题移出,生成子侧边栏,放在了页面的右侧,如果你要全局开启,可在页面 config.js 里设置开启:

  1. module.exports = {
  2. //...
  3. themeConfig: {
  4. subSidebar: 'auto'
  5. }
  6. //...
  7. }

此时效果如下:
image.png

9. 修改主题颜色

VuePress 基于 Vue,所以主题色用的是 Vue 的绿色,然而 TypeScript 的官方色则是蓝色,那如何修改 VuePress 的主题色呢?

你可以创建一个 .vuepress/styles/palette.styl 文件,文件代码如下:

  1. $accentColor = #3178c6

此时可以发现主题颜色变了:
image.png

更多的颜色修改参考 VuePress 的 palette.styl

10. 自定义修改样式

如果你想自定义修改一些 DOM 元素的样式呢?就比如在暗黑模式下:

image.png

我们发现用作强调的文字颜色比较暗淡,在暗黑模式下看不清楚,我想改下这个文字的颜色和背景色呢?

而 VuePress 提供了一种添加额外样式的简便方法。你可以创建一个 .vuepress/styles/index.styl 文件。这是一个 Stylus 文件,但你也可以使用正常的 CSS 语法。

我们在 .vupress 文件夹下创建这个目录,然后创建 index.styl 文件,代码如下:

  1. // 通过检查,查看元素样式声明
  2. .dark .content__default code {
  3. background-color: rgba(58,58,92,0.7);
  4. color: #fff;
  5. }

此时文字就清晰了很多:
image.png

那之前我们提到的隐藏每篇文章的标题、作者、时间呢,其实也是类似的方式:

  1. .page .page-title {
  2. display: none;
  3. }

最后的效果如下:
image.png

11. 部署

我们的博客就算是正式的做好了,接下来我们部署到免费的 Github Pages 上。我们在 Github 上新建一个仓库,这里我取得仓库名为:learn-typescript
image-2.png

对应,我们需要在 config.js 添加一个 base 路径配置:

  1. module.exports = {
  2. // 路径名为 "/<REPO>/"
  3. base: '/learn-typescript/',
  4. //...
  5. }

最终的 config.js 文件内容为:

  1. module.exports = {
  2. title: 'TypeScript4 文档',
  3. description: 'TypeScript4 最新官方文档翻译',
  4. base: '/learn-typescript/',
  5. theme: 'reco',
  6. locales: {
  7. '/': {
  8. lang: 'zh-CN'
  9. }
  10. },
  11. themeConfig: {
  12. // lastUpdated: '上次更新',
  13. subSidebar: 'auto',
  14. nav: [
  15. { text: '首页', link: '/' },
  16. {
  17. text: '冴羽的 JavaScript 博客',
  18. items: [
  19. { text: 'Github', link: 'https://github.com/mqyqingfeng' },
  20. { text: '掘金', link: 'https://juejin.cn/user/712139234359182/posts' }
  21. ]
  22. }
  23. ],
  24. sidebar: [
  25. {
  26. title: '欢迎学习',
  27. path: '/',
  28. collapsable: false,
  29. children: [
  30. { title: "学前必读", path: "/" }
  31. ]
  32. },
  33. {
  34. title: "基础学习",
  35. path: '/handbook/ConditionalTypes',
  36. collapsable: false,
  37. children: [
  38. { title: "条件类型", path: "/handbook/ConditionalTypes" },
  39. { title: "泛型", path: "/handbook/Generics" }
  40. ],
  41. }
  42. ]
  43. }
  44. }

然后我们在项目 vuepress-starter 目录下建立一个脚本文件:deploy.sh,注意修改一下对应的用户名和仓库名:

  1. #!/usr/bin/env sh
  2. # 确保脚本抛出遇到的错误
  3. set -e
  4. # 生成静态文件
  5. npm run docs:build
  6. # 进入生成的文件夹
  7. cd docs/.vuepress/dist
  8. git init
  9. git add -A
  10. git commit -m 'deploy'
  11. # 如果发布到 https://<USERNAME>.github.io/<REPO>
  12. git push -f git@github.com:mqyqingfeng/learn-typescript.git master:gh-pages
  13. cd -

然后命令行切换到 vuepress-starter 目录下,执行 sh deploy.sh,就会开始构建,然后提交到远程仓库,注意这里提交到了 gh-pages 分支,我们查看下对应仓库分支的代码:
image.png

我们可以在仓库的 Settings -> Pages 中看到最后的地址:
image.png

像我最后生成的地址就是 https://mqyqingfeng.github.io/learn-typescript/

至此,我们完成了 VuePress 和 Github Pages 的部署。

系列文章

系列文章目录地址:https://github.com/mqyqingfeng/Blog

微信:「mqyqingfeng」,加我进冴羽唯一的读者群。

如果有错误或者不严谨的地方,请务必给予指正,十分感谢。如果喜欢或者 有所启发,欢迎 star,对作者也是一种鼓励。