{"version":3,"file":"rewrite-url-helpers.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/import/wordpress/rewrite-url-helpers.ts"],"sourcesContent":["const REGEX_SPECIAL_CHARS = /[.*+?^${}()|[\\]\\\\]/g;\nconst WORDPRESS_IMAGE_SIZE_SUFFIX = /-\\d+x\\d+(?=\\.[^./?#]+$)/;\nconst BASE_URL_EXTENSION = /^(.+)(\\.[^./?#]+)$/;\n\n/**\n * Strip query parameters from a URL for base matching\n */\nexport function getBaseUrl(url: string): string {\n\ttry {\n\t\tconst parsed = new URL(url);\n\t\treturn `${parsed.origin}${parsed.pathname}`;\n\t} catch {\n\t\t// If URL parsing fails, try simple string split\n\t\treturn url.split(\"?\")[0] || url;\n\t}\n}\n\n/**\n * Build a map of base URLs to new URLs for flexible matching\n */\nexport function buildBaseUrlMap(urlMap: Record<string, string>): Map<string, string> {\n\tconst baseMap = new Map<string, string>();\n\tfor (const [oldUrl, newUrl] of Object.entries(urlMap)) {\n\t\tconst baseUrl = getBaseUrl(oldUrl);\n\t\tbaseMap.set(baseUrl, newUrl);\n\t}\n\treturn baseMap;\n}\n\n/**\n * Extract the URL to match from a stored media field value.\n *\n * Image/file columns hold a JSON-stringified MediaValue\n * (e.g. `{\"provider\":\"external\",\"id\":\"\",\"src\":\"https://.../hero.jpg\"}`), but legacy\n * rows may hold a bare URL string. Returns the inner `src` for a MediaValue, otherwise\n * the value unchanged. Without this, the whole JSON blob is passed to findMatchingUrl()\n * and the embedded URL is never matched.\n */\nexport function extractMediaUrl(value: string): string {\n\ttry {\n\t\t// eslint-disable-next-line typescript/no-unsafe-type-assertion -- shape validated below\n\t\tconst parsed = JSON.parse(value) as { src?: unknown };\n\t\tif (parsed && typeof parsed.src === \"string\") {\n\t\t\treturn parsed.src;\n\t\t}\n\t} catch {\n\t\t// Not JSON — treat the column value as a bare URL.\n\t}\n\treturn value;\n}\n\n/**\n * Find matching new URL for a given URL, checking exact, base, and WordPress image-size matches\n */\nexport function findMatchingUrl(\n\turl: string,\n\texactMap: Record<string, string>,\n\tbaseMap: Map<string, string>,\n): string | null {\n\tif (exactMap[url]) {\n\t\treturn exactMap[url];\n\t}\n\n\tconst baseUrl = getBaseUrl(url);\n\tconst baseMatch = baseMap.get(baseUrl);\n\tif (baseMatch) {\n\t\treturn baseMatch;\n\t}\n\n\tconst wordPressImageMatch = baseMap.get(stripWordPressImageSizeSuffix(baseUrl));\n\tif (wordPressImageMatch) {\n\t\treturn wordPressImageMatch;\n\t}\n\n\treturn null;\n}\n\n/**\n * Portable Text block type (simplified for URL rewriting)\n */\nexport interface PortableTextBlock {\n\t_type: string;\n\t_key?: string;\n\tasset?: {\n\t\t_type?: string;\n\t\t_ref?: string;\n\t\turl?: string;\n\t};\n\tlink?: string;\n\t// For nested content like galleries\n\timages?: PortableTextBlock[];\n\tcolumns?: Array<{ content?: PortableTextBlock[] }>;\n\t[key: string]: unknown;\n}\n\n/**\n * Rewrite URLs in a Portable Text array, returning whether any changes were made\n */\nexport function rewritePortableTextUrls(\n\tblocks: PortableTextBlock[],\n\texactMap: Record<string, string>,\n\tbaseMap: Map<string, string>,\n): { changed: boolean; urlsRewritten: number } {\n\tlet changed = false;\n\tlet urlsRewritten = 0;\n\n\tfor (const block of blocks) {\n\t\t// Handle image blocks\n\t\tif (block._type === \"image\" && block.asset?.url) {\n\t\t\tconst newUrl = findMatchingUrl(block.asset.url, exactMap, baseMap);\n\t\t\tif (newUrl) {\n\t\t\t\tblock.asset.url = newUrl;\n\t\t\t\tblock.asset._ref = newUrl; // Also update the reference\n\t\t\t\tchanged = true;\n\t\t\t\turlsRewritten++;\n\t\t\t}\n\t\t}\n\n\t\t// Handle image link URLs (for linked images)\n\t\tif (block._type === \"image\" && block.link) {\n\t\t\tconst newUrl = findMatchingUrl(block.link, exactMap, baseMap);\n\t\t\tif (newUrl) {\n\t\t\t\tblock.link = newUrl;\n\t\t\t\tchanged = true;\n\t\t\t\turlsRewritten++;\n\t\t\t}\n\t\t}\n\n\t\t// Handle gallery blocks with nested images\n\t\tif (block._type === \"gallery\" && Array.isArray(block.images)) {\n\t\t\tconst result = rewritePortableTextUrls(block.images, exactMap, baseMap);\n\t\t\tif (result.changed) {\n\t\t\t\tchanged = true;\n\t\t\t\turlsRewritten += result.urlsRewritten;\n\t\t\t}\n\t\t}\n\n\t\t// Handle columns blocks with nested content\n\t\tif (block._type === \"columns\" && Array.isArray(block.columns)) {\n\t\t\tfor (const column of block.columns) {\n\t\t\t\tif (Array.isArray(column.content)) {\n\t\t\t\t\tconst result = rewritePortableTextUrls(column.content, exactMap, baseMap);\n\t\t\t\t\tif (result.changed) {\n\t\t\t\t\t\tchanged = true;\n\t\t\t\t\t\turlsRewritten += result.urlsRewritten;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { changed, urlsRewritten };\n}\n\n/**\n * Rewrite URLs in a string field using simple string replacement\n */\nexport function rewriteStringUrls(\n\tvalue: string,\n\texactMap: Record<string, string>,\n\tbaseMap: Map<string, string>,\n): { newValue: string; changed: boolean; urlsRewritten: number } {\n\tlet newValue = value;\n\tlet changed = false;\n\tlet urlsRewritten = 0;\n\n\t// Try exact matches first\n\tfor (const [oldUrl, newUrl] of Object.entries(exactMap)) {\n\t\tif (newValue.includes(oldUrl)) {\n\t\t\tnewValue = newValue.split(oldUrl).join(newUrl);\n\t\t\tchanged = true;\n\t\t\turlsRewritten++;\n\t\t}\n\t}\n\n\t// For base URL matching in strings, we need to be more careful\n\t// Only match if we find a URL that starts with the base\n\tfor (const [baseUrl, newUrl] of baseMap.entries()) {\n\t\t// Look for the base URL followed by optional query string or end\n\t\tconst regex = buildBaseUrlMatchRegex(baseUrl);\n\t\tconst matches = newValue.match(regex);\n\t\tif (matches) {\n\t\t\tfor (const match of matches) {\n\t\t\t\t// Don't replace if we already have an exact match in the map\n\t\t\t\tif (!exactMap[match]) {\n\t\t\t\t\tnewValue = newValue.split(match).join(newUrl);\n\t\t\t\t\tchanged = true;\n\t\t\t\t\turlsRewritten++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { newValue, changed, urlsRewritten };\n}\n\n/**\n * Escape special regex characters in a string\n */\nfunction escapeRegExp(string: string): string {\n\treturn string.replace(REGEX_SPECIAL_CHARS, \"\\\\$&\");\n}\n\nfunction stripWordPressImageSizeSuffix(url: string): string {\n\treturn url.replace(WORDPRESS_IMAGE_SIZE_SUFFIX, \"\");\n}\n\nfunction buildBaseUrlMatchRegex(baseUrl: string): RegExp {\n\tconst extensionMatch = BASE_URL_EXTENSION.exec(baseUrl);\n\tconst basePattern = extensionMatch\n\t\t? `${escapeRegExp(extensionMatch[1])}(?:-\\\\d+x\\\\d+)?${escapeRegExp(extensionMatch[2])}`\n\t\t: escapeRegExp(baseUrl);\n\n\treturn new RegExp(\n\t\t`${basePattern}(\\\\?[^\"'\\\\s]*)?(?=$|[\"'\\\\s<>)\\\\],;:!?]|\\\\.(?=$|[\"'\\\\s<>)\\\\]]))`,\n\t\t\"g\",\n\t);\n}\n"],"mappings":";AAAA,MAAM,sBAAsB;AAC5B,MAAM,8BAA8B;AACpC,MAAM,qBAAqB;;;;AAK3B,SAAgB,WAAW,KAAqB;AAC/C,KAAI;EACH,MAAM,SAAS,IAAI,IAAI,IAAI;AAC3B,SAAO,GAAG,OAAO,SAAS,OAAO;SAC1B;AAEP,SAAO,IAAI,MAAM,IAAI,CAAC,MAAM;;;;;;AAO9B,SAAgB,gBAAgB,QAAqD;CACpF,MAAM,0BAAU,IAAI,KAAqB;AACzC,MAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,OAAO,EAAE;EACtD,MAAM,UAAU,WAAW,OAAO;AAClC,UAAQ,IAAI,SAAS,OAAO;;AAE7B,QAAO;;;;;;;;;;;AAYR,SAAgB,gBAAgB,OAAuB;AACtD,KAAI;EAEH,MAAM,SAAS,KAAK,MAAM,MAAM;AAChC,MAAI,UAAU,OAAO,OAAO,QAAQ,SACnC,QAAO,OAAO;SAER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