基于Hexo和NexT的博客搭建

前言

主要记录一些要注意的点,因为网上教程版本众多,所以尽量以最新的官方文档为准;我的博客主要用来记录学习,搭的风格比较简练。

博客地址

官方文档地址

Hexo官网

NexT中文文档

NexT英文文档

版本

  • Node.js v12.16.3

  • hexo: 4.2.1

  • NexT: v8.0.0-rc.2

    2014-2017	https://github.com/iissnan/hexo-theme-next
    2018-2019	https://github.com/theme-next/hexo-theme-next
    2020	    https://github.com/next-theme/hexo-theme-next
    

Front-matter

Front-matter 是文件最上方以 --- 分隔的区域,用于指定个别文件的变量,举例来说:

---
title: Hello World
date: 2013/7/13 20:46:25
---

以下是预先定义的参数,您可在模板中使用这些参数值并加以利用。

参数描述默认值
layout布局
title标题文章的文件名
date建立日期文件建立日期
updated更新日期文件更新日期
comments开启文章的评论功能true
tags标签(不适用于分页)
categories分类(不适用于分页)
permalink覆盖文章网址
keywords仅用于 meta 标签和 Open Graph 的关键词(不推荐使用)

分类和标签

只有文章支持分类和标签,您可以在 Front-matter 中设置。在其他系统中,分类和标签听起来很接近,但是在 Hexo 中两者有着明显的差别:分类具有顺序性和层次性,也就是说 Foo, Bar 不等于 Bar, Foo;而标签没有顺序和层次。

categories:
- Diary
tags:
- PS3
- Games

分类方法的分歧

如果您有过使用 WordPress 的经验,就很容易误解 Hexo 的分类方式。WordPress 支持对一篇文章设置多个分类,而且这些分类可以是同级的,也可以是父子分类。但是 Hexo 不支持指定多个同级分类。下面的指定方法:

categories:
  - Diary
  - Life

会使分类Life成为Diary的子分类,而不是并列分类。因此,有必要为您的文章选择尽可能准确的分类。

如果你需要为文章添加多个分类,可以尝试以下 list 中的方法。

categories:
- [Diary, PlayStation]
- [Diary, Games]
- [Life]

此时这篇文章同时包括三个分类: PlayStationGames 分别都是父分类 Diary 的子分类,同时 Life 是一个没有子分类的分类。

搭建要点

在 Hexo 中有两份主要的配置文件,其名称都是 _config.yml。 其中,一份位于站点根目录下,主要包含 Hexo 本身的配置;另一份位于主题目录下,这份配置由主题作者提供,主要用于配置主题相关的选项。

为了描述方便,在以下说明中,将前者称为 站点配置文件, 后者称为 主题配置文件

其实主要就是修改这两个文件,有的会需要结合修改部分的描述安装你需要的功能插件。

菜单栏

设置分类和标签

hexo new page categories
hexo new page tags

分别在Front-matter中设置

type: "categories"
type: "tags"

6款插件

前5款在站点根目录下安装,后1款在主题根目录下安装

相对路径插入图片

npm install https://github.com/CodeFalling/hexo-asset-image --save

修改 站点配置文件

post_asset_folder: true

注意点:打开站点根目录下/node_modules/hexo-asset-image/index.js,将内容更换为下面的代码

'use strict';
var cheerio = require('cheerio');

// http://stackoverflow.com/questions/14480345/how-to-get-the-nth-occurrence-in-a-string
function getPosition(str, m, i) {
  return str.split(m, i).join(m).length;
}

var version = String(hexo.version).split('.');
hexo.extend.filter.register('after_post_render', function(data){
  var config = hexo.config;
  if(config.post_asset_folder){
    	var link = data.permalink;
	if(version.length > 0 && Number(version[0]) == 3)
	   var beginPos = getPosition(link, '/', 1) + 1;
	else
	   var beginPos = getPosition(link, '/', 3) + 1;
	// In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html".
	var endPos = link.lastIndexOf('/') + 1;
    link = link.substring(beginPos, endPos);

    var toprocess = ['excerpt', 'more', 'content'];
    for(var i = 0; i < toprocess.length; i++){
      var key = toprocess[i];
 
      var $ = cheerio.load(data[key], {
        ignoreWhitespace: false,
        xmlMode: false,
        lowerCaseTags: false,
        decodeEntities: false
      });

      $('img').each(function(){
		if ($(this).attr('src')){
			// For windows style path, we replace '\' to '/'.
			var src = $(this).attr('src').replace('\\', '/');
			if(!/http[s]*.*|\/\/.*/.test(src) &&
			   !/^\s*\//.test(src)) {
			  // For "about" page, the first part of "src" can't be removed.
			  // In addition, to support multi-level local directory.
			  var linkArray = link.split('/').filter(function(elem){
				return elem != '';
			  });
			  var srcArray = src.split('/').filter(function(elem){
				return elem != '' && elem != '.';
			  });
			  if(srcArray.length > 1)
				srcArray.shift();
			  src = srcArray.join('/');
			  $(this).attr('src', config.root + link + src);
			  console.info&&console.info("update link as:-->"+config.root + link + src);
			}
		}else{
			console.info&&console.info("no src attr, skipped...");
			console.info&&console.info($(this));
		}
      });
      data[key] = $.html();
    }
  }
});

生成RSS

npm install --save hexo-generator-feed

修改主题配置文件

social:
  RSS: /atom.xml || fa fa-rss

文章pinned

npm uninstall hexo-generator-index --save
npm install hexo-generator-index-pin-top --save

需要pinned的文章在 Front-matter 中加上 top 即可,数值越大表示等级越高,越靠前显示

显示效果需要修改themes\hexo-theme-next\layout_macro\post.njk,在<header class="post-header">(我这里是第18行)下插入

{% if post.top %}
    <i class="fa fa-thumbtack"></i>
{% endif %}

搜索插件

npm install hexo-generator-searchdb --save

编辑 站点配置文件,新增以下内容到任意位置:

search:
  path: search.xml
  field: post
  format: html
  limit: 10000

编辑 主题配置文件,启用本地搜索功能:

# Local search
local_search:
  enable: true

部署插件

npm install hexo-deployer-git --save

编辑 站点配置文件

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: 你的gitee或github库
  branch: master

页面加载进度条

git clone https://github.com/theme-next/theme-next-pace source/lib/pace

修改主题配置文件

pace:
 enable: true

其它的一些功能

大部分修改下主题配置文件就行了

下面是修改的部分

 这个是突出页脚红心的
   # Icon between year and copyright info.
   icon:
     # Icon name in Font Awesome. See: https://fontawesome.com/icons
-    name: fa fa-heart
+    name: fa fa-heartbeat
     # If you want to animate the icon, set it to true.
-    animated: false
+    animated: true
     # Change the color of icon, using Hex Code.
     color: "#ff0000"
 
这个是侧栏显示许可证
 # CC licenses are available in 39 languages, you can find the specific and correct abbreviation you need on https://creativecommons.org
 creative_commons:
   license: by-nc-sa
-  sidebar: false
+  sidebar: true
   post: false
   language:
 
这个是主题风格修改
 # Schemes
-scheme: Muse
+#scheme: Muse
 #scheme: Mist
 #scheme: Pisces
-#scheme: Gemini
+scheme: Gemini
 
这个是菜单栏修改
 menu:
-  #home: / || fa fa-home
+  home: / || fa fa-home
   #about: /about/ || fa fa-user
-  #tags: /tags/ || fa fa-tags
-  #categories: /categories/ || fa fa-th
-  #archives: /archives/ || fa fa-archive
+  tags: /tags/ || fa fa-tags
+  categories: /categories/ || fa fa-th
+  archives: /archives/ || fa fa-archive
   #schedule: /schedule/ || fa fa-calendar
   #sitemap: /sitemap.xml || fa fa-sitemap
   #commonweal: /404/ || fa fa-heartbeat

这个是头像设置
 # Sidebar Avatar
 avatar:
   # Replace the default image and set the url here.
-  url: #/images/avatar.gif
+  url: /images/王也.jpg
   # If true, the avatar will be dispalyed in circle.
   rounded: false
   # If true, the avatar will be rotated with the cursor.

这个是侧栏社交链接和图标设置
 social:
-  #GitHub: https://github.com/yourname || fab fa-github
-  #E-Mail: mailto:yourname@gmail.com || fa fa-envelope
+  Gitee: https://gitee.com/zystrivego/blog || fab fa-github
+  E-Mail: mailto:zystrivego@163.com || fa fa-envelope
+  Music: http://music.163.com/m/user/home?id=15370305 || fa fa-music
+  Douban: https://www.douban.com/people/74346817/ || fa fa-photo-video
+  RSS: /atom.xml || fa fa-rss

这个是代码块复制按钮设置
   # Add copy button on codeblock
   copy_button:
-    enable: false
+    enable: true

这个是滚动百分比设置
   # Scroll percent label in b2t button.
-  scrollpercent: false
+  scrollpercent: true
 
 # Reading progress bar
 reading_progress:
@@ -472,7 +475,7 @@
 baidu_site_verification:
 
 百度推送,搜索引擎优化
 # Enable baidu push so that the blog will push the url to baidu automatically which is very helpful for SEO.
-baidu_push: false
+baidu_push: true
 
 
这个是评论系统,适合国内使用
 # Valine
 # For more information: https://valine.js.org, https://github.com/xCss/Valine
 valine:
-  enable: false
-  appId: # Your leancloud application appid
-  appKey: # Your leancloud application appkey
+  enable: true
+  appId: xxxxxx
+  appKey: xxxxxxx
-  placeholder: Just go go # Comment box placeholder
+  placeholder: 无需注册,支持Markdown格式 # Comment box placeholder


搜索引擎优化
 # Google Analytics
 google_analytics:
-  tracking_id: # <app_id>
+  tracking_id: UA-166280277-1

搜索引擎优化
 # Baidu Analytics
-baidu_analytics: # <app_id>
+baidu_analytics: xxxxxx

一些小知识点

如何设置「阅读全文」

在首页显示一篇文章的部分内容,并提供一个链接跳转到全文页面是一个常见的需求。 NexT 提供2种方式来控制文章在首页的显示方式。 也就是说,在首页显示文章的摘录并显示 阅读全文 按钮,可以通过以下方法:

  1. 在文章中使用 <!-- more --> 手动进行截断,Hexo 提供的方式 推荐
  2. 在文章的 front-matter 中添加 description,并提供文章摘录

建议使用 <!-- more -->(即第一种方式),除了可以精确控制需要显示的摘录内容以外, 这种方式也可以让 Hexo 中的插件更好的识别。

渲染跳过

比如404,readme文件

相关文件可以去这里

修改站点配置文件

skip_render:
  - README.md
  - 404.html

开源协议怎么选

Creative Commons Licenses

Choose an open source license

小图标在哪找

themes\hexo-theme-next\source\lib\font-awesome\css\all.min.css

官方文档摘录

不要处理我的文章

如果你不想你的文章被处理,你可以将 Front-Matter 中的layout: 设为 false

草稿

刚刚提到了 Hexo 的一种特殊布局:draft,这种布局在建立时会被保存到 source/_drafts 文件夹,您可通过 publish 命令将草稿移动到 source/_posts 文件夹,该命令的使用方式与 new 十分类似,您也可在命令中指定 layout 来指定布局。

$ hexo publish [layout] <title>

草稿默认不会显示在页面中,您可在执行时加上 --draft 参数,或是把 render_drafts 参数设为 true 来预览草稿。

指令

init

$ hexo init [folder]

新建一个网站。如果没有设置 folder ,Hexo 默认在目前的文件夹建立网站。

new

$ hexo new [layout] <title>

新建一篇文章。如果没有设置 layout 的话,默认使用 _config.yml 中的 default_layout 参数代替。如果标题包含空格的话,请使用引号括起来。

$ hexo new "post title with whitespace"
参数描述
-p, --path自定义新文章的路径
-r, --replace如果存在同名文章,将其替换
-s, --slug文章的 Slug,作为新文章的文件名和发布后的 URL

默认情况下,Hexo 会使用文章的标题来决定文章文件的路径。对于独立页面来说,Hexo 会创建一个以标题为名字的目录,并在目录中放置一个 index.md 文件。你可以使用 --path 参数来覆盖上述行为、自行决定文件的目录:

hexo new page --path about/me "About me"

以上命令会创建一个 source/about/me.md 文件,同时 Front Matter 中的 title 为 "About me"

注意!title 是必须指定的!如果你这么做并不能达到你的目的:

hexo new page --path about/me

此时 Hexo 会创建 source/_posts/about/me.md,同时 me.md 的 Front Matter 中的 title 为 "page"。这是因为在上述命令中,hexo-cli 将 page 视为指定文章的标题、并采用默认的 layout

generate

$ hexo generate

生成静态文件。

选项描述
-d, --deploy文件生成后立即部署网站
-w, --watch监视文件变动
-b, --bail生成过程中如果发生任何未处理的异常则抛出异常
-f, --force强制重新生成文件 Hexo 引入了差分机制,如果 public 目录存在,那么 hexo g 只会重新生成改动的文件。 使用该参数的效果接近 hexo clean && hexo generate
-c, --concurrency最大同时生成文件的数量,默认无限制

该命令可以简写为

$ hexo g

publish

$ hexo publish [layout] <filename>

发表草稿。

server

$ hexo server

启动服务器。默认情况下,访问网址为: http://localhost:4000/

选项描述
-p, --port重设端口
-s, --static只使用静态文件
-l, --log启动日记记录,使用覆盖记录格式

deploy

$ hexo deploy

部署网站。

参数描述
-g, --generate部署之前预先生成静态文件

该命令可以简写为:

$ hexo d

render

$ hexo render <file1> [file2] ...

渲染文件。

参数描述
-o, --output设置输出路径

migrate

$ hexo migrate <type>

从其他博客系统 迁移内容

###clean

$ hexo clean

清除缓存文件 (db.json) 和已生成的静态文件 (public)。

在某些情况(尤其是更换主题后),如果发现您对站点的更改无论如何也不生效,您可能需要运行该命令。

list

$ hexo list <type>

列出网站资料。

version

$ hexo version

显示 Hexo 版本。

选项

安全模式
$ hexo --safe

在安全模式下,不会载入插件和脚本。当您在安装新插件遭遇问题时,可以尝试以安全模式重新执行。

调试模式
$ hexo --debug

在终端中显示调试信息并记录到 debug.log。当您碰到问题时,可以尝试用调试模式重新执行一次,并 提交调试信息到 GitHub

简洁模式
$ hexo --silent

隐藏终端信息。

自定义配置文件的路径
# 使用 custom.yml 代替默认的 _config.yml
$ hexo server --config custom.yml

# 使用 custom.yml 和 custom2.json,其中 custom2.json 优先级更高
$ hexo generate --config custom.yml,custom2.json,custom3.yml

自定义配置文件的路径,指定这个参数后将不再使用默认的 _config.yml
你可以使用一个 YAML 或 JSON 文件的路径,也可以使用逗号分隔(无空格)的多个 YAML 或 JSON 文件的路径。例如:

# 使用 custom.yml 代替默认的 _config.yml
$ hexo server --config custom.yml

# 使用 custom.yml, custom2.json 和 custom3.yml,其中 custom3.yml 优先级最高,其次是 custom2.json
$ hexo generate --config custom.yml,custom2.json,custom3.yml

当你指定了多个配置文件以后,Hexo 会按顺序将这部分配置文件合并成一个 _multiconfig.yml。如果遇到重复的配置,排在后面的文件的配置会覆盖排在前面的文件的配置。这个原则适用于任意数量、任意深度的 YAML 和 JSON 文件。

显示草稿
$ hexo --draft

显示 source/_drafts 文件夹中的草稿文章。

自定义 CWD
$ hexo --cwd /path/to/cwd

自定义当前工作目录(Current working directory)的路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值