MCPcopy Create free account
hub / github.com/UNLINEARITY/Obsidian-CodeSpace / resolveCodeEmbedReference

Function resolveCodeEmbedReference

src/code_embed_markdown.ts:157–221  ·  view source on GitHub ↗
(
	plugin: CodeSpacePlugin,
	rawReference: string,
	sourcePath: string,
	allowedExtensions: Set<string> = collectAllowedExtensions(plugin)
)

Source from the content-addressed store, hash-verified

155}
156
157export function resolveCodeEmbedReference(
158 plugin: CodeSpacePlugin,
159 rawReference: string,
160 sourcePath: string,
161 allowedExtensions: Set<string> = collectAllowedExtensions(plugin)
162): ResolvedCodeEmbed | null {
163 const trimmed = rawReference.trim();
164 if (!trimmed) return null;
165
166 const pipeIndex = trimmed.indexOf("|");
167 const reference = pipeIndex === -1 ? trimmed : trimmed.slice(0, pipeIndex).trim();
168 if (!reference) return null;
169
170 const hashIndex = reference.indexOf("#");
171 const filePath = hashIndex === -1 ? reference : reference.slice(0, hashIndex).trim();
172 const hashPart = hashIndex === -1 ? "" : reference.slice(hashIndex + 1).trim();
173 if (!filePath) return null;
174 const hadLeadingSlash = /^[\\/]/.test(filePath);
175
176 const lineFragment = parseLineFragment(hashPart);
177 if (hashPart && !lineFragment) {
178 return null;
179 }
180
181 const normalizedFilePath = normalizePath(filePath.replace(/\\/g, "/")).trim();
182 if (!normalizedFilePath) return null;
183
184 let file: TFile | null = null;
185
186 if (hadLeadingSlash) {
187 const rootPath = normalizedFilePath.replace(/^\/+/, "");
188 const exact = plugin.app.vault.getAbstractFileByPath(rootPath);
189 file = exact instanceof TFile ? exact : null;
190 }
191
192 if (!file) {
193 file = plugin.app.metadataCache.getFirstLinkpathDest(normalizedFilePath, sourcePath);
194 }
195
196 if (!file && normalizedFilePath.includes("/")) {
197 const exact = plugin.app.vault.getAbstractFileByPath(normalizedFilePath);
198 file = exact instanceof TFile ? exact : null;
199 }
200
201 if (!file && !normalizedFilePath.includes("/")) {
202 const fileNameLower = normalizedFilePath.toLowerCase();
203 file =
204 plugin.app.vault.getFiles().find((candidate) => candidate.name === normalizedFilePath) ??
205 plugin.app.vault.getFiles().find((candidate) => candidate.name.toLowerCase() === fileNameLower) ??
206 null;
207 }
208
209 if (!file) return null;
210
211 const ext = file.extension.toLowerCase();
212 if (!allowedExtensions.has(ext)) {
213 return null;
214 }

Callers 2

replacePopupCodeEmbedsFunction · 0.90

Calls 3

collectAllowedExtensionsFunction · 0.85
parseLineFragmentFunction · 0.85
replaceMethod · 0.80

Tested by

no test coverage detected