【Obsidian初心者】tp.frontmatterがundefinedになるときの対処方法
概要
- 筆者はObsidian初心者です。
- テンプレートからの新規作成時に、tp.frontmatterがundefinedになることでつまずきました。
- 理由と対処法が示されたGitHubのDiscussionがあったため、備忘録として引用しながらまとめていきます。
実行環境
つまずいたこと
-
テンプレートからの新規作成時に、frontmatter(プロパティ)の内容を参照するため
tp.frontmatter.title
を使用したが、undefinedになってしまう -
使用したテンプレート:
template.md
---
title: "<% tp.system.prompt("タイトルを入力")%>"
---
# <% tp.frontmatter.title %>
- 作成されたノート(プロンプトには「テスト」を入力):
---
title: テスト
---
# undefined
tp.frontmatterがundefinedになるのを回避する方法
-
tp.frontmatter
は既にfrontmatterがあるファイルでのみ正しく動作する -
テンプレートからの新規作成時には
tp.frontmatter
を使わない -
今回やりたかったことの実現例:
revised_template.md
---
<% const title = await tp.system.prompt("タイトルを入力") %>
title: <% title %>
---
# <% title %>
参考記事の解説
The documentation starts with a file that already has frontmatter.
Then, thetp.frontmatter
is applied.
In other words,tp.frontmatter
only works if the document already has pre-existing frontmatter to work with.
If you are using this template on a new file, there is no frontmatter to parse because there is no frontmatter yet.
- 雑日本語訳:
- ドキュメント3は、既にfrontmatterがあるファイルであることを前提にしており、そのときは
tp.frontmatter
を適用することができます。 - すなわち、
tp.frontmatter
は、既にfrontmatterがあるファイルでのみ動作します。 - もし、新規ファイルの作成に
tp.frontmatter
を含むテンプレートを使用した場合は、そのファイルにはまだfrontmatterがないので、tp.frontmatter
でパージングできるfrontmatterがないと言えます。
- ドキュメント3は、既にfrontmatterがあるファイルであることを前提にしており、そのときは