MCPcopy
hub / github.com/obsidianmd/obsidian-help / fixFilenameLinks

Function fixFilenameLinks

scripts/translate-locale.ts:369–382  ·  view source on GitHub ↗
(content: string, enToLocale: Map<string, string>)

Source from the content-addressed store, hash-verified

367
368// Rewrite [[EN filename]] → [[FR filename]] in translated content
369function fixFilenameLinks(content: string, enToLocale: Map<string, string>): string {
370 if (enToLocale.size === 0) return content;
371 // Match [[target]], [[target|display]], [[target\|display]] (escaped pipe in tables), ![[target]], also with #anchors
372 return content.replace(/(!?\[\[)([^\]#|]+)(#[^\]|\\]+)?((?:\\\||[|])[^\]]+)?(\]\])/g, (match, open, target, anchor, display, close) => {
373 const trimmedTarget = target.trim();
374 // Preserve path prefix if any (e.g. folder/file)
375 const lastSlash = trimmedTarget.lastIndexOf("/");
376 const prefix = lastSlash >= 0 ? trimmedTarget.slice(0, lastSlash + 1) : "";
377 const basename = lastSlash >= 0 ? trimmedTarget.slice(lastSlash + 1) : trimmedTarget;
378 const localeBasename = enToLocale.get(basename.toLowerCase());
379 if (!localeBasename) return match;
380 return `${open}${prefix}${localeBasename}${anchor ?? ""}${display ?? ""}${close}`;
381 });
382}
383
384// ─── LLM ─────────────────────────────────────────────────────────────────────
385

Callers 2

saveTranslatedFunction · 0.70
walkFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected