这次给大家带来使用vuepress搭建个人博客步骤详解(附代码),使用vuepress搭建个人博客的注意事项有哪些,下面就是实战案例,一起来看一下。
vuepress
vuepress是尤大大4月12日发布的一个全新的基于vue的静态网站生成器,实际上就是一个vue的spa应用,内置webpack,可以用来写文档。
一个基于 vue ssr 的静态站生成器,本来的目的是爽爽的写文档,但是我发现用来撸一个人博客也非常不错。
这是vuepress的官方文档
上手搭建
你可以跟着文档上的例子自己玩一玩,不过由于vuepress的文档也是用vuepress来实现的,所以我取巧直接拿vuepress仓库中的docs目录拿来玩耍。
1.首先安装vuepress到全局
npm install -g vuepress
2.然后把vuepress仓库克隆到你的电脑
git clone git@github.com:docschina/vuepress.git
在docs文件中执行(请确保你的 node.js 版本 >= 8)
cd vuepress
cd docs
vuepress dev
当你看到这一行就说明已经成功了:
vuepress dev server listening at http://localhost:8080/
下面我们打开http://localhost:8080/
发现真的打开了vuepress文档:
下面的工作就是数据的替换了,但我们应该先看一下docs的目录结构:
├─.vuepress
│ ├─components
│ └─public
│ └─icons
│ └─config.js // 配置文件
├─config // vuepress文档的配置参考内容
├─default-theme-config // vuepress文档的默认主题配置内容
├─guide // vuepress文档的指南内容
└─zh // 中文文档目录
├─config
├─default-theme-config
└─guide
└─readme.md // 首页配置文件
文档分成了两部分,中文文档在/zh/目录下,英文文档在根目录下。
其实目录里面的东西都挺好看懂的,首先guide 、default-theme-config、config 这三个目录中的都是vuepress文档的主要内容,从中文文档里也可以看到只有这三个目录被替换了。
首页配置
默认主题提供了一个主页布局,要使用它,需要在你的根目录 readme.md 的 yaml front matter 中指定 home:true,并加上一些其他的元数据。
我们先看看根目录下的readme,md:
home: true // 是否使用vuepress默认主题
heroimage: /hero.png // 首页的图片
actiontext: get started → // 按钮的文字
actionlink: /guide/ // 按钮跳转的目录
features: // 首页三个特性
- title: simplicity first
details: minimal setup with markdown-centered project structure helps you focus on writing.
- title: vue-powered
details: enjoy the dev experience of vue + webpack, use vue components in markdown, and develop custom themes with vue.
- title: performant
details: vuepress generates pre-rendered static html for each page, and runs as an spa once a page is loaded.
footer: mit licensed | copyright © 2018-present evan you // 页尾
实在看不懂,官网有比我更详细的配置说明。
导航配置
导航配置文件在.vuepress/config.js中
在导航配置文件中nav是控制导航栏链接的,你可以把它改成自己的博客目录。
nav: [
{
text: 'guide',
link: '/guide/',
},
{
text: 'config reference',
link: '/config/'
},
{
text: 'default theme config',
link: '/default-theme-config/'
}
]
剩下的默认主题配置官方文档都有很详细的文档说明这里就不在啰嗦了。
更改默认主题色
你可以在.vuepress/目录下创建一个override.styl文件。
vuepress提供四个可更改的颜色:
$accentcolor = #3eaf7c // 主题色
$textcolor = #2c3e50 // 文字颜色
$bordercolor = #eaecef // 边框颜色
$codebgcolor = #282c34 // 代码背景颜色
我把它改成了这样:
侧边栏的实现
由于评论区里问的人较多,所以在这里更新一下,其实我就算在这里写的再详细也不如大家去看官方文档。
侧边栏的配置也在.vuepress/config.js中:
sidebar: [
{
title: 'javascript', // 侧边栏名称
collapsable: true, // 可折叠
children: [
'/blog/javascript/学会了es6,就不会写出那样的代码', // 你的md文件地址
]
},
{
title: 'css',
collapsable: true,
children: [
'/blog/css/搞懂z-index的所有细节',
]
},
{
title: 'http',
collapsable: true,
children: [
'/blog/http/认识http-cookie和session篇',
]
},
]
对应的文档结构:
├─blog // docs目录下新建一个博客目录
│ ├─css
│ ├─http
│ └─javascript
我的博客:brownhu
部署
在配置好你博客之后,命令行执行:
vuepress build
当你看到这一行就说明成功了:
success! generated static files in vuepress.
将打包好的vuepress目录上传到你的github仓库,和github page配合,就可以配置好你的博客网站了。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue实现移动端微信公众号步骤详解
jquery实现点击标题文字切换字体步骤详解
以上就是使用vuepress搭建个人博客步骤详解(附代码)的详细内容。
