please analyze this code from an obsidian plugin and detect potential issues:…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
please analyze this code from an obsidian plugin and detect potential issues:
"dependencies": { "minisearch": "^7.1.2", "obsidian": "^1.8.7", "p-queue": "^8.1.0" }, code:
/* -------------------------------------------------------------------
import MiniSearch from "minisearch"; import { type Editor, type EditorPosition, EditorSuggest, type EditorSuggestContext, type EditorSuggestTriggerInfo, Plugin, setIcon, TFile, } from "obsidian"; import PQueue from "p-queue";
/* ------------------------------------------------------------------ / / Types / / ------------------------------------------------------------------ */
type IndexEntryType = "title" | "heading" | "block" | "tag";
/** What we store in MiniSearch. */ interface IndexEntry { readonly id: string; // composite key :: readonly type: IndexEntryType; readonly file: TFile; // live reference (never stale) readonly target: string; // original text (heading, block id, tag…) readonly displayText: string; }
/* ------------------------------------------------------------------ / / Constants / / ------------------------------------------------------------------ */
const SEARCH_LIMIT = 100; const DEBOUNCE_MS = 300;
/* ------------------------------------------------------------------ / / MiniSearch instance / / ------------------------------------------------------------------ */
let miniSearch = new MiniSearch({ idField: "id", fields: ["target", "displayText"], storeFields: ["type", "displayText", "file", "target"], searchOptions: { // sane defaults → can still be overridden prefix: true, fuzzy: 0.2, }, });
/* ------------------------------------------------------------------ / / Helper / / ------------------------------------------------------------------ */
type Timeout = ReturnType;
const iconMap: Record<IndexEntryType, string> = { title: "file-text", heading: "heading", block: "link", tag: "tag", };
/** Ignore “id not found” errors – everything else is re-thrown. / function safeRemoveId(id: string) { try { miniSearch.remove({ id } as IndexEntry); } catch (e) { if ( e instanceof Error && /cannot discard document .? it is not in the index/i.test(e.message) ) { // already gone → fine return; } throw e; // any other problem should surface } }
/* ------------------------------------------------------------------ / / Main plugin class / / ------------------------------------------------------------------ */
export default class EchoLink extends Plugin { private readonly queue = new PQueue({ concurrency: 1 }); private readonly debounceTimers = new Map<string, Timeout>();
}
/* ------------------------------------------------------------------ / / EditorSuggest implementation / / ------------------------------------------------------------------ */
class EchoLinkSuggest extends EditorSuggest { constructor(private readonly plugin: EchoLink) { super(plugin.app); }
/* -------- what triggers the suggester? -------- */
onTrigger(
cursor: EditorPosition,
editor: Editor,
): EditorSuggestTriggerInfo | null {
const line = editor.getLine(cursor.line);
if (!line) return null;
/* 1. ISO date yyyy-mm-dd → complete file titles that are dates */
const dateRE = /\b\d{4}-\d{2}-\d{2}\b/g;
for (const match of line.matchAll(dateRE)) {
const [full] = match;
const startIdx = match.index!;
const endIdx = startIdx + full.length;
if (cursor.ch >= startIdx && cursor.ch <= endIdx) {
return {
start: { line: cursor.line, ch: startIdx },
end: { line: cursor.line, ch: endIdx },
query: full,
};
}
}
/* 2. Generic “word under cursor” (unicode aware) */
const before = line.slice(0, cursor.ch);
const after = line.slice(cursor.ch);
const leftMatch = before.match(/([^\s]+)$/);
const rightMatch = after.match(/^([^\s]+)/);
const word = (leftMatch?.[1] ?? "") + (rightMatch?.[1] ?? "");
if (!word) return null;
const start = cursor.ch - (leftMatch?.[1]?.length ?? 0);
const end = cursor.ch + (rightMatch?.[1]?.length ?? 0);
return {
start: { line: cursor.line, ch: start },
end: { line: cursor.line, ch: end },
query: word,
};
}
/* -------- suggestions -------- */
async getSuggestions(ctx: EditorSuggestContext): Promise<IndexEntry[]> {
return this.plugin.search(ctx.query);
}
/* -------- render each suggestion -------- */
renderSuggestion(entry: IndexEntry, el: HTMLElement) {
const container = el.createDiv({ cls: "echo-link-suggestion" });
/* icon */
const icon = container.createDiv({ cls: "echo-link-icon" });
setIcon(icon, iconMap[entry.type]);
/* main text */
const text = container.createDiv({ cls: "echo-link-text" });
text.createEl("strong", { text: entry.displayText });
/* sub text */
container.createEl("small", {
text: `${entry.type} in ${entry.file.basename}`,
cls: "echo-link-sub",
});
}
/* -------- what happens when user chooses one -------- */
selectSuggestion(entry: IndexEntry): void {
if (!this.context) return;
const { editor, start, end, query } = this.context;
let link = "";
switch (entry.type) {
case "title":
link = `[[${entry.file.basename}|${query}]]`;
break;
case "heading":
link = `[[${entry.file.basename}#${entry.target}|${query}]]`;
break;
case "block":
link = `[[${entry.file.basename}#^${entry.target}|${query}]]`;
break;
case "tag":
link = `[[#${entry.target}|${query}]]`;
break;
}
editor.replaceRange(link, start, end);
this.close();
}
}
This is a well-structured plugin for Obsidian that provides enhanced intra-vault linking capabilities. Here's my analysis of potential issues:
Memory Leak Risk:
miniSearch instance is global, which could lead to memory leaks if the plugin is unloaded and reloaded without proper cleanup.onunload() method sets miniSearch to undefined with a @ts-expect-error, which is not ideal.Debounce Timer Management:
debounceTimers map could accumulate stale entries if files are renamed or deleted while timers are pending.onunload method does clear them).Error Handling:
safeRemoveId is good, but other operations (like file indexing) could benefit from more robust error handling.Performance Considerations:
search("") call to get all documents is inefficient (though it's only used during file removal/reindexing).Type Safety:
@ts-expect-error for miniSearch = undefined suggests a type mismatch that should be properly resolved.Edge Cases:
MiniSearch Configuration:
Memory Management:
miniSearch an instance variable rather than global.@ts-expect-error by properly typing the variable.Debounce Improvement:
/* ----------------------------- life-cycle ---------------------- */
async onload() {
console.log("[Echo-Link] loading …");
await this.buildFullIndex(); // initial indexing
/* ---------- vault / cache events ---------- */
this.registerEvent(
this.app.metadataCache.on("changed", (file) => this.scheduleUpdate(file)),
);
this.registerEvent(
this.app.vault.on("create", (file) => {
if (file instanceof TFile) this.scheduleUpdate(file);
}),
);
this.registerEvent(
this.app.vault.on("delete", (file) => {
if (file instanceof TFile) this.removeFile(file);
}),
);
/* ---------- editor suggester ---------- */
this.registerEditorSuggest(new EchoLinkSuggest(this));
}
onunload() {
miniSearch.removeAll(); // clear index
// throw the whole instance away – GC will release the store
// (not strictly needed, but explicit is nice)
// @ts-expect-error – we reassign in onload()
miniSearch = undefined;
this.queue.clear();
this.debounceTimers.forEach((timeout) => clearTimeout(timeout));
this.debounceTimers.clear();
}
/* ----------------------------- public API ---------------------- */
/** Called by the `EditorSuggest` to retrieve suggestions. */
public search(query: string): IndexEntry[] {
if (!query.trim()) return [];
return miniSearch
.search(query)
.slice(0, SEARCH_LIMIT)
.map((result) => ({
id: result.id,
type: result.type,
file: result.file,
target: result.target,
displayText: result.displayText,
score: result.score,
}));
}
/* ----------------------------- indexing ------------------------ */
/** Index **all** markdown files at once (called on start-up). */
private async buildFullIndex() {
await this.queue.add(async () => {
miniSearch.removeAll();
const files = this.app.vault.getMarkdownFiles();
for (const file of files) {
await this.indexFile(file);
}
});
}
/** Debounced “update this single file” entry-point. */
private scheduleUpdate(file: TFile) {
const existing = this.debounceTimers.get(file.path);
if (existing) clearTimeout(existing);
const timeout = setTimeout(() => {
this.debounceTimers.delete(file.path);
this.queue.add(() => this.indexFile(file));
}, DEBOUNCE_MS);
this.debounceTimers.set(file.path, timeout);
}
/* ------------------------------------------------------------ */
/* delete / invalidate a whole file */
/* ------------------------------------------------------------ */
private removeFile(file: TFile) {
// cancel any pending debounce for that path
const timer = this.debounceTimers.get(file.path);
if (timer) {
clearTimeout(timer);
this.debounceTimers.delete(file.path);
}
this.queue.add(() => {
const ids = miniSearch
.search("")
.filter((d) => d.file === file)
.map((d) => d.id);
ids.forEach(safeRemoveId);
});
}
/* ------------------------------------------------------------- */
/* 2. re-index a single file */
/* ------------------------------------------------------------- */
private async indexFile(file: TFile) {
if (file.extension !== "md") return;
/* ---- 2.1 wipe all previous entries for this file path ---- */
const idPrefix = `${file.path}:`;
const idsToRemove = miniSearch
.search("") // empty query returns all docs
.filter((d) => d.id.startsWith(idPrefix))
.map((d) => d.id);
idsToRemove.forEach(safeRemoveId);
/* ---- 2.2 build fresh entries ---- */
const meta = this.app.metadataCache.getFileCache(file) ?? {};
const title = file.basename;
const batch: IndexEntry[] = [];
const seenIds = new Set<string>(); // <- prevents dupes
const push = (e: Omit<IndexEntry, "id">) => {
const id = `${file.path}:${e.type}:${e.target}`;
if (seenIds.has(id)) return; // ignore duplicate
seenIds.add(id);
batch.push({ ...e, id });
};
/* -- title -- */
push({ type: "title", file, target: title, displayText: title });
/* -- tags (front-matter + inline) -- */
const allTags = new Set<string>(
(meta.tags ?? []).map((t) => t.tag.replace(/^#/, "")),
);
const fmTags = meta.frontmatter?.tags;
if (Array.isArray(fmTags)) fmTags.forEach((t) => allTags.add(t));
else if (typeof fmTags === "string")
fmTags
.split(",")
.map((t) => t.trim())
.forEach((t) => allTags.add(t));
allTags.forEach((tag) =>
push({ type: "tag", file, target: tag, displayText: `#${tag}` }),
);
/* -- headings -- */
meta.headings?.forEach((h) =>
push({
type: "heading",
file,
target: h.heading,
displayText: `${title} > ${h.heading}`,
}),
);
/* -- blocks -- */
meta.sections?.forEach((sec) => {
if (!sec.id) return;
push({
type: "block",
file,
target: sec.id,
displayText: `${title} > ^${sec.id}`,
});
});
/* ---- 2.3 add batch ---- */
try {
miniSearch.addAll(batch);
} catch (e) {
/* Fallback: replace any doc that is still considered a duplicate
(should be very rare now) */
if (e instanceof Error && /duplicate ID/i.test(e.message)) {
for (const doc of batch) miniSearch.replace(doc);
} else {
throw e; // something else went wrong → propagate
}
}
}
Performance Optimization:
Error Handling:
Configuration:
Testing:
Overall, this is a well-designed plugin with good separation of concerns. The main risks are around memory management and edge case handling, but the core functionality appears solid.