1 import { apiRefRedirects } from "./api-ref-paths"
2 import { OpenAPI } from "types"
3
4 const ABSOLUTE_API_HASH_RE =
5 /^https?:\/\/docs\.medusajs\.com(?:\/v2)?\/api\/(store|admin)#(.+)$/
6 const RELATIVE_HASH_RE = /^#(.+)$/
7
8 /**
9 * Resolve a doc link that may point to the API reference using the legacy hash
10 * format to its new page path. Handles:
11 * - absolute legacy links (`https://docs.medusajs.com/api/store#carts_getcartsid`)
12 * - relative anchors (`#select-fields-and-relations`) using the current area
13 *
14 * Returns a path without the `basePath` — consumers render it through the
15 * `next/link`-based `Link`, which prepends `/api` (adding it here would double it
16 * to `/api/api/...`). Hashes that aren't known page anchors (e.g. in-section
17 * sub-headings) and non-API links are returned unchanged.
18 */
19 export function resolveApiRefDocUrl(url: string, area: OpenAPI.Area): string {
20 const absoluteMatch = url.match(ABSOLUTE_API_HASH_RE)
21 if (absoluteMatch) {
22 const [, linkArea, hash] = absoluteMatch
23 const target = apiRefRedirects[linkArea as OpenAPI.Area]?.[hash]
24 return target ?? url
25 }
26
27 const relativeMatch = url.match(RELATIVE_HASH_RE)
28 if (relativeMatch) {
29 const hash = relativeMatch[1]
30 const target = apiRefRedirects[area]?.[hash]
31 // leave unknown anchors as in-page hashes
32 return target ?? url
33 }
34
35 return url
36 }