|
1
|
import pkg from "slugify" |
|
2
|
import { getSectionId } from "./sidebar-utils.js" |
|
3
|
|
|
4
|
const { default: slugify } = pkg |
|
5
|
|
|
6
|
/** |
|
7
|
* Minimal shape of an OAS operation needed to derive its URL slug. |
|
8
|
*/ |
|
9
|
export type ApiRefOperationLike = { |
|
10
|
operationId?: string |
|
11
|
summary?: string |
|
12
|
"x-sidebar-summary"?: string |
|
13
|
} |
|
14
|
|
|
15
|
/** |
|
16
|
* Slug reserved for a tag's associated schema page (e.g. `/store/carts/schema`). |
|
17
|
*/ |
|
18
|
export const API_REF_SCHEMA_SLUG = "schema" |
|
19
|
|
|
20
|
/** |
|
21
|
* Slugify a single URL segment: lowercased and hyphen-separated. |
|
22
|
* (e.g. `"Get a Cart"` -> `"get-a-cart"`) |
|
23
|
*/ |
|
24
|
export function getApiRefSegmentSlug(value: string): string { |
|
25
|
// `strict` drops characters that aren't URL-safe (apostrophes, parentheses, |
|
26
|
// etc.) so summary-derived slugs stay clean, e.g. |
|
27
|
// `"Change Cart's Customer"` -> `"change-carts-customer"`. |
|
28
|
return slugify(value.trim().toLowerCase(), { strict: true }) |
|
29
|
} |
|
30
|
|
|
31
|
/** |
|
32
|
* The slug for an intro/markdown section heading. Kept identical to the |
|
33
|
* previous hash anchor (`getSectionId([heading])`) so old `#authentication` |
|
34
|
* links map cleanly to `/authentication`. |
|
35
|
*/ |
|
36
|
export function getApiRefIntroSlug(heading: string): string { |
|
37
|
return getSectionId([heading]) |
|
38
|
} |
|
39
|
|
|
40
|
/** |
|
41
|
* The slug for a tag. Kept identical to the previous tag hash |
|
42
|
* (`getSectionId([tag.name])`), e.g. `"Gift Cards"` -> `"gift-cards"`. |
|
43
|
*/ |
|
44
|
export function getApiRefTagSlug(tagName: string): string { |
|
45
|
return getSectionId([tagName]) |
|
46
|
} |
|
47
|
|
|
48
|
/** |
|
49
|
* The base (pre-deduplication) slug for a single operation, derived from its |
|
50
|
* `x-sidebar-summary`, then `summary`, then `operationId`. |
|
51
|
*/ |
|
52
|
export function getApiRefOperationSlug(operation: ApiRefOperationLike): string { |
|
53
|
const base = |
|
54
|
operation["x-sidebar-summary"] || |
|
55
|
operation.summary || |
|
56
|
operation.operationId || |
|
57
|
"" |
|
58
|
|
|
59
|
return getApiRefSegmentSlug(base) |
|
60
|
} |
|
61
|
|
|
62
|
/** |
|
63
|
* Compute unique operation slugs for all operations of a single tag. |
|
64
|
* |
|
65
|
* Slugs only need to be unique within a tag. The `operations` array should be |
|
66
|
* passed in the desired display order so that duplicate/reserved slugs receive |
|
67
|
* a deterministic numeric suffix (`-2`, `-3`, ...). |
|
68
|
* |
|
69
|
* @returns a map of `operationId` to its final, unique slug. |
|
70
|
*/ |
|
71
|
export function getApiRefTagOperationSlugs( |
|
72
|
operations: (ApiRefOperationLike & { operationId: string })[] |
|
73
|
): Map<string, string> { |
|
74
|
const result = new Map<string, string>() |
|
75
|
const usedSlugs = new Set<string>([API_REF_SCHEMA_SLUG]) |
|
76
|
|
|
77
|
for (const operation of operations) { |
|
78
|
const base = |
|
79
|
getApiRefOperationSlug(operation) || |
|
80
|
getApiRefSegmentSlug(operation.operationId) |
|
81
|
|
|
82
|
let slug = base |
|
83
|
let counter = 2 |
|
84
|
while (usedSlugs.has(slug)) { |
|
85
|
slug = `${base}-${counter}` |
|
86
|
counter++ |
|
87
|
} |
|
88
|
|
|
89
|
usedSlugs.add(slug) |
|
90
|
result.set(operation.operationId, slug) |
|
91
|
} |
|
92
|
|
|
93
|
return result |
|
94
|
} |
|
95
|
|
|
96
|
/** |
|
97
|
* Build an API reference page path (without the app `basePath`). |
|
98
|
* |
|
99
|
* - Tag/intro page: `getApiRefPath({ area, section })` -> `/store/carts` |
|
100
|
* - Operation page: `getApiRefPath({ area, section, operationSlug })` -> |
|
101
|
* `/store/carts/get-a-cart` |
|
102
|
*/ |
|
103
|
export function getApiRefPath({ |
|
104
|
area, |
|
105
|
section, |
|
106
|
operationSlug, |
|
107
|
}: { |
|
108
|
area: string |
|
109
|
section: string |
|
110
|
operationSlug?: string |
|
111
|
}): string { |
|
112
|
const segments = [area, section] |
|
113
|
if (operationSlug) { |
|
114
|
segments.push(operationSlug) |
|
115
|
} |
|
116
|
|
|
117
|
return `/${segments.join("/")}` |
|
118
|
} |