Obsidian

動作

  • ノート作成時にファイル名、frontmatterのtitle、H1要素を同期する
  • <Ctrl-N>で作成された場合にはプロンプトを表示してタイトルを入力させる
  • frontmatterを初期化する

デフォルトのプロンプトが表示される場合

Appearanceの設定でShow tab title barOnにしてください

実装

_templates/_on_create
<%*
// settings
const keys = {
  uid: "uid",
  title: "title",
  created: "created",
  modified: "modified",
}
const dateFormat = "YYYY-MM-DDTHH:mm+09:00"
 
// for date
const now = tp.date.now(dateFormat)
 
// for uid
const digit = "0123456789abcdef"
            + "ghijklmnopqrstuv"
            + "wxyzABCDEFGHIJKL"
            + "MNOPQRSTUVWXYZ-_"
const to64 = x => x.toString(2)
                   .split(/(?=(?:.{6})+(?!.))/g)
                   .map(v => digit[parseInt(v,2)])
                   .join("")
const uid = to64(parseInt(tp.date.now("X")));
 
// for title
let title = tp.file.title
if (title.startsWith("Untitled")) {
  title = await tp.system.prompt("Title")
  await tp.file.rename(title)
}
 
// insert them at the top of the note
const frontmatter = `---
${keys.uid}: ${uid}
${keys.title}: ${title}
${keys.created}: ${now}
${keys.modified}: ${now}
---
`
 
if (tp.file.content.length === 0) {
  tR += frontmatter
  tR += `# ${title}`
} else {
  const file = app.workspace.getActiveFile()
  await app.vault.modify(file, frontmatter + tp.file.content)
}
 
new Notice("Templater: _on_create was applied.")
-%>

設定

  • Trigger Templater on new file creationOnにする
  • Enable folder templatesOnにして作ったテンプレートを設定する