建站备忘录
Why blog
- 广告铺天盖地
- 存储在垄断企业的物理服务器上,人为刀俎,我为鱼肉。
- 各个网站的信息排布方式没有办法自由控制
- 在互联网上写东西,需要一个真正的网站,否则很可能到最后只是在四处留下了的失去控制的信息。
自己动手丰衣足食,建立个人博客可以聚合在在网上冲浪的碎片,记录自己生活和成长的轨迹。它不像twiter那样碎片化,也不像facebook那样关系化,它是一个可以由自己主宰的空间。
本站搭建使用到的工具
- hexo: https://hexo.io/zh-cn/
- theme: https://github.com/liuyib/hexo-theme-stun
- theme-doc: https://liuyib.github.io/hexo-theme-stun/zh-CN/guide/quick-start.html
- gitee: https://gitee.com/lienhui68/lienhui68/pages
- github: https://github.com/lienhui68
- picgo: https://github.com/Molunerfinn/PicGo
- 图片压缩工具: https://tinypng.com/
- 图片下载免费网站: https://unsplash.com/
- font-icon: https://fontawesome.com/icons?d=gallery
- valine: https://valine.js.org/quickstart.html
- 在线制作favicon.ico:http://www.bitbug.net/
创建和发布流程
- 安装nodejs
- 安装hexo
- 选择主题
- 配置&个性化
- gitee或者github等提供page服务的网站上创建自己的page
- hexo clean && hexo d 发布到用户命名下的仓库(非用户命名仓库需要在hexo config文件中做相关设置)
- github可以自动化构建,gitee需要手动更新部署
图床
本来准备使用七牛云作为图床,结果七牛云试用期完需要绑定自己的已备案域名,看了下备案流程果断放弃。 github受距离流量等限制,访问效果很差,最终选择使用gitee作为图床,由于是免费图床,为了防止好景不长,在此附上搬家工具以作不时之需。 https://toolinbox.net/iPic/iPicMover.html 图床工具:picgo、iPic
小贴士
更新, 后面使用原始git备份的方法比较繁琐,可以使用hexo-backup插件进行备份
|
|
- 可以为博客多设几个站点,防止服务商因各种原因停止服务,毕竟连gitee、Coding都不稳定,更何况免费静态网站托管服务本身也不盈利。
- 本地博客源码最好上传到github上,防止本地误操作。 在根目录下新建.gitignore文件如下,过滤掉非源文件
|
|
接下来就是把blog源文件夹搞成一个本地仓库,如下命令
|
|
之后发布博客按照以下几个步骤就可以进行
|
|
更新日志
- 从hexo迁移到hugo,hexo部署实在太慢了,300个文件耗时10分钟。hugo号称世界上最快的网站构建框架,try it。
-
配置gitee和github双部署
修改根目录下
_config.yml
1 2 3 4 5 6 7
# Deployment ## Docs: https://hexo.io/docs/deployment.html deploy: type: git repo: gitee: git@gitee.com:lienhui68/lienhui68.git,master github: git@github.com:lienhui68/lienhui68.github.io.git,master
-
配置自定义域名
-
ping lienhui68.github.io 得到对应ip地址
-
进入阿里云后台,对域名进行解析配置
-
在hexo博客source文件夹下新建CNAME文件,添加内容
1
vanilla.org.cn
如果是在github上想很快看到效果,可以直接Add file
hugo 添加到content目录下
-
访问vanilla.org.cn
-
-
增加私密文章功能
-
设置页面显示数量
如果我们想对页面做独立的配置,需要安装插件进行功能支持。
使用如下命令进行安装需要的插件
1
$ npm install hexo-generator-archive --save
对应的
_config.yml
文件中添加如下配置1 2 3 4 5 6 7 8
index_generator: per_page: 5 archive_generator: per_page: 20 //为0时表示不分页全展示 yearly: true //按年生成归档 monthly: true //按月生成归档 tag_generator: per_page: 10
注意上面归档设置中的按年或者按月,需要修改模板给出对应的链接入口,对于没有兴趣修改模板的同学,可以将此处设为false,减少生成页面时的工作量。
问题解决
-
hexo生成的标签类别修改大小写问题
描述
当我的分类标签写的是Scrapy时,打开我的博客找到Scrapy标签,点击Scrapy却出现404页面。 当我把标签改为scrapy小写,再发布到网上,点击scrapy就不会出现404问题。 后来发现原来是git标签生成时忽略了大写,生成的实际标签为scrapy。 于是我来到我的Github中,找到
Gladysgong.github.io/categories/爬虫/
这个目录,发现实际生成的也是scrapy,所以 原因就在这里了。解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14
修改文件: cd blog/.deploy_git vi .git/config 将ignorecase=true改为ignorecase=false 删除Gladysgong.github.io中的文件并提交:(没设置关联这个可以不用做) git rm -rm * git commit -m "clean all files" git push Hexo再次生成及部署: cd .. hexo clean hexo g hexo d
-
批量操作
-
批量修改分类名称
- 一个分类名下的所有文件都将被修改
- 扫描所有文件,引用链接里的文件路径需要被修改
1 2 3 4 5 6 7 8 9 10
renameCategory(a, b); 操作前 categories: - a 操作后 categories: - b 站内链接: 操作前:{% post_link xxx/xxx/a xxx(可选) %} 操作后:{% post_link xxx/xxx/b xxx(可选) %}
-
修改tag名称
- 扫描所有文件,修改tag名称
1 2 3 4 5 6 7
renameTag(a, b); 操作前 tags: - a 操作后 tags: - b
-
移动分类
a->b 分类a下的所有文件移动到分类b下,移动后categories如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
moveCategory(a, b); 操作前: categories: - a - a1 操作后: categories: - c - b - a - a1 站内链接: 操作前:{% post_link xxx/xxx/a xxx(可选) %} 操作后:{% post_link xxx/xxx/b/a xxx(可选) %}
-
删除分类
- 修改当前分类下所有文件,删除该分类所在行
1 2 3 4 5 6 7 8
deleteCategory(a); 操作前 categories: - b - a 操作后 categories: - b
-
删除标签
- 扫描所有文件,删除该标签所在行
1 2 3 4 5 6 7 8
deleteTag(a); 操作前 tags: - b - a 操作后 tags: - b
源码:
工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
package com.eh.eden.utils.hexo; import com.google.common.base.Charsets; import com.google.common.collect.Lists; import lombok.Data; import org.apache.commons.io.FileDeleteStrategy; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Consumer; import java.util.stream.Stream; /** * 博客辅助工具类 * * @author David Li * @create 2020/09/16 */ public class BlogFileUtil { private static final Log log = LogFactory.getLog(BlogFileUtil.class); private static final String BLOG_ARTICLE_ROOT_DIR = "/Users/david/my/study/project/blog/source/_posts"; public static final File RootFile = new File(BLOG_ARTICLE_ROOT_DIR); /** * 将src文件夹移动到dest文件夹下 * case1: dest新建, 分类层次中没有dest * case2: 分类层次中有dest * 为了统一处理,将两个文件夹路径合并构造分类层次写入到文件中。 * eg: src文件夹下的文件分类层次是 * - 工作 * - x1 * - src * - x2 * dest文件路径是工作/dest * 移动之后 * - 工作 * - dest * - src * - x2 * 所以需要先获取dest文件夹的分类层次 工作/dest,然后与src/x2合并 * 此时有两种case * case1 遇到src时还没写完, 需要插入直到遇到src * case2 在遇到src之前已经写完 需要删除知道遇到src * <p> * 1. 创建临时文件000,往临时文件拷贝直到遇到category 001 * 2. 写入dest分类层次 101 * 3. 分类层次写完之后,先判断是否遇到src 111,没遇到则跳过,遇到之后继续拷贝。 * * @param destDirectoryPath * @param srcDirectoryPaths */ public static void move(String destDirectoryPath, String... srcDirectoryPaths) { File destDirectory = new File(RootFile, destDirectoryPath); // 验证 if (destDirectory.exists()) { assertDirectory(destDirectory); } else { destDirectory.mkdirs(); } if (srcDirectoryPaths.length < 1) { return; } List<String> destCategoryLayers = calculateCategoryLayers(destDirectory); // 创建临时文件用来组装新的文件内容 Arrays.asList(srcDirectoryPaths).forEach(src -> { File srcDirectory = new File(RootFile, src); // 断言srcFile是个文件夹,单个文件自己动手搞更快 assertDirectory(srcDirectory); // 扫描srcDirectory目录下所有文件,修改分类 scanFile(srcDirectory, srcFile -> { try { File file = new File(destDirectory, srcDirectory.getName() + "/" + srcFile.getName()); // 验证 if (file.exists()) { throw new RuntimeException("操作有误?" + file.getAbsolutePath()); } else { File dir = new File(destDirectory, srcDirectory.getName()); dir.mkdirs(); file.createNewFile(); } try ( FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel(); Stream<String> lines = Files.lines(srcFile.toPath(), Charsets.UTF_8) ) { // 使用引用类型突破闭包限制 @Data final class Flag { byte flag; public void plus(int i) { flag = (byte) (flag + i); } public boolean eq(int i) { return flag == (byte) i; } } Flag flag = new Flag(); lines.forEach(line -> { if (flag.eq(1)) { // 写入分类层次 destCategoryLayers.forEach(layer -> { try { fileChannel.write(ByteBuffer.wrap((" - " + layer + "\n").getBytes(Charsets.UTF_8))); } catch (IOException e) { e.printStackTrace(); } }); flag.plus(4); } else if (flag.eq(5) && !line.contains(srcDirectory.getName())) { // 从较多层级目录移动到较少层级目录,需要跳过多余的层级 return; } else { try { fileChannel.write(ByteBuffer.wrap((line + "\n").getBytes())); } catch (IOException e) { e.printStackTrace(); } } if (flag.eq(7)) { // 加快运行速度 return; } if (flag.eq(0) && line.contains("categories")) { // // 在遇到categories之前写 flag.plus(1); } else if (!flag.eq(0) && line.contains(srcDirectory.getName())) { flag.plus(2); } }); // 删除原文件 srcFile.delete(); } } catch (IOException e) { e.printStackTrace(); } }); // 删除原文件夹,有DS_STORE隐藏文件 try { FileDeleteStrategy.FORCE.delete(srcDirectory); } catch (IOException e) { e.printStackTrace(); } }); } /** * 扫描给定文件夹下的所有文件,执行consumer * * @param file 文件夹或者文件 * @param consumer */ private static void scanFile(File file, Consumer<File> consumer) { assertNotNull(file); // 对单个文件处理 if (file.isFile() && file.getName().endsWith("md")) { consumer.accept(file); return; } List<File> files = getAllFiles(file); if (files.isEmpty()) { return; } // 对文件夹处理 files.stream().forEach(f -> scanFile(f, consumer)); } /** * 获取分类层次 * eg: src文件夹下的文件分类层次是 * * - 工作 * * - x1 * * - src * * - x2 * * dest文件路径是工作/dest * * 移动之后 * * - 工作 * * - dest * * - src * * - x2 * * 所以需要先获取文件夹的分类层次 工作/dest * * @param file * @return [工作, dest] */ private static List<String> calculateCategoryLayers(File file) { List<String> result = Lists.newArrayList(file.getName()); while (true) { File parent = file.getParentFile(); if (RootFile.equals(parent)) { break; } file = parent; result.add(parent.getName()); } Collections.reverse(result); return result; } /** * 修改文件夹名 * 1. 修改文件夹下所有文件指定分类 * 2. 重命名文件夹 * * @param file * @param name */ public static void renameCategory(File file, String name) { assertDirectory(file); // 1. 修改文件夹下所有文件指定分类 scanFile(file, f -> modifyRow(f, file.getName(), name)); // 2. 重命名文件夹 renameDirectory(file, name); } /** * 修改某一行(替换内容) * 修改分类名/标签名 * 1. 创建临时文件 tmp_原文件名.md * 2. 往临时文件写 * 3. 删除原文件 * 4. 临时文件改名 * * @param file * @param newStr */ private static void modifyRow(File file, String oldStr, String newStr) { // 1. 创建临时文件 File tmpFile = new File(file.getParent(), "tmp_" + file.getName()); // 验证 if (tmpFile.exists()) { throw new RuntimeException("操作有误?" + file.getAbsolutePath()); } try ( FileChannel fileChannel = new RandomAccessFile(tmpFile, "rw").getChannel(); Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8) ) { lines.forEach(line -> { try { if (line.contains(oldStr)) { fileChannel.write(ByteBuffer.wrap((line.replace(oldStr, newStr) + "\n").getBytes(Charsets.UTF_8))); } else { fileChannel.write(ByteBuffer.wrap((line + "\n").getBytes(Charsets.UTF_8))); } } catch (IOException e) { e.printStackTrace(); } }); String oldName = file.getName(); // 删除原文件 file.delete(); // 更改临时文件名 tmpFile.renameTo(new File(tmpFile.getParent(), oldName)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 修改站内引用 * 用于修改文章里的站内链接 * old: {%post_link oldStr 别名 %} * new: {%post_link newStr 别名 %} * {%post_link 数据结构与算法应用/迷宫路径(回溯+递归) 迷宫路径(回溯+递归) %} * * @param file * @param oldPath /xxx/xxx * @param newPath /yyy/yyy */ private static void modifyQuote(File file, String oldPath, String newPath) { try ( RandomAccessFile raf = new RandomAccessFile(file, "rw") ) { String lineRegex = ".*\\{%.*post_link.*" + oldPath + ".*%}.*"; String line; long lastPointer = 0; // 记住上一次的偏移量 while ((line = raf.readLine()) != null) { if (StringUtils.isEmpty(line)) { continue; } if (line.matches(lineRegex)) { raf.seek(lastPointer); String fileName = file.getName(); // old: {%post_link /xxx/xxx/fileName 别名 %} // new: {%post_link /yyy/yyy/fileName 别名 %} String contentRegex = "{%.*post_link.*" + oldPath + "/" + fileName; newPath = "{%post_link " + newPath + "/" + fileName; raf.writeBytes(line.replace(contentRegex, newPath)); return; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * * 输出 * * path1 * * line1 * * path1 * * line2 * * @param influencePath 可能的原链接path */ public static void printAllArticleQuotes(String influencePath) { scanFile(RootFile, f -> { try ( RandomAccessFile raf = new RandomAccessFile(f, "r") ) { String regex = ".*\\{%.*post_link.*" + influencePath + ".*%}.*"; String line; while ((line = readLine(raf)) != null) { if (StringUtils.isEmpty(line)) { continue; } if (line.matches(regex)) { System.out.println(f.getAbsolutePath()); System.out.println(line); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }); } /** * 使用 RandomAccessFile对象方法的 readLine() 都会将编码格式转换成 ISO-8859-1 所以 输出显示是还要在进行一次转码 * * @param raf * @return */ private static String readLine(RandomAccessFile raf) throws IOException { String line = raf.readLine(); if (line == null) { return null; } return new String(line.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8); } /** * 重命名文件夹 * * @param file * @param name * @return */ private static boolean renameDirectory(File file, String name) { if (!file.exists()) { log.debug("not exits."); return false; } File newFile = new File(file.getParent(), name); return file.renameTo(newFile); } /** * 获取给定文件夹的所有文件夹 * * @param file * @return */ private static List<File> getAllDirectories(File file) { assertNotNull(file); assertDirectory(file); File[] files = file.listFiles((dir, name) -> dir.isDirectory()); List<File> result = Lists.newArrayList(); Collections.addAll(result, files); return result; } /** * 获取给定文件夹下的所有普通文件 * * @param file * @return */ private static List<File> getAllNormalFiles(File file) { assertNotNull(file); assertDirectory(file); File[] files = file.listFiles((dir, name) -> dir.isFile()); List<File> result = Lists.newArrayList(); Collections.addAll(result, files); return result; } /** * 获取给定文件夹下的所有文件 * * @param file * @return */ private static List<File> getAllFiles(File file) { if (file.getName().endsWith("DS_Store") || file.getName().endsWith("zip")) { return Lists.newArrayList(); } assertNotNull(file); assertDirectory(file); File[] files = file.listFiles((dir, name) -> dir.isDirectory() || name.endsWith("md")); List<File> result = Lists.newArrayList(); Collections.addAll(result, files); return result; } /** * not null * * @param file */ private static void assertNotNull(File file) { Assert.notNull(file, () -> "文件为null"); } /** * 文件夹断言 * * @param file */ private static void assertDirectory(File file) { Assert.isTrue(file.isDirectory(), () -> file.getAbsolutePath() + "is not a directory."); } /** * 普通文件断言 * * @param file */ private static void assertFile(File file) { Assert.isTrue(file.isFile(), () -> file.getAbsolutePath() + "is not a File."); } }
操作类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
package com.eh.eden.utils.hexo; import org.testng.annotations.Test; import java.io.File; import static com.eh.eden.utils.hexo.BlogFileUtil.*; /** * 博客辅助工具操作类 * * @author David Li * @create 2020/09/16 */ public class BlogFileUtilTest { /** * */ @Test public void test() { // renameCategory(new File(RootFile, "技术"), "工作"); } /** * 打印所有影响到的站内链接 */ @Test public void test1() { printAllArticleQuotes(""); } /** * 更改文件夹名 */ // @Test public void test2() { renameCategory(new File(RootFile, "工作/999. 其他"), "999_其他"); // renameCategory(new File(RootFile, "工作/数据结构与算法"), "理论"); } /** * 移动文件夹 */ // @Test public void test3() { // move("工作/计科基础", "工作/c"); move("工作/读书笔记", "工作/1000. 读书笔记"); } public static void main(String[] args) { String line = "- 数据结构与算法"; System.out.println(line.replace("数据结构与算法", "理论")); } }
-