【教程】使用vuepress构建静态文档网站,并部署到github上

桃扇骨 2024-02-22 09:49 83阅读 0赞

官网

  • 快速上手 | VuePress (vuejs.org)

构建项目

我们跟着官网的教程先构建一个demo

这里我把 vuepress-starter 这个项目名称换成了 howtolive

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

    mkdir howtolive && cd howtolive

  2. 使用你喜欢的包管理器进行初始化

    yarn init

image.png
这里的问题可以一路回车

  1. 将 VuePress 安装为本地依赖

我们已经不再推荐全局安装 VuePress

  1. yarn add -D vuepress

注意:如果你的现有项目依赖了 webpack 3.x,我们推荐使用 Yarn (opens new window)而不是 npm 来安装 VuePress。因为在这种情形下,npm 会生成错误的依赖树。
注意: 不要在后面添加:# npm install -D vuepress

  1. 创建你的第一篇文档

    mkdir docs && echo ‘# Hello VuePress’ > docs/README.md

  2. package.json 中添加一些 scripts(opens new window)

这一步骤是可选的,但我们推荐你完成它。在下文中,我们会默认这些 scripts 已经被添加。

  1. {
  2. "scripts": {
  3. "docs:dev": "vuepress dev docs",
  4. "docs:build": "vuepress build docs"
  5. }
  6. }

如下图所示
image.png

  1. 在本地启动服务器

    yarn docs:dev

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

项目启动之后就是这样
image.png

目录结构

VuePress 遵循 “约定优于配置” 的原则,推荐的目录结构如下:

  1. .
  2. ├── docs
  3. ├── .vuepress (可选的)
  4. ├── public (存放网站图标等信息)
  5. ├── styles
  6. └── palette.styl (网站自定义样式)
  7. └── config.js (路由,图标等的配置文件)
  8. ├── README.md
  9. ├── guide
  10. └── README.md (具体文章博客)
  11. └── package.json

如下图所示
image.png

配置网站标题和图标

配置文件

如果没有任何配置,这个网站将会是非常局限的,用户也无法在你的网站上自由导航。为了更好地自定义你的网站,让我们首先在你的文档目录下创建一个 .vuepress 目录,所有 VuePress 相关的文件都将会被放在这里。你的项目结构可能是这样:

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

一个 VuePress 网站必要的配置文件是 .vuepress/config.js,它应该导出一个 JavaScript 对象:

  1. module.exports = {
  2. title: 'How to live',
  3. description: '让我们一起学习如何生活',
  4. head: [['link', {
  5. rel: 'icon', href: `favicon.ico` }]],
  6. }

把ico放在public下面
image.png

项目启动之后,如下之后:

image.png

默认主题配置

  • 默认主题配置 | VuePress (vuejs.org)

首页

  1. ---
  2. home: true
  3. heroText: How to live
  4. tagline: 让我们一起学习如何生活吧
  5. actionText: 开始
  6. actionLink: /
  7. features:
  8. - title: 生理健康
  9. details: 了解自己的身体,养成良好的作息生活习惯
  10. - title: 心理健康
  11. details: 了解不同的文化,形成完善的认知观念
  12. ---

image.png

注意需要重新编译后,才会看到效果

image.png

导航栏

在config.js中配置

  1. themeConfig: {
  2. // 主题设置
  3. logo: '/logo.png',
  4. nav: [{
  5. // 右上导航航条 docs/.vuepress 文件夹下
  6. text: '首页',
  7. link: '/',
  8. }, {
  9. text: '生理健康',
  10. items: [
  11. {
  12. text: '了解身体结构', link: '/' }, // 可不写后缀 .md
  13. {
  14. text: '养成良好生活习惯', link: 'https://www.baidu.com/' }// 外部链接
  15. ]
  16. }, {
  17. text: '心理健康',
  18. link: '/'
  19. }],
  20. }

image.png

image.png

侧边栏

  1. themeConfig: {
  2. // 主题设置
  3. logo: '/logo.png',
  4. nav: [{
  5. // 右上导航航条 docs/.vuepress 文件夹下
  6. text: '首页',
  7. link: '/',
  8. }, {
  9. text: '生理健康',
  10. items: [
  11. {
  12. text: '人体构造', link: '/生理健康/人体构造/通用/1肌肉' }, // 可不写后缀 .md
  13. {
  14. text: '生活习惯', link: 'https://www.baidu.com/' }// 外部链接
  15. ]
  16. }, {
  17. text: '心理健康',
  18. link: '/'
  19. }],
  20. sidebar: {
  21. //左侧列表
  22. '/生理健康/人体构造/': [{
  23. title: '人体构造',
  24. collapsable: true,
  25. children: [{
  26. title: '肌肉',
  27. path: '通用/1肌肉'
  28. },{
  29. title: '眼睛',
  30. path: '通用/2眼睛'
  31. }]
  32. },{
  33. title: '男性',
  34. collapsable: true,
  35. children: [{
  36. title: '喉结',
  37. path: '/男性/3喉结'
  38. }]
  39. }],
  40. '/': [''], //不能放在数组第一个,否则会导致右侧栏无法使用
  41. },
  42. // 左侧列表展开级数,默认是 1
  43. sidebarDepth: 2,
  44. }

image.png

image.png

image.png

自定义样式

image.png

改变主题颜色

  1. $accentColor = #378ae2

改变文章两侧空白宽度

  1. .page .theme-default-content:not(.custom){
  2. max-width: none;
  3. }

部署到github

部署 | VuePress (vuejs.org)

在根目录下建一个depoy.sh
image.png

  1. #!/usr/bin/env sh
  2. # 确保脚本抛出遇到的错误
  3. set -e
  4. # 生成静态文件
  5. npm run docs:build
  6. # 进入生成的文件夹
  7. cd docs/.vuepress/dist
  8. # 如果是发布到自定义域名
  9. # echo 'www.example.com' > CNAME
  10. git init
  11. git branch -m master main
  12. git add -A
  13. git commit -m 'deploy'
  14. # 如果发布到 https://<USERNAME>.github.io
  15. # git push -f git@github.com:<USERNAME>/<USERNAME>.github.io.git main
  16. # 如果发布到 https://<USERNAME>.github.io/<REPO>
  17. git push -f git@github.com:ni'de.git main
  18. cd -

config.js也要修改
image.png

然后双击deploy.sh运行
image.png

或者在gitbash中打开

然后dist目录下生成打包好的项目,并同步到gihub
image.png

image.png

配置github

image.png

然后就可以通过github来访问静态网站了

image.png

发表评论

表情:
评论列表 (有 0 条评论,83人围观)

还没有评论,来说两句吧...

相关阅读

    相关 在 IIS 构建静态网站

    本文档将指导您完成安装 IIS Web 服务器并将其配置为提供静态内容的过程。静态内容是一个网页 (HTML),它完全按照存储的方式交付给用户。相比之下,动态内容由 Web 应