( plugin: CodeSpacePlugin, rawReference: string, sourcePath: string, allowedExtensions: Set<string> = collectAllowedExtensions(plugin) )
| 155 | } |
| 156 | |
| 157 | export 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 | } |
no test coverage detected