Files
mindnet_obsidian/src/lint/LintEngine.ts
T
Lars d577283af6
Node.js build / build (20.x) (push) Waiting to run
Node.js build / build (22.x) (push) Waiting to run
Add graph export and chain traversal commands; enhance linting options
- Introduced commands for exporting graph data and displaying chains from the current note.
- Enhanced linting functionality with options for showing canonical hints and specifying chain traversal direction.
- Added new utility functions for graph traversal and index building.
- Updated settings interface to include new options for user configuration.
2026-01-15 12:27:16 +01:00

151 lines
4.5 KiB
TypeScript

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<string>,
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<Finding[]> {
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<string>();
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;
}
}