@camunda8/docusaurus-plugin-openapi-docs 4.5.2 → 4.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/openapi/sdkExamples.js +24 -4
- package/package.json +1 -1
- package/src/openapi/sdkExamples.ts +215 -195
|
@@ -49,13 +49,24 @@ function extractRegion(content, regionName) {
|
|
|
49
49
|
// Check if this line is an end marker for our region
|
|
50
50
|
for (const pattern of REGION_PATTERNS) {
|
|
51
51
|
const endMatch = line.match(pattern.end);
|
|
52
|
-
if (endMatch &&
|
|
53
|
-
(!endMatch[1] || endMatch[1].trim() === regionName)) {
|
|
52
|
+
if (endMatch && (!endMatch[1] || endMatch[1].trim() === regionName)) {
|
|
54
53
|
// Found end marker — return captured content
|
|
55
54
|
return trimIndentation(capturedLines);
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
|
-
|
|
57
|
+
// Skip nested region markers for the same region name
|
|
58
|
+
// (e.g. C# files may have both #region and // <Name> markers)
|
|
59
|
+
let isNestedMarker = false;
|
|
60
|
+
for (const pattern of REGION_PATTERNS) {
|
|
61
|
+
const startMatch = line.match(pattern.start);
|
|
62
|
+
if (startMatch && startMatch[1].trim() === regionName) {
|
|
63
|
+
isNestedMarker = true;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!isNestedMarker) {
|
|
68
|
+
capturedLines.push(line);
|
|
69
|
+
}
|
|
59
70
|
}
|
|
60
71
|
else {
|
|
61
72
|
// Check if this line is a start marker for our region
|
|
@@ -87,6 +98,13 @@ function trimIndentation(lines) {
|
|
|
87
98
|
.join("\n")
|
|
88
99
|
.trim();
|
|
89
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Convert a camelCase string to snake_case.
|
|
103
|
+
* e.g. "activateJobs" -> "activate_jobs"
|
|
104
|
+
*/
|
|
105
|
+
function camelToSnake(str) {
|
|
106
|
+
return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
107
|
+
}
|
|
90
108
|
/**
|
|
91
109
|
* Build x-codeSamples entries for a given operationId from all configured SDK sources.
|
|
92
110
|
*
|
|
@@ -110,7 +128,9 @@ function buildCodeSamples(operationId, sdkExamples, siteDir, fileCache, mapCache
|
|
|
110
128
|
operationMap = fs_extra_1.default.readJSONSync(mapPath);
|
|
111
129
|
mapCache.set(mapPath, operationMap);
|
|
112
130
|
}
|
|
113
|
-
|
|
131
|
+
// Try exact match first, then fall back to snake_case conversion
|
|
132
|
+
// (Python SDKs conventionally use snake_case operationIds)
|
|
133
|
+
const entries = operationMap[operationId] || operationMap[camelToSnake(operationId)];
|
|
114
134
|
if (!entries || entries.length === 0)
|
|
115
135
|
continue;
|
|
116
136
|
const mapDir = path_1.default.dirname(mapPath);
|
package/package.json
CHANGED
|
@@ -1,195 +1,215 @@
|
|
|
1
|
-
/* ============================================================================
|
|
2
|
-
* Copyright (c) Palo Alto Networks
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
* ========================================================================== */
|
|
7
|
-
|
|
8
|
-
import path from "path";
|
|
9
|
-
|
|
10
|
-
import fs from "fs-extra";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Configuration for a single SDK example source.
|
|
14
|
-
*/
|
|
15
|
-
export interface SdkExampleSource {
|
|
16
|
-
/** Language label shown in the code sample tab (e.g. "TypeScript", "Python", "C#"). */
|
|
17
|
-
lang: string;
|
|
18
|
-
/** Syntax highlight mode (e.g. "typescript", "python", "csharp"). */
|
|
19
|
-
highlight?: string;
|
|
20
|
-
/** Path to the operation-map.json file (relative to site root or absolute). */
|
|
21
|
-
operationMapPath: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A single entry in operation-map.json for one operationId.
|
|
26
|
-
*/
|
|
27
|
-
interface OperationMapEntry {
|
|
28
|
-
file: string;
|
|
29
|
-
region: string;
|
|
30
|
-
label?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Shape of operation-map.json: operationId -> entries[].
|
|
35
|
-
*/
|
|
36
|
-
type OperationMap = Record<string, OperationMapEntry[]>;
|
|
37
|
-
|
|
38
|
-
// Region marker patterns for supported languages.
|
|
39
|
-
// Each pattern matches the start/end of a named region.
|
|
40
|
-
const REGION_PATTERNS: {
|
|
41
|
-
start: RegExp;
|
|
42
|
-
end: RegExp;
|
|
43
|
-
}[] = [
|
|
44
|
-
// TypeScript/JavaScript: //#region Name ... //#endregion [Name]
|
|
45
|
-
{
|
|
46
|
-
start: /^\s*\/\/#region\s+(.+?)\s*$/,
|
|
47
|
-
end: /^\s*\/\/#endregion(?:\s+(.+?))?\s*$/,
|
|
48
|
-
},
|
|
49
|
-
// C#: #region Name ... #endregion [Name]
|
|
50
|
-
{
|
|
51
|
-
start: /^\s*#region\s+(.+?)\s*$/,
|
|
52
|
-
end: /^\s*#endregion(?:\s+(.+?))?\s*$/,
|
|
53
|
-
},
|
|
54
|
-
// XML-style: // <RegionName> ... // </RegionName>
|
|
55
|
-
{
|
|
56
|
-
start: /^\s*\/\/\s*<([A-Za-z]\w*)>\s*$/,
|
|
57
|
-
end: /^\s*\/\/\s*<\/([A-Za-z]\w*)>\s*$/,
|
|
58
|
-
},
|
|
59
|
-
// Python: # region Name ... # endregion [Name]
|
|
60
|
-
{
|
|
61
|
-
start: /^\s*#\s*region\s+(.+?)\s*$/,
|
|
62
|
-
end: /^\s*#\s*endregion(?:\s+(.+?))?\s*$/,
|
|
63
|
-
},
|
|
64
|
-
];
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Extract the code between region markers from a source file's content.
|
|
68
|
-
* Returns the trimmed code block or undefined if the region is not found.
|
|
69
|
-
*/
|
|
70
|
-
function extractRegion(
|
|
71
|
-
content: string,
|
|
72
|
-
regionName: string
|
|
73
|
-
): string | undefined {
|
|
74
|
-
const lines = content.split(/\r?\n/);
|
|
75
|
-
let capturing = false;
|
|
76
|
-
let capturedLines: string[] = [];
|
|
77
|
-
|
|
78
|
-
for (const line of lines) {
|
|
79
|
-
if (capturing) {
|
|
80
|
-
// Check if this line is an end marker for our region
|
|
81
|
-
for (const pattern of REGION_PATTERNS) {
|
|
82
|
-
const endMatch = line.match(pattern.end);
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
* Copyright (c) Palo Alto Networks
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* ========================================================================== */
|
|
7
|
+
|
|
8
|
+
import path from "path";
|
|
9
|
+
|
|
10
|
+
import fs from "fs-extra";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for a single SDK example source.
|
|
14
|
+
*/
|
|
15
|
+
export interface SdkExampleSource {
|
|
16
|
+
/** Language label shown in the code sample tab (e.g. "TypeScript", "Python", "C#"). */
|
|
17
|
+
lang: string;
|
|
18
|
+
/** Syntax highlight mode (e.g. "typescript", "python", "csharp"). */
|
|
19
|
+
highlight?: string;
|
|
20
|
+
/** Path to the operation-map.json file (relative to site root or absolute). */
|
|
21
|
+
operationMapPath: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A single entry in operation-map.json for one operationId.
|
|
26
|
+
*/
|
|
27
|
+
interface OperationMapEntry {
|
|
28
|
+
file: string;
|
|
29
|
+
region: string;
|
|
30
|
+
label?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Shape of operation-map.json: operationId -> entries[].
|
|
35
|
+
*/
|
|
36
|
+
type OperationMap = Record<string, OperationMapEntry[]>;
|
|
37
|
+
|
|
38
|
+
// Region marker patterns for supported languages.
|
|
39
|
+
// Each pattern matches the start/end of a named region.
|
|
40
|
+
const REGION_PATTERNS: {
|
|
41
|
+
start: RegExp;
|
|
42
|
+
end: RegExp;
|
|
43
|
+
}[] = [
|
|
44
|
+
// TypeScript/JavaScript: //#region Name ... //#endregion [Name]
|
|
45
|
+
{
|
|
46
|
+
start: /^\s*\/\/#region\s+(.+?)\s*$/,
|
|
47
|
+
end: /^\s*\/\/#endregion(?:\s+(.+?))?\s*$/,
|
|
48
|
+
},
|
|
49
|
+
// C#: #region Name ... #endregion [Name]
|
|
50
|
+
{
|
|
51
|
+
start: /^\s*#region\s+(.+?)\s*$/,
|
|
52
|
+
end: /^\s*#endregion(?:\s+(.+?))?\s*$/,
|
|
53
|
+
},
|
|
54
|
+
// XML-style: // <RegionName> ... // </RegionName>
|
|
55
|
+
{
|
|
56
|
+
start: /^\s*\/\/\s*<([A-Za-z]\w*)>\s*$/,
|
|
57
|
+
end: /^\s*\/\/\s*<\/([A-Za-z]\w*)>\s*$/,
|
|
58
|
+
},
|
|
59
|
+
// Python: # region Name ... # endregion [Name]
|
|
60
|
+
{
|
|
61
|
+
start: /^\s*#\s*region\s+(.+?)\s*$/,
|
|
62
|
+
end: /^\s*#\s*endregion(?:\s+(.+?))?\s*$/,
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extract the code between region markers from a source file's content.
|
|
68
|
+
* Returns the trimmed code block or undefined if the region is not found.
|
|
69
|
+
*/
|
|
70
|
+
function extractRegion(
|
|
71
|
+
content: string,
|
|
72
|
+
regionName: string
|
|
73
|
+
): string | undefined {
|
|
74
|
+
const lines = content.split(/\r?\n/);
|
|
75
|
+
let capturing = false;
|
|
76
|
+
let capturedLines: string[] = [];
|
|
77
|
+
|
|
78
|
+
for (const line of lines) {
|
|
79
|
+
if (capturing) {
|
|
80
|
+
// Check if this line is an end marker for our region
|
|
81
|
+
for (const pattern of REGION_PATTERNS) {
|
|
82
|
+
const endMatch = line.match(pattern.end);
|
|
83
|
+
if (endMatch && (!endMatch[1] || endMatch[1].trim() === regionName)) {
|
|
84
|
+
// Found end marker — return captured content
|
|
85
|
+
return trimIndentation(capturedLines);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Skip nested region markers for the same region name
|
|
89
|
+
// (e.g. C# files may have both #region and // <Name> markers)
|
|
90
|
+
let isNestedMarker = false;
|
|
91
|
+
for (const pattern of REGION_PATTERNS) {
|
|
92
|
+
const startMatch = line.match(pattern.start);
|
|
93
|
+
if (startMatch && startMatch[1].trim() === regionName) {
|
|
94
|
+
isNestedMarker = true;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (!isNestedMarker) {
|
|
99
|
+
capturedLines.push(line);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
// Check if this line is a start marker for our region
|
|
103
|
+
for (const pattern of REGION_PATTERNS) {
|
|
104
|
+
const startMatch = line.match(pattern.start);
|
|
105
|
+
if (startMatch && startMatch[1].trim() === regionName) {
|
|
106
|
+
capturing = true;
|
|
107
|
+
capturedLines = [];
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Remove the common leading whitespace from all non-empty lines.
|
|
119
|
+
*/
|
|
120
|
+
function trimIndentation(lines: string[]): string {
|
|
121
|
+
const nonEmptyLines = lines.filter((l) => l.trim().length > 0);
|
|
122
|
+
if (nonEmptyLines.length === 0) return "";
|
|
123
|
+
|
|
124
|
+
const minIndent = Math.min(
|
|
125
|
+
...nonEmptyLines.map((l) => {
|
|
126
|
+
const match = l.match(/^(\s*)/);
|
|
127
|
+
return match ? match[1].length : 0;
|
|
128
|
+
})
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return lines
|
|
132
|
+
.map((l) => (l.trim().length === 0 ? "" : l.slice(minIndent)))
|
|
133
|
+
.join("\n")
|
|
134
|
+
.trim();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Convert a camelCase string to snake_case.
|
|
139
|
+
* e.g. "activateJobs" -> "activate_jobs"
|
|
140
|
+
*/
|
|
141
|
+
function camelToSnake(str: string): string {
|
|
142
|
+
return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Build x-codeSamples entries for a given operationId from all configured SDK sources.
|
|
147
|
+
*
|
|
148
|
+
* @param operationId - The OpenAPI operationId to look up.
|
|
149
|
+
* @param sdkExamples - Array of SDK example source configurations.
|
|
150
|
+
* @param siteDir - The Docusaurus site root directory (for resolving relative paths).
|
|
151
|
+
* @param fileCache - Shared cache to avoid re-reading the same source files.
|
|
152
|
+
* @param mapCache - Shared cache for parsed operation-map.json files.
|
|
153
|
+
* @returns Array of x-codeSamples entries, or empty array if none found.
|
|
154
|
+
*/
|
|
155
|
+
export function buildCodeSamples(
|
|
156
|
+
operationId: string,
|
|
157
|
+
sdkExamples: SdkExampleSource[],
|
|
158
|
+
siteDir: string,
|
|
159
|
+
fileCache: Map<string, string>,
|
|
160
|
+
mapCache: Map<string, OperationMap>
|
|
161
|
+
): { lang: string; label: string; source: string }[] {
|
|
162
|
+
const samples: { lang: string; label: string; source: string }[] = [];
|
|
163
|
+
|
|
164
|
+
for (const sdk of sdkExamples) {
|
|
165
|
+
const mapPath = path.resolve(siteDir, sdk.operationMapPath);
|
|
166
|
+
let operationMap = mapCache.get(mapPath);
|
|
167
|
+
if (!operationMap) {
|
|
168
|
+
if (!fs.existsSync(mapPath)) {
|
|
169
|
+
console.warn(`SDK examples: operation-map not found at ${mapPath}`);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
operationMap = fs.readJSONSync(mapPath) as OperationMap;
|
|
173
|
+
mapCache.set(mapPath, operationMap);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Try exact match first, then fall back to snake_case conversion
|
|
177
|
+
// (Python SDKs conventionally use snake_case operationIds)
|
|
178
|
+
const entries =
|
|
179
|
+
operationMap[operationId] || operationMap[camelToSnake(operationId)];
|
|
180
|
+
if (!entries || entries.length === 0) continue;
|
|
181
|
+
|
|
182
|
+
const mapDir = path.dirname(mapPath);
|
|
183
|
+
|
|
184
|
+
for (const entry of entries) {
|
|
185
|
+
const filePath = path.resolve(mapDir, entry.file);
|
|
186
|
+
let fileContent = fileCache.get(filePath);
|
|
187
|
+
if (fileContent === undefined) {
|
|
188
|
+
if (!fs.existsSync(filePath)) {
|
|
189
|
+
console.warn(
|
|
190
|
+
`SDK examples: source file not found at ${filePath} (referenced by ${sdk.lang} operation-map for ${operationId})`
|
|
191
|
+
);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
fileContent = fs.readFileSync(filePath, "utf-8");
|
|
195
|
+
fileCache.set(filePath, fileContent);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const code = extractRegion(fileContent, entry.region);
|
|
199
|
+
if (!code) {
|
|
200
|
+
console.warn(
|
|
201
|
+
`SDK examples: region "${entry.region}" not found in ${filePath} (${sdk.lang}, ${operationId})`
|
|
202
|
+
);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
samples.push({
|
|
207
|
+
lang: sdk.lang,
|
|
208
|
+
label: "SDK",
|
|
209
|
+
source: code,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return samples;
|
|
215
|
+
}
|