Github Page + Hexo 搭建个人博客

本文介绍了如何利用GithubPage和Hexo搭建个人博客,包括环境准备、Hexo安装、初始化、本地预览、部署到Github、备份以及设置文章摘要。通过简单的命令行操作,你可以轻松创建并维护自己的博客。同时,还提供了批量添加文章摘要的Python脚本以及使用hexo-excerpt插件的备用方法。
摘要由CSDN通过智能技术生成

title: Github Page + Hexo 搭建个人博客
categories: Hexo


复制代码

Github Page + Hexo 搭建个人博客

准备环境

首先查看环境是否安装,主要用到的工具有git,node,npm

git version
node -v
npm -v

安装Hexo

如果以上环境准备好了就可以使用 npm 开始安装 Hexo 了。也可查看 Hexo 的详细文档
在命令行输入执行以下命令:

npm install -g hexo-cli

安装 Hexo 完成后,再执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件

hexo init myBlog
cd myBlog
npm install

此时已安装完成,可以启动服务了

hexo s

在浏览器中输入 http://localhost:4000 回车就可以预览效果了

Hexo 部署

安装 hexo-deployer-git

npm install hexo-deployer-git --save

修改配置。

deploy:
  type: git
  repository: git@github.com:xxx/xxx.git,branchName
  branch: master
  message: update myblog

Hexo 备份

首先在 github 上 master 主分支下创建 hexo 空白分支
安装 hexo-git-backup 插件

npm install hexo-git-backup --save

到 Hexo 博客根目录的 _config.yml 配置文件里添加以下配置:

backup:
    type: git
    message: update myblog
    repository:
       github: git@github.com:xxx/xxx.git,branchName

然后使用命令即可

hexo b

Hexo 文章显示摘要

方法一(建议):在新版本中 _config.yml 下已经没有 auto_excerpt, 官方对于此选项已不支持,要显示摘要只有通过加 <!--more-->的方式

通过 python 脚本批量添加

import os

def walk_file(dir_path):
    file_list = []
    for root, dirs, files in os.walk(dir_path):
        for f in files:
            file_list.append(os.path.join(root, f))
            #print(os.path.join(root, f))
    return file_list

def insert_file(file_path):
    with open(file_path, 'r') as f:
        lines = f.readlines()
        lines.insert(14, '<!--more-->\n')
        s = ''.join(lines) 
    with open(file_path, 'w') as f:
        f.write(s)
    print(file_path+'Add <!--more-->')

def del_more(file_path):
    with open(file_path,"r",encoding="utf-8") as f:
        lines = f.readlines()
        #print(lines)
    with open(file_path,"w",encoding="utf-8") as f_w:
        for line in lines:
            if "<!--more-->" in line:
                continue
            f_w.write(line)
    print(file_path+'Delete <!--more-->')

if __name__ == "__main__":
    dir_path = '/home/coke/Documents/csdn2hexo/csdn'
    file_list = walk_file(dir_path)
    # print(file_list[0])
    for file in file_list:
        del_more(file)
        insert_file(file)

方法二(不建议):是通过安装插件的方法,但是经常会出现bug

1:使用npm安装hexo-excerpt

npm install hexo-excerpt --save

2:在站点配置文件中添加

excerpt:
  depth: 5  
  excerpt_excludes: []
  more_excludes: []
  hideWholePostExcerpts: true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值