import type { App } from "obsidian"; import type { ParsedEdge } from "../parser/types"; import type { Vocabulary } from "../vocab/Vocabulary"; import type { Finding } from "./types"; import { parseEdgesFromCallouts } from "../parser/parseEdgesFromCallouts"; export interface LintOptions { showCanonicalHints?: boolean; } /** * Pure function to lint parsed edges against vocabulary and file existence. * This can be tested independently. */ export function lintParsedEdges( parsedEdges: ParsedEdge[], vocabulary: Vocabulary, existingFileNamesSet: Set, opts: LintOptions = {} ): Finding[] { const findings: Finding[] = []; const { showCanonicalHints = false } = opts; for (const edge of parsedEdges) { const normalized = vocabulary.normalize(edge.rawType); // R1: Check for unknown edge type (ERROR) if (normalized.canonical === null) { findings.push({ ruleId: "unknown_edge_type", severity: "ERROR", message: `Unknown edge type: "${edge.rawType}"`, filePath: "", // Will be set by caller lineStart: edge.lineStart, lineEnd: edge.lineEnd, evidence: edge.rawType, }); } else { // Optional: Show canonical hints (INFO) if (showCanonicalHints) { findings.push({ ruleId: "canonical_hint", severity: "INFO", message: `Edge type resolved: raw='${edge.rawType}' canonical='${normalized.canonical}'`, filePath: "", // Will be set by caller lineStart: edge.lineStart, lineEnd: edge.lineStart, evidence: edge.rawType, }); } } // R3: Check for edges without targets (WARN) if (edge.targets.length === 0) { findings.push({ ruleId: "edge_without_target", severity: "WARN", message: `Edge type "${edge.rawType}" has no target notes`, filePath: "", // Will be set by caller lineStart: edge.lineStart, lineEnd: edge.lineEnd, evidence: edge.rawType, }); } // R2: Check for missing target notes (WARN) // Check targets regardless of whether edge type is known or unknown for (const target of edge.targets) { if (!target) continue; // Check if file exists (exact match to markdown file name) // Target might be "Note Name" or "Note Name#heading" - we check the base name const parts = target.split("#"); const firstPart = parts[0]; if (!firstPart) continue; const baseName = firstPart.trim(); if (!baseName) continue; const markdownFileName = baseName.endsWith(".md") ? baseName : `${baseName}.md`; if (!existingFileNamesSet.has(markdownFileName) && !existingFileNamesSet.has(baseName)) { findings.push({ ruleId: "missing_target_note", severity: "WARN", message: `Target note "${target}" not found in vault`, filePath: "", // Will be set by caller lineStart: edge.lineStart, lineEnd: edge.lineEnd, evidence: target, }); } } } return findings; } /** * LintEngine for validating markdown notes containing edge definitions. */ export class LintEngine { /** * Lint the currently active markdown note. */ static async lintCurrentNote( app: App, vocabulary: Vocabulary, options: LintOptions = {} ): Promise { const activeFile = app.workspace.getActiveFile(); if (!activeFile) { throw new Error("No active file"); } if (activeFile.extension !== "md") { throw new Error("Active file is not a markdown file"); } // Read file content const content = await app.vault.read(activeFile); // Parse edges const parsedEdges = parseEdgesFromCallouts(content); // Build set of existing markdown file names in vault const existingFileNamesSet = new Set(); const markdownFiles = app.vault.getMarkdownFiles(); for (const file of markdownFiles) { existingFileNamesSet.add(file.name); // Also add without .md extension for matching if (file.name.endsWith(".md")) { const baseName = file.name.slice(0, -3); existingFileNamesSet.add(baseName); } } // Run pure linting logic const findings = lintParsedEdges(parsedEdges, vocabulary, existingFileNamesSet, options); // Set filePath for all findings const filePath = activeFile.path; for (const finding of findings) { finding.filePath = filePath; } return findings; } }