// Name: Search Obsidian Notes // Description: Search through local Obsidian vault files // Author: johnlindquist import "@johnlindquist/kit" // Get the Obsidian vault directory const vaultPath = await env("OBSIDIAN_VAULT_PATH", async () => { return await path({ hint: "Select your Obsidian vault directory", onlyDirs: true }) }) // Search for markdown files in the vault const searchNotes = async (query) => { if (!query) return [] const mdFiles = await globby([ path.join(vaultPath, "**/*.md") ]) const results = [] for (const filePath of mdFiles) { try { const content = await readFile(filePath, "utf8") const fileName = path.basename(filePath, ".md") // Check if query matches filename or content if (fileName.toLowerCase().includes(query.toLowerCase()) || content.toLowerCase().includes(query.toLowerCase())) { // Get a preview snippet around the match const lines = content.split('\n') let preview = lines.slice(0, 3).join(' ').substring(0, 100) // If query is in content, try to show context around it if (content.toLowerCase().includes(query.toLowerCase())) { const index = content.toLowerCase().indexOf(query.toLowerCase()) const start = Math.max(0, index - 50) const end = Math.min(content.length, index + 100) preview = content.substring(start, end) } results.push({ name: fileName, description: preview + "...", value: filePath, preview: () => md(content) }) } } catch (error) { // Skip files that can't be read continue } } return results } const selectedNote = await arg({ placeholder: "Search Obsidian notes...", enter: "Open Note", shortcuts: [ { name: "Open in Obsidian", key: `${cmd}+o`, onPress: async (input, { focused }) => { if (focused?.value) { const fileName = path.basename(focused.value, ".md") await exec(`open "obsidian://open?vault=${encodeURIComponent(path.basename(vaultPath))}&file=${encodeURIComponent(fileName)}"`) } }, bar: "right" }, { name: "Reveal in Finder", key: `${cmd}+r`, onPress: async (input, { focused }) => { if (focused?.value) { await revealFile(focused.value) } }, bar: "right" } ] }, searchNotes) if (selectedNote) { const content = await readFile(selectedNote, "utf8") await editor({ value: content, language: "markdown", onSubmit: async (updatedContent) => { await writeFile(selectedNote, updatedContent) await toast("Note saved!") } }) }