Enhance InterviewWizardModal and SectionEdgesOverviewModal for improved link handling
- Integrated LinkTargetPickerModal into InterviewWizardModal to allow users to select link targets dynamically, enhancing user experience during note linking. - Updated link handling logic to support full links and improved edge type resolution in SectionEdgesOverviewModal, ensuring accurate target type identification. - Refactored code to streamline the extraction of note links and their associated types, improving overall functionality and maintainability.
This commit is contained in:
1 parent
054cfcf82d
commit
c044d6e8db
4 files changed
+361
-92
No files matched your search
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Ermittelt Zieltyp für Note-Links: Note-Type (ganze Note) oder Sektionstyp (bei Note#Abschnitt).
|
||||
* Wird in Kantenübersicht und Toolbar-Link-Auswahl genutzt.
|
||||
*/
|
||||
|
||||
import type { App } from "obsidian";
|
||||
import { TFile } from "obsidian";
|
||||
|
||||
/** Link ohne Alias: "Note" oder "Note#Abschnitt" */
|
||||
export function parseNoteLink(linkTarget: string): { basename: string; heading: string | null } {
|
||||
const withoutAlias = linkTarget.split("|")[0]?.trim() || linkTarget;
|
||||
const sharp = withoutAlias.indexOf("#");
|
||||
if (sharp === -1) {
|
||||
return { basename: withoutAlias.trim(), heading: null };
|
||||
}
|
||||
const basename = withoutAlias.slice(0, sharp).trim();
|
||||
const heading = withoutAlias.slice(sharp + 1).trim() || null;
|
||||
return { basename, heading };
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest aus Dateiinhalt den Sektionstyp für eine Überschrift.
|
||||
* Erwartet Format: ## Überschrift (optional ^block-id), nächste Zeile > [!section] typ
|
||||
*/
|
||||
export function getSectionTypeForHeading(content: string, heading: string): string | null {
|
||||
const lines = content.split("\n");
|
||||
const headingNorm = heading.trim().toLowerCase();
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line === undefined) continue;
|
||||
const match = line.match(/^(#{1,6})\s+(.+?)(?:\s+\^[\w-]+)?\s*$/);
|
||||
if (match && match[2]) {
|
||||
const lineHeading = match[2].trim();
|
||||
if (lineHeading.toLowerCase() === headingNorm) {
|
||||
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
|
||||
const nextLine = lines[j];
|
||||
if (nextLine === undefined) continue;
|
||||
const sectionMatch = nextLine.match(/^\s*>\s*\[!section\]\s+(.+)\s*$/);
|
||||
if (sectionMatch && sectionMatch[1]) {
|
||||
return sectionMatch[1].trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export interface ResolvedTargetType {
|
||||
targetType: string | null;
|
||||
displayLabel: string;
|
||||
basename: string;
|
||||
heading: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt Zieltyp für einen Note-Link (ganze Note → Note-Type, mit #Abschnitt → Sektionstyp).
|
||||
* @param app Obsidian App
|
||||
* @param linkTarget z.B. "Note" oder "Note#Abschnitt" (ohne Alias)
|
||||
* @param sourcePath Pfad der Quelldatei für getFirstLinkpathDest
|
||||
*/
|
||||
export async function resolveTargetTypeForNoteLink(
|
||||
app: App,
|
||||
linkTarget: string,
|
||||
sourcePath: string
|
||||
): Promise<ResolvedTargetType> {
|
||||
const { basename, heading } = parseNoteLink(linkTarget);
|
||||
const displayLabel = heading ? `${basename}#${heading}` : basename;
|
||||
|
||||
let targetType: string | null = null;
|
||||
try {
|
||||
const targetFile = app.metadataCache.getFirstLinkpathDest(basename, sourcePath);
|
||||
if (!targetFile || !(targetFile instanceof TFile)) {
|
||||
return { targetType: null, displayLabel, basename, heading };
|
||||
}
|
||||
|
||||
if (heading) {
|
||||
const content = await app.vault.read(targetFile);
|
||||
targetType = getSectionTypeForHeading(content, heading);
|
||||
}
|
||||
|
||||
if (targetType === null) {
|
||||
const cache = app.metadataCache.getFileCache(targetFile);
|
||||
if (cache?.frontmatter) {
|
||||
const t = cache.frontmatter.type ?? cache.frontmatter.noteType;
|
||||
targetType = t != null ? String(t) : null;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return { targetType, displayLabel, basename, heading };
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest aus einer Note alle Überschriften mit zugehörigem Sektionstyp (falls vorhanden).
|
||||
* Für Toolbar-Auswahl: "Ganze Note" oder "## Überschrift (typ)".
|
||||
*/
|
||||
export interface HeadingWithType {
|
||||
heading: string;
|
||||
sectionType: string | null;
|
||||
level: number;
|
||||
}
|
||||
|
||||
export async function getHeadingsWithSectionTypes(
|
||||
app: App,
|
||||
file: TFile
|
||||
): Promise<HeadingWithType[]> {
|
||||
const content = await app.vault.read(file);
|
||||
const lines = content.split("\n");
|
||||
const result: HeadingWithType[] = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line === undefined) continue;
|
||||
const match = line.match(/^(#{1,6})\s+(.+?)(?:\s+\^[\w-]+)?\s*$/);
|
||||
if (match && match[1] && match[2]) {
|
||||
const level = match[1].length;
|
||||
const heading = match[2].trim();
|
||||
let sectionType: string | null = null;
|
||||
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
|
||||
const nextLine = lines[j];
|
||||
if (nextLine === undefined) continue;
|
||||
const sectionMatch = nextLine.match(/^\s*>\s*\[!section\]\s+(.+)\s*$/);
|
||||
if (sectionMatch && sectionMatch[1]) {
|
||||
sectionType = sectionMatch[1].trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
result.push({ heading, sectionType, level });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user