Refactor plugin to Mindnet Causal Assistant with enhanced vocabulary management and linting features. Updated manifest and package files to reflect new plugin details and dependencies. Added commands for reloading vocabulary and validating notes.
This commit is contained in:
1 parent
dc2fa22c4d
commit
9b8550c387
31 files changed
+3133
-320
No files matched your search
@@ -0,0 +1,229 @@
|
||||
import type { App, TFile } from "obsidian";
|
||||
import type { ParsedEdge } from "../parser/types";
|
||||
import type { Vocabulary } from "../vocab/Vocabulary";
|
||||
import type { Finding, QuickFix } from "./types";
|
||||
import { parseEdgesFromCallouts } from "../parser/parseEdgesFromCallouts";
|
||||
|
||||
const EDGE_HEADER_RE = /^\s*(>+)\s*\[!edge\]\s*(.+?)\s*$/i;
|
||||
|
||||
/**
|
||||
* Pure function to lint parsed edges against vocabulary and file existence.
|
||||
* This can be tested independently.
|
||||
*/
|
||||
export function lintEdges(
|
||||
parsedEdges: ParsedEdge[],
|
||||
vocabulary: Vocabulary,
|
||||
existingFilesSet: Set<string>
|
||||
): Finding[] {
|
||||
const findings: Finding[] = [];
|
||||
|
||||
for (const edge of parsedEdges) {
|
||||
const normalized = vocabulary.normalize(edge.rawType);
|
||||
|
||||
// Check for unknown edge type
|
||||
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,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for alias not normalized
|
||||
const rawLower = edge.rawType.trim().toLowerCase();
|
||||
const canonicalLower = normalized.canonical.toLowerCase();
|
||||
if (rawLower !== canonicalLower) {
|
||||
findings.push({
|
||||
ruleId: "alias_not_normalized",
|
||||
severity: "WARN",
|
||||
message: `Edge type "${edge.rawType}" should be normalized to "${normalized.canonical}"`,
|
||||
filePath: "", // Will be set by caller
|
||||
lineStart: edge.lineStart,
|
||||
lineEnd: edge.lineStart,
|
||||
evidence: edge.rawType,
|
||||
quickFixes: [], // Will be populated by caller with file context
|
||||
});
|
||||
}
|
||||
|
||||
// Check for missing target notes
|
||||
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 (!existingFilesSet.has(markdownFileName) && !existingFilesSet.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
|
||||
): 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 files in vault
|
||||
const existingFilesSet = new Set<string>();
|
||||
const markdownFiles = app.vault.getMarkdownFiles();
|
||||
for (const file of markdownFiles) {
|
||||
existingFilesSet.add(file.name);
|
||||
// Also add without .md extension for matching
|
||||
if (file.name.endsWith(".md")) {
|
||||
const baseName = file.name.slice(0, -3);
|
||||
existingFilesSet.add(baseName);
|
||||
}
|
||||
}
|
||||
|
||||
// Run pure linting logic
|
||||
const findings = lintEdges(parsedEdges, vocabulary, existingFilesSet);
|
||||
|
||||
// Set filePath and add quickfixes
|
||||
const filePath = activeFile.path;
|
||||
const lines = content.split(/\r?\n/);
|
||||
|
||||
for (const finding of findings) {
|
||||
finding.filePath = filePath;
|
||||
|
||||
// Add quickfix for alias_not_normalized
|
||||
if (finding.ruleId === "alias_not_normalized" && finding.lineStart !== undefined) {
|
||||
const lineIndex = finding.lineStart;
|
||||
const line = lines[lineIndex];
|
||||
|
||||
if (line) {
|
||||
const normalized = vocabulary.normalize(finding.evidence || "");
|
||||
if (normalized.canonical) {
|
||||
finding.quickFixes = [
|
||||
createNormalizeQuickFix(
|
||||
app,
|
||||
activeFile,
|
||||
content,
|
||||
lineIndex,
|
||||
finding.evidence || "",
|
||||
normalized.canonical
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return findings;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a quickfix that normalizes an edge type in the file.
|
||||
*/
|
||||
function createNormalizeQuickFix(
|
||||
app: App,
|
||||
file: TFile,
|
||||
currentContent: string,
|
||||
lineIndex: number,
|
||||
rawType: string,
|
||||
canonical: string
|
||||
): QuickFix {
|
||||
return {
|
||||
id: "normalize_edge_type",
|
||||
title: `Normalize to "${canonical}"`,
|
||||
apply: async () => {
|
||||
const { Notice } = await import("obsidian");
|
||||
const lines = currentContent.split(/\r?\n/);
|
||||
const line = lines[lineIndex];
|
||||
|
||||
if (!line) {
|
||||
new Notice("Line not found");
|
||||
return;
|
||||
}
|
||||
|
||||
// Match the edge header pattern
|
||||
const match = line.match(EDGE_HEADER_RE);
|
||||
if (!match || !match[2]) {
|
||||
new Notice("Edge header pattern not found on line");
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the position of the raw type in the line
|
||||
// match[2] is the captured type, but we need to find where it appears in the original line
|
||||
const edgeMarker = "[!edge]";
|
||||
const edgeIndex = line.indexOf(edgeMarker);
|
||||
if (edgeIndex === -1) {
|
||||
new Notice("Edge marker not found on line");
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the type after [!edge]
|
||||
const afterEdge = line.substring(edgeIndex + edgeMarker.length);
|
||||
const typeMatch = afterEdge.match(/^\s+(\S+)/);
|
||||
if (!typeMatch || typeMatch[1] !== rawType.trim()) {
|
||||
new Notice("Type token not found at expected position");
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace the raw type with canonical
|
||||
const beforeType = line.substring(0, edgeIndex + edgeMarker.length + typeMatch[0].indexOf(typeMatch[1]));
|
||||
const afterType = line.substring(beforeType.length + typeMatch[1].length);
|
||||
const newLine = beforeType + canonical + afterType;
|
||||
|
||||
// Safety check: verify the new line still matches the pattern
|
||||
const verifyMatch = newLine.match(EDGE_HEADER_RE);
|
||||
if (!verifyMatch) {
|
||||
new Notice("Quickfix would produce invalid line - skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the line
|
||||
lines[lineIndex] = newLine;
|
||||
const newContent = lines.join("\n");
|
||||
|
||||
// Write back to file
|
||||
await app.vault.modify(file, newContent);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user