如需转载,请根据 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 许可,附上本文作者及链接。
本文作者: 执笔成念
作者昵称: zbcn
本文链接: https://1363653611.github.io/zbcn.github.io/2019/12/05/hexo_createBlog_01/
基于Hexo+Github 搭建个人博客
node.js
环境安装
- 下载地址 :https://nodejs.org/zh-cn/download/
- 这里选择
windows64位.msi格式安装包
- 这里选择
- 安装 hexo
npm i -g hexo-cli
安装 yilia 主题的坑
头像设置
- 在
source\assets\image添加图片 - 在
themes\yilia\_config.yml配置头像 和favicon:notify: 根目录下的url 配置为博客地址:https://1363653611.github.io/zbcn.github.io, 要配置1favicon: /zbcn.github.io/assets/image/favicon.ico2#你的头像url3avatar: /zbcn.github.io/assets/image/head.jpgzbcn.github.io,否则会引发一堆问题
- 在
点击 所有文章/友联/关于我 出现
habout:blank#blocked页面- 解决方案:
- 找到
themes\yilia\layout\_partial\left-col.ejs文件 - 找到
<nav class="header-smart-menu">标签 - 个里面的a标签,加上一个
target="_parent"标签,修改后的标签是1<nav class="header-smart-menu">2<% for (var i in theme.smart_menu){ %>3<% if(theme.smart_menu[i]){ %>4<a q-on="click: openSlider(e, '<%-i%>')" href="javascript:void(0)" target="_parent"><%= theme.smart_menu[i] %></a>5<% } %>6<%}%>7</nav>
- 找到
- 解决方案:
添加 访问量统计1(效果不是很好)添加本站总访问量(引入不蒜子):(pv的方式,单个用户连续点击n篇文章,记录n次访问量。)
在themes/yilia/layout/_partial/footer.ejs的末尾 添加1<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>2<span id="busuanzi_container_site_pv">3本站总访问量<span id="busuanzi_value_site_pv"></span>次4</span>本站访客数:
1//uv的方式,单个用户连续点击n篇文章,只记录1次访客数。2<span id="busuanzi_container_site_uv">3本站访客数<span id="busuanzi_value_site_uv"></span>人次4</span>
在
themes/yilia/layout/_partial/article.ejs下添加12<% if (!index){ %>3<span id="busuanzi_container_page_pv">4⋉(●本文总阅读量 <span id="busuanzi_value_page_pv"></span> 次●)⋊5</span>6<% } %>访问量统计2:
网站访问量显示:
- 我使用了不蒜子第三方的统计插件,网址:http://ibruce.info/2015/04/04/busuanzi/
- 在
themes\yilia\layout\_partial下的footer.ejs中加入如下代码即可实现单篇文章浏览统计和评论统计1<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">2</script>3<span id="busuanzi_container_site_pv">4本站总访问量<span id="busuanzi_value_site_pv"></span>次5</span>6<span id="busuanzi_container_site_uv">7总访客数<span id="busuanzi_value_site_uv"></span>人次8</span> - 评论数的统计是网易云跟帖中获取的,下面给出
- 修改
themes\yilia\layout\_partial文件夹下的article.ejs文件 - 在
<%- partial('post/title', {class_name: 'article-title'}) %>节点下加入:- 注意这里网易云跟帖还没设置,而评论数中使用到了,这里运行会有问题,下面给出
1<!-- 显示阅读和评论数 -->2<% if (!index && post.comments && config.wangYi){ %>3<br/>4<a class="cloud-tie-join-count" href="javascript:void(0);"target="_parent" style="color:gray;font-size:14px;">5<span class="icon-sort"></span>6<span id="busuanzi_container_page_pv" style="color:#ef7522;font-size:14px;">7阅读数: <span id="busuanzi_value_page_pv"></span>次 8</span>9</a>10<a class="cloud-tie-join-count" href="javascript:void(0);" target="_parent" style="color:#ef7522;font-size:14px;">11<span class="icon-comment"></span>12<span class="join-text" style="color:#ef7522;font-size:14px;">评论数:</span>13<span class="join-count">0</span>次14</a>15<% } %>实现网易云跟帖评论
- 注册账号:https://gentie.163.com/info.html
- 注意这里网易云跟帖还没设置,而评论数中使用到了,这里运行会有问题,下面给出
配置置顶文章
- 安装插件
1npm uninstall hexo-generator-index --save2npm install hexo-generator-index-pin-top --save - 配置置顶标准
- 打开:
/themes/*/layout(/_macro)/post.ejs - 直接在最前面加入以下代码即可
1<% if (page.top) { %>2<i class="fa fa-thumb-tack"></i>3<font color=7D26CD>置顶</font>4<span class="post-meta-divider">|</span>5<% } %>
- 打开:
- 配置文章
然后在需要置顶的文章的Front-matter中加上top选项即可
top后面的数字越大,优先级越高1title: 20192date: 2019-02-14 16:10:033top: 5 - 优先级配置
修改根目录配置文件/_config.yml,top值-1标示根据top值倒序(正序设置为1即可),同样date也是根据创建日期倒序。
Hexo 实现私密文章加密
1 | cd /Hexo |
2 | npm install hexo-blog-encrypt |
3 | |
4 | vim /Hexo/_config.yml 添加如下内容 |
5 | |
6 | # Security |
7 | ## 文章加密 hexo-blog-encrypt |
8 | encrypt: |
9 | enable: true |
10 | |
11 | 然后在想加密的文章头部添加上对应字段,如 |
12 | |
13 | --- |
14 | title: hello world |
15 | date: 2016-03-30 21:18:02 |
16 | tags: |
17 | password: 12345 (密码) |
18 | abstract: Welcome to my blog, enter password to read. |
19 | message: Welcome to my blog, enter password to read. |
20 | --- |
21 | |
22 | password: 是该博客加密使用的密码 |
23 | abstract: 是该博客的摘要,会显示在博客的列表页 |
24 | message: 这个是博客查看时,密码输入框上面的描述性文字 |
增加版权声明
配置yilia
- 位置
themes/yilia/layout/_partial/article.ejs - 中标注的位置添加代码
1<div class="article-entry" itemprop="articleBody">2<% if (post.excerpt && index){ %>3<%- post.excerpt %>4<% if (theme.excerpt_link) { %>5<a class="article-more-a" href="<%- url_for(post.path) %>#more"><%= theme.excerpt_link %> >></a>6<% } %>7<% } else { %>8<%- post.content %>9<% } %>10<-- 在此处添加代码-->11<% if ((theme.reward_type === 2 || (theme.reward_type === 1 && post.reward)) && !index){ %>12<div class="page-reward">13<a href="javascript:;" class="page-reward-btn tooltip-top">14<div class="tooltip tooltip-east"> - 添加的代码如下
1<!-- 增加版权声明 -->2<%3var sUrl = url.replace(/index\.html$/, '');4sUrl = /^(http:|https:)\/\//.test(sUrl) ? sUrl : 'https:' + sUrl;5%>6<% if ((theme.declare_type === 2 || (theme.declare_type === 1 && post.declare)) && !index){ %>7<div class="declare">8<strong>本文作者:</strong>9<% if(config.author != undefined){ %>10<%= config.author%>11<% }else{%>12<font color="red">请在博客根目录“_config.yml”中填入正确的“author”</font>13<%}%>14<br>15<strong>本文链接:</strong>16<a rel="license" href="<%=sUrl%>"><%=sUrl%></a>17<br>18<strong>版权声明:</strong>19本作品采用20<a rel="license" href="<%= theme.licensee_url%>"><%= theme.licensee_name%></a>21进行许可。转载请注明出处!22<% if(theme.licensee_img != undefined){ %>23<br>24<a rel="license" href="<%= theme.licensee_url%>"><img alt="知识共享许可协议" style="border-width:0" src="<%= theme.licensee_img%>"/></a>25<% } %>26</div>27<% } else {%>28<div class="declare" hidden="hidden"></div>29<% } %>创建新文件
themes/yilia/source-src/css/declare.scss并添加如下CSS代码。
1.declare {2background-color: #eaeaea;3margin-top: 2em;4border-left: 3px solid #ff1700;5padding: .5em 1em;6}为
themes/yilia/source-src/css/main.scss添加@import "./declare";
- 位置
配置显示
- 修改为
themes/yilia/_config.yml
添加:1版权基础设定:0-关闭声明; 1-文章对应的md文件里有declare: true属性,才有版权声明; 2-所有文章均有版权声明2#当前应用的版权协议地址。3#版权协议的名称4#版权协议的Logo5declare_type: 16licensee_url: https://creativecommons.org/licenses/by-nc-sa/4.0/7licensee_name: '知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议'8licensee_img: https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png - 然后在需要进行版权声明的文章的md文件头部,设置属性
declare:true
- 修改为
在左侧显示总文章数
将themes\yilia\layout_partial\left-col.ejs文中的
1 | <nav class="header-menu"> |
2 | <ul> |
3 | <% for (var i in theme.menu){ %> |
4 | <li><a href="<%- url_for(theme.menu[i]) %>"><%= i %></a></li> |
5 | <%}%> |
6 | </ul> |
7 | </nav> |
后面加上
1 | <nav> |
2 | 总文章数 <%=site.posts.length%> |
3 | </nav> |
文章顶部转载说明
- 配置yilia主题文件
在themes/yilia/layout/_partial/article.ejs中下面标注的位置添加代码
`<% if (!post.noDate){ %>
<% } %><%- partial('post/date', {class_name: 'archive-article-date', date_format: null}) %> <% } %> </header>然后在需要进行版权声明的文章的md文件头部,设置属性 `topdeclare:true`1添加的代码如下2```html3<!-- 文章头增加转载声明 -->4<%5var sUrl = url.replace(/index\.html$/, '');6sUrl = /^(http:|https:)\/\//.test(sUrl) ? sUrl : 'https:' + sUrl;7%>8<% if ((theme.topdeclare_type === 2 || (theme.topdeclare_type === 1 && post.topdeclare)) && !index){ %>9<div class="topdeclare">10<hr>11<strong>如需转载,请根据</strong>12<a rel="license" href="<%= theme.toplicensee_url%>"><%= theme.toplicensee_name%></a>13许可,附上本文作者及链接。14<br>15<strong>本文作者:</strong>16<% if(config.author != undefined){ %>17<%= config.author%>18<% }else{%>19<font color="red">请在博客根目录“_config.yml”中填入正确的“author”</font>20<%}%>21<br>22<strong>作者昵称:</strong>23<% if(theme.topnickname!= undefined){ %>24<%= theme.topnickname%>25<% }else{%>26<font color="red">请在博客主题目录“_config.yml”中填入正确的“昵称”</font>27<%}%>28<br>29<strong>本文链接:</strong>30<a rel="license" href="<%=sUrl%>"><%=sUrl%></a>31<br>32<hr>33</div>34<% } else {%>35<div class="topdeclare" hidden="hidden"></div>36<% } %>37<!-- 文章头增加转载声明结束 -->38```39创建新文件`themes/yilia/source-src/css/topdeclare.scss`40并添加如下CSS代码。41```css42.declare {43background-color: #eaeaea;44margin-top: 2em;45border-left: 3px solid #ff1700;46padding: .5em 1em;47}48```4950为`themes/yilia/source-src/css/main.scss` 添加如下代码:`@import "./topdeclare";`5152然后在需要进行版权声明的文章的md文件头部,设置属性 `topdeclare:true`5354- 配置显示55修改 `themes/yilia/_config.yml`在里面加入:56```yaml57#顶部版权基础设定:0-关闭声明; 1-文章对应的md文件里有topdeclare: true属性,才有版权声明; 2-所有文章均有版权声明58#当前应用的版权协议地址。59#昵称60#版权协议的名称61topdeclare_type: 162toplicensee_url: https://creativecommons.org/licenses/by-nc-sa/4.0/63topnickname: 莫与 #你的昵称64toplicensee_name: '知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议'分类的构建
添加categories链接
打开yilia/_config.yml文件,menu处做出以下修改1menu:2主页: /3分类: /categories4归档: /archives分类页面的构建
根目录执行hexo new page categories
该命令在source目录下生成一个categories目录,categories目录下有一个index.md文件。修改categories/index.md为:
1title: 文章分类2date: 2018-06-11 10:13:213type: "categories"4layout: "categories"5comments: false修改 yilia 主题
修改yilia\source\main.0cf68a.css,将下面的内容添加进去:1category-all-page {2margin: 30px 40px 30px 40px;3position: relative;4min-height: 70vh;5}6.category-all-page h2 {7margin: 20px 0;8}9.category-all-page .category-all-title {10text-align: center;11}12.category-all-page .category-all {13margin-top: 20px;14}15.category-all-page .category-list {16margin: 0;17padding: 0;18list-style: none;19}20.category-all-page .category-list-item-list-item {21margin: 10px 15px;22}23.category-all-page .category-list-item-list-count {24color: $grey;25}26.category-all-page .category-list-item-list-count:before {27display: inline;28content: " (";29}30.category-all-page .category-list-item-list-count:after {31display: inline;32content: ") ";33}34.category-all-page .category-list-item {35margin: 10px 10px;36}37.category-all-page .category-list-count {38color: $grey;39}40.category-all-page .category-list-count:before {41display: inline;42content: " (";43}44.category-all-page .category-list-count:after {45display: inline;46content: ") ";47}48.category-all-page .category-list-child {49padding-left: 10px;50}多层分类
新建yilia/layout/categories.ejs,输入1<article class="article article-type-post show">2<header class="article-header" style="border-bottom: 1px solid #ccc">3<h1 class="article-title" itemprop="name">4<%= page.title %>5</h1>6</header>78<% if (site.categories.length){ %>9<div class="category-all-page">10<h2>共计 <%= site.categories.length %> 个分类</h2>11<%- list_categories(site.categories, {12show_count: true,13class: 'category-list-item',14style: 'list',15depth:3, #这里代表着几层分类16separator: ''17}) %>18</div>19<% } %>20</article>修改自己的文章
1itle: HTML入门笔记23copyright: true4date: 2018-11-23 21:07:1556toc: true7tags: [HTML,前端]8categories: [前端,HTML]
yilia 主题 翻页报错
原因是 :
themes\yilia\layout\_partial\archive.ejs多出了«和»
解决方案,是在分页的地方删除以上两个标签,如下:
1//修改前2<% if (page.total > 1){ %>3<nav id="page-nav">4<%- paginator({5prev_text: '« Prev',6next_text: 'Next »'7}) %>8</nav>9<% } %>10// 修改后11<% if (page.total > 1){ %>12<nav id="page-nav">13<%- paginator({14prev_text: 'Prev',15next_text: 'Next'16}) %>17</nav>18<% } %>19...20//修改前21<% if (page.total > 1){ %>22<nav id="page-nav">23<%- paginator({24prev_text: '« Prev',25next_text: 'Next »'26}) %>27</nav>28<% } %>29//修改后30<% if (page.total > 1){ %>31<nav id="page-nav">32<%- paginator({33prev_text: 'Prev',34next_text: 'Next'35}) %>36</nav>37<% } %>