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.
This commit is contained in:
1 parent
9b8550c387
commit
d577283af6
20 files changed
+2008
-208
No files matched your search
+39
-118
@@ -1,26 +1,30 @@
|
||||
import type { App, TFile } from "obsidian";
|
||||
import type { App } from "obsidian";
|
||||
import type { ParsedEdge } from "../parser/types";
|
||||
import type { Vocabulary } from "../vocab/Vocabulary";
|
||||
import type { Finding, QuickFix } from "./types";
|
||||
import type { Finding } from "./types";
|
||||
import { parseEdgesFromCallouts } from "../parser/parseEdgesFromCallouts";
|
||||
|
||||
const EDGE_HEADER_RE = /^\s*(>+)\s*\[!edge\]\s*(.+?)\s*$/i;
|
||||
export interface LintOptions {
|
||||
showCanonicalHints?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure function to lint parsed edges against vocabulary and file existence.
|
||||
* This can be tested independently.
|
||||
*/
|
||||
export function lintEdges(
|
||||
export function lintParsedEdges(
|
||||
parsedEdges: ParsedEdge[],
|
||||
vocabulary: Vocabulary,
|
||||
existingFilesSet: Set<string>
|
||||
existingFileNamesSet: Set<string>,
|
||||
opts: LintOptions = {}
|
||||
): Finding[] {
|
||||
const findings: Finding[] = [];
|
||||
const { showCanonicalHints = false } = opts;
|
||||
|
||||
for (const edge of parsedEdges) {
|
||||
const normalized = vocabulary.normalize(edge.rawType);
|
||||
|
||||
// Check for unknown edge type
|
||||
// R1: Check for unknown edge type (ERROR)
|
||||
if (normalized.canonical === null) {
|
||||
findings.push({
|
||||
ruleId: "unknown_edge_type",
|
||||
@@ -31,26 +35,36 @@ export function lintEdges(
|
||||
lineEnd: edge.lineEnd,
|
||||
evidence: edge.rawType,
|
||||
});
|
||||
continue;
|
||||
} 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Check for alias not normalized
|
||||
const rawLower = edge.rawType.trim().toLowerCase();
|
||||
const canonicalLower = normalized.canonical.toLowerCase();
|
||||
if (rawLower !== canonicalLower) {
|
||||
// R3: Check for edges without targets (WARN)
|
||||
if (edge.targets.length === 0) {
|
||||
findings.push({
|
||||
ruleId: "alias_not_normalized",
|
||||
ruleId: "edge_without_target",
|
||||
severity: "WARN",
|
||||
message: `Edge type "${edge.rawType}" should be normalized to "${normalized.canonical}"`,
|
||||
message: `Edge type "${edge.rawType}" has no target notes`,
|
||||
filePath: "", // Will be set by caller
|
||||
lineStart: edge.lineStart,
|
||||
lineEnd: edge.lineStart,
|
||||
lineEnd: edge.lineEnd,
|
||||
evidence: edge.rawType,
|
||||
quickFixes: [], // Will be populated by caller with file context
|
||||
});
|
||||
}
|
||||
|
||||
// Check for missing target notes
|
||||
// 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;
|
||||
|
||||
@@ -65,7 +79,7 @@ export function lintEdges(
|
||||
|
||||
const markdownFileName = baseName.endsWith(".md") ? baseName : `${baseName}.md`;
|
||||
|
||||
if (!existingFilesSet.has(markdownFileName) && !existingFilesSet.has(baseName)) {
|
||||
if (!existingFileNamesSet.has(markdownFileName) && !existingFileNamesSet.has(baseName)) {
|
||||
findings.push({
|
||||
ruleId: "missing_target_note",
|
||||
severity: "WARN",
|
||||
@@ -91,7 +105,8 @@ export class LintEngine {
|
||||
*/
|
||||
static async lintCurrentNote(
|
||||
app: App,
|
||||
vocabulary: Vocabulary
|
||||
vocabulary: Vocabulary,
|
||||
options: LintOptions = {}
|
||||
): Promise<Finding[]> {
|
||||
const activeFile = app.workspace.getActiveFile();
|
||||
|
||||
@@ -109,121 +124,27 @@ export class LintEngine {
|
||||
// Parse edges
|
||||
const parsedEdges = parseEdgesFromCallouts(content);
|
||||
|
||||
// Build set of existing markdown files in vault
|
||||
const existingFilesSet = new Set<string>();
|
||||
// Build set of existing markdown file names in vault
|
||||
const existingFileNamesSet = new Set<string>();
|
||||
const markdownFiles = app.vault.getMarkdownFiles();
|
||||
for (const file of markdownFiles) {
|
||||
existingFilesSet.add(file.name);
|
||||
existingFileNamesSet.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);
|
||||
existingFileNamesSet.add(baseName);
|
||||
}
|
||||
}
|
||||
|
||||
// Run pure linting logic
|
||||
const findings = lintEdges(parsedEdges, vocabulary, existingFilesSet);
|
||||
const findings = lintParsedEdges(parsedEdges, vocabulary, existingFileNamesSet, options);
|
||||
|
||||
// Set filePath and add quickfixes
|
||||
// Set filePath for all findings
|
||||
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