obsidian是一款好用的markdown编辑器, 用来记笔记还是不错的。 并且支持插件系统,可以通过模板来生成博客的frontmatter, 省去了hexo new的操作 所以准备配置一下用obsidian来写博客。

相关文章:
静态博客生成工具hexo
使用typora更好更快地写hugo博客
使用vscode来写hugo博客并处理图片插入问题

打开仓库

首先使用obsidian打开source/_posts目录, 然后会生成一些配置文件。进入_posts目录

创建.gitignore将一些临时文件排除掉。

1
.obsidian/workspace.json

配置模板

自带的模板插件功能太单一了, 我们关闭安全模式, 安装第三方插件Templater

创建Templates目录,修改配置指定Template的目录。修改配置项Template folder locationTemplates

然后再此目录下创建Front-matter.md文件,此文件用作hexo的frontmatter模板。

1
2
3
4
5
6
7
8
9
---
title: <% tp.file.title %>
categories:
  - <% tp.file.folder(relative=true) %>
tags:
  - ''
abbrlink: <% tp.user.get_guid() %>
date: <% tp.date.now(format="YYYY-MM-DD HH:mm:ss") %>
---

其中:

模板 作用
tp.file.title 获取到的就是文件名
tp.file.folder(relative=true) 是获取文件所在的相对路径,就是所在目录名字
tp.user.get_guid() 是自定义方法,脚本后面展示,用于获取GUID,用作博客的url
tp.date.now(format=”YYYY-MM-DD HH:mm:ss”) 以指定格式格式化时间

详细的变量使用请查看Templater官方文档https://silentvoid13.github.io/Templater/

自定义脚本

创建目录Scripts, 然后在设置里配置Script files folder locationScripts

获取GUID脚本 Scripts/get_guid.js

1
2
3
4
5
6
7
8
function get_guid() {
    function S4() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

module.exports = get_guid;

然后每次创建新markdown文件的时候,只需要点击templater按钮, 然后就会自动生成我们需要的frontmatter, 就不用 hexo new 了

使用git插件进行同步

安装Obsidian GIT 插件
按下快捷键CTRL + P 选择 commit all changes , 然后选择 push 即可发布。