|
| 1 | +export class AssetsBaseUrlResolver { |
| 2 | + constructor({ documentRef = typeof document !== 'undefined' ? document : null, moduleUrl = import.meta.url } = {}) { |
| 3 | + this.documentRef = documentRef; |
| 4 | + this.moduleUrl = moduleUrl; |
| 5 | + this._cachedBaseUrl = null; |
| 6 | + } |
| 7 | + |
| 8 | + getBaseUrl() { |
| 9 | + if (this._cachedBaseUrl) return this._cachedBaseUrl; |
| 10 | + const fromEmbed = this._resolveFromEmbedMarker(); |
| 11 | + const fromModule = this._resolveFromModuleUrl(); |
| 12 | + const fallback = '/assets/'; |
| 13 | + this._cachedBaseUrl = this._normalizeBaseUrl(fromEmbed || fromModule || fallback); |
| 14 | + return this._cachedBaseUrl; |
| 15 | + } |
| 16 | + |
| 17 | + resolve(assetPath = '') { |
| 18 | + if (!assetPath) return this.getBaseUrl(); |
| 19 | + if (this._isAbsolute(assetPath)) return assetPath; |
| 20 | + const baseUrl = this.getBaseUrl(); |
| 21 | + const normalizedBase = this._normalizeBaseUrl(baseUrl); |
| 22 | + const trimmedPath = this._stripAssetsPrefix(assetPath).replace(/^\/+/, ''); |
| 23 | + return `${normalizedBase}${trimmedPath}`; |
| 24 | + } |
| 25 | + |
| 26 | + _resolveFromEmbedMarker() { |
| 27 | + if (!this.documentRef) return null; |
| 28 | + const candidates = []; |
| 29 | + if (this.documentRef.currentScript) { |
| 30 | + candidates.push(this.documentRef.currentScript); |
| 31 | + } |
| 32 | + if (this.documentRef.querySelector) { |
| 33 | + const explicit = this.documentRef.querySelector('[data-interdead-assets-base]'); |
| 34 | + if (explicit) candidates.push(explicit); |
| 35 | + } |
| 36 | + for (const candidate of candidates) { |
| 37 | + const attr = candidate.getAttribute?.('data-interdead-assets-base'); |
| 38 | + const dataset = candidate.dataset?.interdeadAssetsBase; |
| 39 | + const value = attr || dataset; |
| 40 | + if (value && value.trim()) return value.trim(); |
| 41 | + } |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + _resolveFromModuleUrl() { |
| 46 | + if (!this.moduleUrl) return null; |
| 47 | + const url = new URL(this.moduleUrl); |
| 48 | + const marker = '/assets/'; |
| 49 | + const index = url.pathname.lastIndexOf(marker); |
| 50 | + if (index === -1) return null; |
| 51 | + const basePath = url.pathname.slice(0, index + marker.length); |
| 52 | + return `${url.origin}${basePath}`; |
| 53 | + } |
| 54 | + |
| 55 | + _normalizeBaseUrl(baseUrl) { |
| 56 | + if (!baseUrl) return '/assets/'; |
| 57 | + return baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`; |
| 58 | + } |
| 59 | + |
| 60 | + _stripAssetsPrefix(assetPath) { |
| 61 | + if (assetPath.startsWith('/assets/')) return assetPath.slice('/assets/'.length); |
| 62 | + if (assetPath.startsWith('assets/')) return assetPath.slice('assets/'.length); |
| 63 | + return assetPath; |
| 64 | + } |
| 65 | + |
| 66 | + _isAbsolute(value) { |
| 67 | + return /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(value); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export class AssetUrlMapper { |
| 72 | + constructor(resolver = null) { |
| 73 | + this.resolver = resolver || new AssetsBaseUrlResolver(); |
| 74 | + } |
| 75 | + |
| 76 | + mapConfig(value) { |
| 77 | + return this._mapValue(value); |
| 78 | + } |
| 79 | + |
| 80 | + _mapValue(value) { |
| 81 | + if (Array.isArray(value)) { |
| 82 | + return value.map(item => this._mapValue(item)); |
| 83 | + } |
| 84 | + if (value && typeof value === 'object') { |
| 85 | + return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, this._mapValue(val)])); |
| 86 | + } |
| 87 | + if (typeof value === 'string') { |
| 88 | + return this._mapString(value); |
| 89 | + } |
| 90 | + return value; |
| 91 | + } |
| 92 | + |
| 93 | + _mapString(value) { |
| 94 | + if (value.startsWith('/assets/') || value.startsWith('assets/')) { |
| 95 | + const stripped = value.replace(/^\/?assets\//, ''); |
| 96 | + return this.resolver.resolve(stripped); |
| 97 | + } |
| 98 | + return value; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export const assetsBaseUrlResolver = new AssetsBaseUrlResolver(); |
| 103 | +export const assetsBaseUrl = assetsBaseUrlResolver.getBaseUrl(); |
| 104 | +export const resolveAssetUrl = assetPath => assetsBaseUrlResolver.resolve(assetPath); |
| 105 | +export const assetUrlMapper = new AssetUrlMapper(assetsBaseUrlResolver); |
0 commit comments