title: 【记录】在 GitHub 上 搭建 Hexo urlname: -ji-lu--zai-GitHub-shang-da-jian-Hexo date: 2023-08-30 21:07:42 index_img: https://api.limour.top/randomImg?d=2023-08-30 21:07:42 tags: ['Github', 'hexo']
conda activate node
npm i hexo-cli -g
hexo -v
hexo init
npm install
npm install hexo-deployer-git --save
echo hexo.limour.top > source/CNAME
建立 <github usrname>.github.io
的仓库
仓库新建 gh-pages
分支
按 hexo-deployer-git 的指引获取 token
编辑 _config.yml
,添加内容如下
deploy:
type: git
repo: https://oauth2:ghp_xxxxx@xxx.limour.top/xxx/https://github.com/limour-blog/limour-blog.github.io.git
branch: gh-pages
token: ghp_xxxxx
name: xxx@limour.top # 注册 Github 的邮箱
email: xxx@limour.top # 注册 Github 的邮箱
npm install hexo-theme-butterfly --save
npm install hexo-renderer-pug hexo-renderer-stylus --save
mkdir scripts && touch scripts/CDN.js
编辑 _config.yml
,修改内容如下
theme: butterfly
编辑 scripts/CDN.js
,内容如下
'use strict';
const { filter } = hexo.extend;
// 替换 CDN
filter.register('before_generate', () => {
const { asset } = hexo.theme.config;
for (const name in asset) {
asset[name] = asset[name]
.replace('//cdn.jsdelivr.net/', '//你自己的反代/');
}
}, 99);
rm -rf .deploy_git && hexo c && hexo g && hexo d
编辑 .gitignore
,添加 _config.yml
git init && git branch -m main
git remote add origin https://oauth2:ghp_xxxxx@xxx.limour.top/xxx/https://github.com/limour-blog/limour-blog.github.io.git
git add . && git commit -m 'backup' && git push -u origin +main
0.0.0.0/0
2.2.12 or later
npm i hexo-filter-links --save
去 _config.yml
添加配置
links:
enable: true
field: "post"
exclude:
- "limour.top"
- "*.limour.top" #1.0.4及以上版本支持
cd ~/base/NGPM/data
git clone --depth=1 -b gh-pages --single-branch https://github.com/limour-blog/limour-blog.github.io.git
location / {
gzip on;
gzip_min_length 256;
gzip_comp_level 6;
gzip_types text/plain text/xml application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
gzip_vary on;
gzip_buffers 32 4k;
root /data/limour-blog.github.io;
try_files $uri $uri.html $uri/index.html =404;
error_page 404 /404.html;
}
后续更新
cd ~/base/NGPM/data/limour-blog.github.io
git fetch --depth=1 -f && git reset --hard origin/gh-pages
修改node_modules/hexo-theme-fluid/scripts/generators/local-search.js
文件
env.addFilter('noControlChars', function(str) {
// eslint-disable-next-line no-control-regex
return str && str.replace(/[\x00-\x1F\x7F]/g, '').replace(/<figure class="highlight.*?<\/figure>/ig, '').replace(/(<([^>]+)>)/ig, '').replace(/(https?:\/\/[^\s]+)/ig, '');
});
env.addFilter('urlJoin', function(str) {
const base = str[0];
const relative = str[1].replace(/\.html$/g, '');
return relative
? base.replace(/\/+$/, '') + '/' + relative.replace(/^\/+/, '')
: base;
});
编辑 scripts/custom.js
, 内容如下
// 首选网页 canonical
hexo.extend.helper.register('autoCanonical', function (config, page) {
var base_url = config.url;
if (config.url.charAt(config.url.length - 1) !== '/') base_url += '/';
base_url += page.canonical_path
return '<link rel="canonical" href="' + base_url.replace('/index.html', '/').replace(/\.html$/g, '') + '"/>';
});
编辑 node_modules/hexo-theme-fluid/layout/_partials/head.ejs
, 内容如下
// ....
<%- autoCanonical(config, page) %>
</head>