Github Page + 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

posted @ 2021-10-05 20:00  可乐芬达  阅读(76)  评论(0编辑  收藏  举报