import { dirname } from "node:path"; import type { ObsidianCliConfig, PermissionMode } from "../config.ts"; const WIKILINK_RE = /^\[\[([^\]|#]+)(?:[|#][^\]]+)?\]\]$/; export function toolNameFor(command: string): string { return `obsidian_${command.replace(/:/g, "_")}`; } export function parseWikilink(raw: string): string | null { const match = raw.trim().match(WIKILINK_RE); return match ? match[1] : null; } export function injectContextDefaults( cmdName: string, params: Record, config: ObsidianCliConfig, activeFile: { path: string; name: string } | null, ): { params: Record; defaulted: boolean } { if (config.contextMode !== "active-tab" || !activeFile) return { params, defaulted: false }; if (params.file !== undefined || params.path !== undefined) return { params, defaulted: false }; const usesActiveFile = new Set([ "read", "file", "outline", "properties", "property:read", "links", "backlinks", "tags", "tasks", "history", "diff", ]); if (usesActiveFile.has(cmdName)) { return { params: { ...params, file: activeFile.path }, defaulted: true }; } if (cmdName === "files") { const parent = dirname(activeFile.path); if (parent !== ".") return { params: { ...params, folder: parent }, defaulted: true }; } return { params, defaulted: false }; } export function isBlockedOpenPath(command: string, args: string[]): boolean { if (command !== "open" && command !== "tab:open") return false; return args.some((arg) => { const eq = arg.indexOf("="); if (eq === -1) return false; const key = arg.slice(0, eq); const value = arg.slice(eq + 1).toLowerCase(); return (key === "path" || key === "file") && value.endsWith(".svg"); }); } export function requiresConfirmation( options: { confirm?: boolean; fixedScript?: boolean } | undefined, risk: "read" | "write" | "danger", config: ObsidianCliConfig, ): boolean { return options?.confirm !== false && (!options?.fixedScript || options.confirm === true) && risk !== "read" && config.confirmDestructive; } export function effectivePermissionMode( sessionMode: PermissionMode | undefined, config: ObsidianCliConfig, ): PermissionMode { return sessionMode ?? config.permissionMode; }