hexo 搭建静态博客
写在前面
本博客呢就是使用 hexo 搭建的 静态博客,部署在 github 站点,hexo官网是 https://hexo.io/zh-cn/,使用了zhaoo这款主题,这款主题的开源地址是 https://github.com/izhaoo/hexo-theme-zhaoo 。
原理
使用 markdown语法编写一篇文章,hexo将 markdown 文件渲染成 html 页面和资源文件,把这些文件上传到 github 仓库,开启 github pages 服务就可以访问了,所以,其本质就是渲染出静态页面。
环境准备
在 D 盘新建 hexo 文件夹来安装hexo服务 ----->
在安装hexo服务之前确保有 node 和 npm 环境。
如果没有 node 和 npm 可以参考:https://www.runoob.com/nodejs/nodejs-install-setup.html
安装 Hexo 服务
打开CMD 进入 D 盘的 hexo 目录,输入命令: npm install hexo-cli -g
接下来输入命令:hexo init blog 进行初始化。
初始化完成后可以看到:
CMD 进入 D:\hexo\blog 目录输入 npm install 安装项目依赖。
继续输入 hexo server 命令即可启动服务,也可以简写成 hexo s
打开浏览器访问 localhost:4000 可以看到:
我们可以看到,这个默认的主题不太好看呀,于是我们准备换一个漂亮的主题:
CMD 进入 D:\hexo\blog\themes 目录,输入如下命令下载主题:git clone https://github.com/izhaoo/hexo-theme-zhaoo.git zhaoo 输入这个命令前请确保有 Git 环境(相信你明白我说的啥吧),如果实在没有 Git 环境,可以在 Github 网站上下载后解压到 D:\hexo\blog\themes 目录,若果 Github 下载太慢的话可以使用 http://g.widyun.com/ 此网站下载。
进入 D:\hexo\blog 目录打开并编辑 _config.yml 文件,加入以下配置启用 zhaoo 主题
然后再对 D:\hexo\blog 的 _config.yml 文件和 D:\hexo\blog\themes\zhaoo 目录下 _config.yml进行定制化配置:
D:\hexo\blog_config.yml:
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site
title: 一只小安仔
subtitle:
description:
keywords:
author: junan
language:
timezone:
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://junanchen.github.io/
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks
# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:
# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
enable: true # Open external links in new tab
field: site # Apply to the whole site
exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
path: ''
per_page: 9
order_by: -date
highlight:
enable: true
line_number: true
auto_detect: true
hljs: true
tab_replace:
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true
# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
## Use post's date for updated date unless set in front-matter
use_date_for_updated: false
# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page
# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: zhaoo
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
# repo: https://gitee.com/junanaa/blog.git
repo: https://github.com/JunanChen/junanchen.github.io.git
branch: master
D:\hexo\blog\themes\zhaoo_config.yml :
# --------------------------------------------------
# Site settings
# --------------------------------------------------
# Color
color:
default: "#33333D"
link: "#FFBE5B"
# Favicon
favicon:
small: /images/icons/favicon-16x16.png
medium: /images/icons/favicon-32x32.png
apple_touch_icon: /images/icons/apple-touch-icon.png
safari_pinned_tab: /images/icons/stun-logo.svg
msapplication: /images/icons/favicon-144x144.png
# Keywords
keywords: 一只小安仔
# Preview
preview:
enable: true
model: wave #cloud
welcome_image: /images/theme/background.jpg
motto:
default: 夜半花落听雨声,不自欢喜不自愁。
api: #'http://localhost:3000/api/motto'
# Menu
menu:
home: / || 首页
# galleries: /galleries || 摄影
archives: /archives || 归档
tags: /tags || 标签
categories: /categories || 分类
about: /about || 关于
# Copyright
copyright: <p>Copyright© 2019-2020 | <a target="_blank" href="http://junanaa.gitee.io/">一只小安仔</a> .AllRightsReserved</p>
# Social
social:
qq: tencent://message/?Menu=yes&uin=1286890175 || iconQQ || '#12B7F5'
wechat: javascript:; || iconwechat-fill || '#09BB07'
# instagram: https://www.instagram.com/izhaoo/ || iconinstagram || '#DA2E76'
# github: https://github.com/izhaoo || icongithub-fill || '#24292E'
email: mailto:chenjunan_2018@163.com || iconmail
# Fab (FloatActionButton)
fab:
enable: true
alwaysShow: false
# --------------------------------------------------
# Post settings
# --------------------------------------------------
# Post image
post_image: /images/theme/post-image.jpg
# Donate
donate:
alipay:
wechat:
# --------------------------------------------------
# Page settings
# --------------------------------------------------
# Galleries
galleries:
enable: true
title: 摄影
subtitle: Photography
# --------------------------------------------------
# Comment settings
# --------------------------------------------------
comments:
enable: true
type: valine
button: true
# Gitalk
gitalk:
owner:
repo:
admin:
clientID:
clientSecret:
# Valine
valine:
appId: a8aBoMUoOnc1VQB8luHmCH4Y-gzGzoHsz
appKey: EEUcoQEF0g8aC9y8tqj1koxE
placeholder: 大爷来玩呀~
avatar: mp
pageSize: 10
lang: zh-CN
# ChangYan
changyan:
appId:
appKey:
# --------------------------------------------------
# Plugin settings
# --------------------------------------------------
# Lazyload
lazyload:
enable: true
onlyPost: false
loadingImage: /images/theme/loading.gif
# LeanCloud
leancloud:
enable: false
appId:
appKey:
serverURLs:
# FancyBox
fancybox: true
# Pjax
pjax: false
# HighLight
highlight: true
# --------------------------------------------------
# Script settings
# --------------------------------------------------
# CDN
vendors:
jquery_js: https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
fancybox_js: https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.js
pjax_js: https://cdn.bootcss.com/jquery.pjax/2.0.1/jquery.pjax.min.js
lazyload_js: https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js
justifiedGallery_js: https://cdn.bootcdn.net/ajax/libs/justifiedGallery/3.7.0/js/jquery.justifiedGallery.min.js
fancybox_css: https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.css
highlight_css: https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css
iconfont_css: //at.alicdn.com/t/font_1445822_58xq2j9v1id.css
justifiedGallery_css: https://cdn.bootcdn.net/ajax/libs/justifiedGallery/3.7.0/css/justifiedGallery.min.css
# SEO
seo:
baidu_auto_push: true
# Analytics
analytics:
baidu: #id
google: #id
tencent: #id
cnzz: #id
# Custom
custom:
head:
foot:
css:
js:
可以在 D:\hexo\blog 目录下重启 hexo s 启动。
上传 Github 静态博客
想放在 Github 上供其他人访问,就需要上传 Github ,如果考虑 Github 访问太慢,想上传 Gitee 也是可以的,但上传 Gitee 好像会有些坑,可以自己去尝试。
新建仓库
修改配置文件
修改 D:\hexo\blog 下的 _config.yml 文件,确保 repo 配置为你自己仓库的地址。
CMD 打开 D:\hexo\blog 输入以下命令:hexo g(即hexo generat) 编译生成静态文件
最后输入 hexo d (即 hexo deploy) 命令上传到仓库:(这个过程需要Git环境哦,你知道我在说啥吧)
期间可能会让你输入 Github 的账号和密码哟!
进入仓库可以看到文件以及上传:
开启 Github pages 服务
点击 Setting
往下翻找到 Github Pages ,开启服务即可。
访问这个网址即可看到自己的博客:
其他插件
我这里用到了 Valine 评论插件 ,具体配置可参考: https://blog.csdn.net/blue_zy/article/details/79071414