- Updated `inspectChains` to accept optional `editorContent`, allowing for real-time inspection without relying on potentially stale vault data. - Introduced `buildNoteIndexFromContent` to facilitate graph indexing directly from provided content. - Improved handling of template matching profiles in `ChainWorkbenchModal`, ensuring accurate context during chain inspections. - Added debug logging for better traceability of the chain inspection process.
198 lines
5.3 KiB
TypeScript
198 lines
5.3 KiB
TypeScript
/**
|
|
* Graph index for single-note scope with optional neighbor loading.
|
|
*/
|
|
|
|
import type { App } from "obsidian";
|
|
import { TFile } from "obsidian";
|
|
import { splitIntoSections } from "../mapping/sectionParser";
|
|
import { parseEdgesFromCallouts } from "../parser/parseEdgesFromCallouts";
|
|
import { normalizeLinkTarget } from "../unresolvedLink/linkHelpers";
|
|
import type { NoteSection } from "../mapping/sectionParser";
|
|
|
|
export interface EdgeTarget {
|
|
file: string;
|
|
heading: string | null;
|
|
}
|
|
|
|
export interface IndexedEdge {
|
|
rawEdgeType: string;
|
|
source: { file: string; sectionHeading: string | null } | { file: string };
|
|
target: EdgeTarget;
|
|
scope: "section" | "note" | "candidate";
|
|
evidence: {
|
|
file: string;
|
|
sectionHeading: string | null;
|
|
lineRange?: { start: number; end: number };
|
|
};
|
|
}
|
|
|
|
export interface SectionNode {
|
|
file: string;
|
|
heading: string | null;
|
|
}
|
|
|
|
const NOTE_LINKS_HEADING = "Note-Verbindungen";
|
|
const CANDIDATES_HEADING = "Kandidaten";
|
|
|
|
/** Intra-note block reference: [[#^block-id]] or [[#^block-id|alias]] */
|
|
const INTRA_NOTE_BLOCK_REF_RE = /^#\^([a-zA-Z0-9_-]+)(?:\s*\|.*)?$/;
|
|
|
|
/**
|
|
* Resolve [[#^block-id]] to (currentFile, sectionHeading) using section blockIds.
|
|
* Returns null if not an intra-note block ref.
|
|
*/
|
|
function resolveIntraNoteBlockRef(
|
|
linkText: string,
|
|
currentFilePath: string,
|
|
sections: NoteSection[]
|
|
): EdgeTarget | null {
|
|
const trimmed = linkText.trim();
|
|
const match = trimmed.match(INTRA_NOTE_BLOCK_REF_RE);
|
|
if (!match) return null;
|
|
const blockId = match[1];
|
|
const section = sections.find((s) => s.blockId === blockId);
|
|
return {
|
|
file: currentFilePath,
|
|
heading: section?.heading ?? null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Parse target from link text: [[file]], [[file#Heading]], or [[#^block-id]] (intra-note).
|
|
*/
|
|
function parseTarget(linkText: string, currentFilePath: string, sections: NoteSection[]): EdgeTarget {
|
|
const intraNote = resolveIntraNoteBlockRef(linkText, currentFilePath, sections);
|
|
if (intraNote) return intraNote;
|
|
|
|
const normalized = normalizeLinkTarget(linkText);
|
|
const parts = linkText.split("#");
|
|
|
|
if (parts.length > 1) {
|
|
const file = normalized;
|
|
const headingPart = parts[1]?.split("|")[0]?.trim();
|
|
return {
|
|
file,
|
|
heading: headingPart || null,
|
|
};
|
|
}
|
|
|
|
return {
|
|
file: normalized,
|
|
heading: null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Build graph index for a single note from file content.
|
|
* Use buildNoteIndexFromContent if you have the content already (e.g., from editor).
|
|
*/
|
|
export async function buildNoteIndex(
|
|
app: App,
|
|
file: TFile
|
|
): Promise<{ edges: IndexedEdge[]; sections: SectionNode[] }> {
|
|
const content = await app.vault.read(file);
|
|
return buildNoteIndexFromContent(app, file, content);
|
|
}
|
|
|
|
/**
|
|
* Build graph index for a single note from provided content.
|
|
* This is useful when you have the content from an editor and want to avoid
|
|
* reading stale data from the vault cache.
|
|
*/
|
|
export async function buildNoteIndexFromContent(
|
|
app: App,
|
|
file: TFile,
|
|
content: string
|
|
): Promise<{ edges: IndexedEdge[]; sections: SectionNode[] }> {
|
|
const sections = splitIntoSections(content);
|
|
const edges: IndexedEdge[] = [];
|
|
const sectionNodes: SectionNode[] = [];
|
|
|
|
// Create section nodes
|
|
for (const section of sections) {
|
|
sectionNodes.push({
|
|
file: file.path,
|
|
heading: section.heading,
|
|
});
|
|
}
|
|
|
|
// Determine zone kind for each section
|
|
for (let i = 0; i < sections.length; i++) {
|
|
const section = sections[i];
|
|
if (!section) continue;
|
|
|
|
let scope: "section" | "note" | "candidate" = "section";
|
|
if (section.heading === NOTE_LINKS_HEADING) {
|
|
scope = "note";
|
|
} else if (section.heading === CANDIDATES_HEADING) {
|
|
scope = "candidate";
|
|
}
|
|
|
|
// Parse edges from section content
|
|
const parsedEdges = parseEdgesFromCallouts(section.content);
|
|
|
|
for (const parsedEdge of parsedEdges) {
|
|
// Determine source context
|
|
const source =
|
|
scope === "note"
|
|
? { file: file.path }
|
|
: { file: file.path, sectionHeading: section.heading };
|
|
|
|
// Process each target (resolve [[#^block-id]] to same-note section)
|
|
for (const targetLink of parsedEdge.targets) {
|
|
const target = parseTarget(targetLink, file.path, sections);
|
|
|
|
edges.push({
|
|
rawEdgeType: parsedEdge.rawType,
|
|
source,
|
|
target,
|
|
scope,
|
|
evidence: {
|
|
file: file.path,
|
|
sectionHeading: section.heading,
|
|
lineRange: {
|
|
start: section.startLine + parsedEdge.lineStart,
|
|
end: section.startLine + parsedEdge.lineEnd,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return { edges, sections: sectionNodes };
|
|
}
|
|
|
|
/**
|
|
* Load neighbor note if needed (for incoming edge detection).
|
|
*/
|
|
export async function loadNeighborNote(
|
|
app: App,
|
|
targetFile: string,
|
|
sourceFile?: string
|
|
): Promise<TFile | null> {
|
|
try {
|
|
// Try direct path first
|
|
const file = app.vault.getAbstractFileByPath(targetFile);
|
|
if (file instanceof TFile) {
|
|
return file;
|
|
}
|
|
} catch {
|
|
// File not found via direct path
|
|
}
|
|
|
|
// If not found and sourceFile provided, try resolving as wikilink
|
|
if (sourceFile) {
|
|
try {
|
|
const resolved = app.metadataCache.getFirstLinkpathDest(targetFile, sourceFile);
|
|
if (resolved && resolved instanceof TFile) {
|
|
return resolved;
|
|
}
|
|
} catch {
|
|
// Resolution failed
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|