Obsidianでノートの内容をGeminiに要約してもらい、descriptionに設定するTemplater
参考:Obsidian:プロパティにdescriptionを追記する - Jazzと読書の日々
APIキーは以下で取得可能
https://aistudio.google.com/app/api-keys
(たぶん、プロジェクトの作成が必要で、テキトーに作ればいい)
(以下、実際はmdファイルだが、シンタックスを使うためjsにしてある)
code:_setDescription.js
<%*
// APIキーを https://aistudio.google.com/app/apikey で取得してください。
const api_key = ''
const prompt = "<title>と<content>を合わせて簡潔に要約し、descriptionに設定できるような一文にしてください。"//自由に書き換えてよい
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent'
if(!prompt) return
const title = tp.file.title
const content = tp.file.content
text = <title>${title}</title><content>${content}</content>\n:${prompt}
new Notice("thinking...")
const data = {
contents: [{
parts: text: text }
}]
}
response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-goog-api-key': api_key
},
body: JSON.stringify(data)
})
if(response.ok) {
const result = await response.json()
new Notice("プロパティに設定")
s = result.candidates0.content.parts0.text //ここにGeminiからの回答が入っている
const file = tp.file.find_tfile(tp.file.path(true)); // フロントマターを処理・更新
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter"description" = s; //設定したいプロパティを変えたいならここを変更
});
}else{
new Notice(response.statusText)
}
%>
#Obsidianの使い方