<%* // Gemini API 키 설정 const GEMINI_API_KEY=“AIzaSyAUyWPGBkl9fxpAh1O4uIGU87I2dpgSYOg”%>
<%_* // 프롬프트 const index_prompt = `You are an expert in generating an appropriate index properties that will be used in Obsidian Note. Your mission is to generate one or two indexes suitable for given content. Your generated output must be comma-separated values.
Here is the example output:
Example Output:
Here’s a list of possible substitutions for index. You must use one of these indexes listed below.
[Note]
- Write your Final Answer in Korean.
- DO NOT narrate, just write the output without any markdown formatting of wrapping.
- Generated indexes must be related to the content. Otherwise, you will be penalized.
- Generated indexes must be one of the
` _%>
<%_*
// frontmatter의 index 속성을 업데이트하는 함수
const processIndex = async (file, newIndex) ⇒ {
await tp.app.fileManager.processFrontMatter(file, (frontmatter) ⇒ {
const index = newIndex.split(’,‘).map(index ⇒ index.trim());
frontmatter.index = index.map(index ⇒ ${index});
});
};
_%>
<%_*
// Gemini API를 호출하여 인덱스를 생성하는 함수
async function generateIndex(content) {
const model = “gemini-1.5-flash”;
const url = https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${GEMINI_API_KEY};
const full_prompt = ${index_prompt}\n\nHere is the content of the note:\n${content};
try {
const response = await tp.obsidian.requestUrl({
method: "POST",
url: url,
contentType: "application/json",
body: JSON.stringify({
contents: [{ parts: [{ text: full_prompt }] }]
})
});
const resultText = response.json.candidates[0].content.parts[0].text;
return resultText.trim();
} catch (error) {
console.error('Gemini API 호출 중 오류 발생:', error);
if (error.response) {
console.error('Gemini API Error Response:', await error.response.text());
}
return '인덱스 생성 실패';
}
}
// 현재 노트의 내용을 가져옵니다 const fileContent = tp.file.content;
// 인덱스를 생성하고 파일에 적용 const index = await generateIndex(fileContent); const file = tp.config.target_file; await processIndex(file, index); _%>