MCPcopy Index your code
hub / github.com/UNLINEARITY/Obsidian-CodeSpace / processCodeEmbed

Function processCodeEmbed

src/code_embed.ts:778–966  ·  view source on GitHub ↗
(embedEl: HTMLElement, plugin: CodeSpacePlugin, sourcePath: string, renderToken: number)

Source from the content-addressed store, hash-verified

776}
777
778async function processCodeEmbed(embedEl: HTMLElement, plugin: CodeSpacePlugin, sourcePath: string, renderToken: number) {
779 const effectiveSourcePath = sourcePath || resolveSourcePathForEmbed(embedEl, plugin) || "";
780 if (effectiveSourcePath) rememberSourcePath(embedEl, effectiveSourcePath);
781
782 // If another debounced run already rendered this embed for the same file, skip.
783 const lastRenderedFor = embedEl.getAttribute("data-code-space-rendered-for");
784
785 // Get the file path from the title element or src attribute
786 const titleEl = embedEl.querySelector('.file-embed-title');
787
788 // Prefer the embed title link; avoid picking unrelated links inside embed content.
789 const internalLink =
790 titleEl?.querySelector<HTMLAnchorElement>("a.internal-link") ??
791 embedEl.querySelector<HTMLAnchorElement>(".file-embed-title a.internal-link");
792
793 const vaultName = typeof plugin.app.vault.getName === "function" ? plugin.app.vault.getName() : "";
794 const stripVaultPrefix = (value: string) => {
795 const normalized = value.replace(/\\/g, "/").replace(/^\/+/, "");
796 if (!vaultName) return normalized;
797 if (normalized === vaultName) return "";
798 if (normalized.startsWith(`${vaultName}/`)) return normalized.slice(vaultName.length + 1);
799 return normalized;
800 };
801
802 const extractPathFromCandidate = (value: string | null | undefined): string => {
803 if (!value) return "";
804 let trimmed = value.trim();
805 if (!trimmed) return "";
806
807 // Handle Obsidian/app URLs and extract the path portion when possible.
808 if (/^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed)) {
809 try {
810 const url = new URL(trimmed);
811 if (url.protocol === "obsidian:" || url.protocol === "app:") {
812 const pathParam = url.searchParams.get("path") ?? url.searchParams.get("file");
813 if (pathParam) {
814 // Preserve hash fragment (line numbers) if present
815 const hashPart = url.hash || "";
816 return stripVaultPrefix(decodeURIComponent(pathParam)) + hashPart;
817 }
818
819 const pathFromApp = decodeURIComponent(url.pathname.replace(/^\/+/, ""));
820 if (pathFromApp && pathFromApp !== "open") {
821 const hashPart = url.hash || "";
822 return stripVaultPrefix(pathFromApp) + hashPart;
823 }
824 }
825
826 // Ignore other URL schemes.
827 return "";
828 } catch {
829 return "";
830 }
831 }
832
833 return trimmed;
834 };
835

Callers 1

scheduleProcessCodeEmbedFunction · 0.85

Calls 5

rememberSourcePathFunction · 0.85
extractPathFromCandidateFunction · 0.85
renderCodeEmbedFunction · 0.85
replaceMethod · 0.80

Tested by

no test coverage detected