// Name: Navigate Obsidian Markdown // Description: Browse and open Markdown files from a local Obsidian vault. // Author: johnlindquist // GitHub: johnlindquist import "@johnlindquist/kit" const VAULT_DIR = await env("OBSIDIAN_VAULT_DIR", async () => { return await selectFolder("Select your Obsidian vault folder") }) const pattern = path.join(VAULT_DIR, "**/*.md") const files = await globby(pattern) if (!files.length) { await div(md(`No Markdown files found in:\n\n- ${VAULT_DIR}\n\nMake sure your vault contains .md files.`)) exit() } const choices = files.map(f => ({ name: path.relative(VAULT_DIR, f), value: f, })) const filePath = await arg( { placeholder: "Search Obsidian notes", enter: "Open in Editor", onChoiceFocus: async (input, { focused }) => { if (!focused?.value) { await setPreview(md(`Select a note to preview`)) return } try { const content = await readFile(focused.value, "utf8") const trimmed = content.length > 6000 ? content.slice(0, 6000) + "\n\n..." : content await setPreview(md(trimmed || "_(Empty file)_")) } catch (e) { await setPreview(md(`Error reading file:\n\n- ${String(e)}`)) } }, actions: [ { name: "Reveal in Finder", shortcut: `${cmd}+shift+r`, onAction: async (input, { focused }) => { if (focused?.value) await revealFile(focused.value) }, bar: "right", }, { name: "Copy Path", shortcut: `${cmd}+shift+c`, onAction: async (input, { focused }) => { if (focused?.value) { await copy(focused.value) await toast("Path copied") } }, bar: "right", }, { name: "Change Vault", shortcut: `${cmd}+,`, onAction: async () => { const newDir = await selectFolder("Select your Obsidian vault folder") if (newDir) { // Persist new selection by updating the .env variable interactively await div(md(`Vault updated for this session:\n\n- ${newDir}\n\nRe-run the script to reload index.`)) } }, bar: "right", }, ], }, choices ) await edit(filePath)