docusaurus-theme-openapi-docs 2.0.3 → 2.0.4

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.
@@ -0,0 +1,21 @@
1
+ export declare type CodeSampleLanguage = "C" | "C#" | "C++" | "CoffeeScript" | "CSS" | "Dart" | "DM" | "Elixir" | "Go" | "Groovy" | "HTML" | "Java" | "JavaScript" | "Kotlin" | "Objective-C" | "Perl" | "PHP" | "PowerShell" | "Python" | "Ruby" | "Rust" | "Scala" | "Shell" | "Swift" | "TypeScript";
2
+ export interface Language {
3
+ highlight: string;
4
+ language: string;
5
+ codeSampleLanguage: CodeSampleLanguage;
6
+ logoClass: string;
7
+ variant: string;
8
+ variants: string[];
9
+ options?: {
10
+ [key: string]: boolean;
11
+ };
12
+ sample?: string;
13
+ samples?: string[];
14
+ samplesSources?: string[];
15
+ samplesLabels?: string[];
16
+ }
17
+ export interface CodeSample {
18
+ source: string;
19
+ lang: CodeSampleLanguage;
20
+ label?: string;
21
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,20 +1,10 @@
1
1
  /// <reference types="react" />
2
2
  import sdk from "@paloaltonetworks/postman-collection";
3
- export interface Language {
4
- highlight: string;
5
- language: string;
6
- logoClass: string;
7
- variant: string;
8
- variants: string[];
9
- options: {
10
- [key: string]: boolean;
11
- };
12
- source?: string;
13
- }
3
+ import { CodeSample, Language } from "./code-snippets-types";
14
4
  export declare const languageSet: Language[];
15
5
  export interface Props {
16
6
  postman: sdk.Request;
17
- codeSamples: any;
7
+ codeSamples: CodeSample[];
18
8
  }
19
9
  declare function CodeSnippets({ postman, codeSamples }: Props): JSX.Element | null;
20
10
  export default CodeSnippets;
@@ -72,10 +72,12 @@ const buildPostmanRequest_1 = __importDefault(
72
72
  const CodeTabs_1 = __importDefault(require("@theme/ApiExplorer/CodeTabs"));
73
73
  const hooks_1 = require("@theme/ApiItem/hooks");
74
74
  const merge_1 = __importDefault(require("lodash/merge"));
75
+ const languages_1 = require("./languages");
75
76
  exports.languageSet = [
76
77
  {
77
78
  highlight: "bash",
78
79
  language: "curl",
80
+ codeSampleLanguage: "Shell",
79
81
  logoClass: "bash",
80
82
  options: {
81
83
  longFormat: false,
@@ -88,6 +90,7 @@ exports.languageSet = [
88
90
  {
89
91
  highlight: "python",
90
92
  language: "python",
93
+ codeSampleLanguage: "Python",
91
94
  logoClass: "python",
92
95
  options: {
93
96
  followRedirect: true,
@@ -99,6 +102,7 @@ exports.languageSet = [
99
102
  {
100
103
  highlight: "go",
101
104
  language: "go",
105
+ codeSampleLanguage: "Go",
102
106
  logoClass: "go",
103
107
  options: {
104
108
  followRedirect: true,
@@ -110,6 +114,7 @@ exports.languageSet = [
110
114
  {
111
115
  highlight: "javascript",
112
116
  language: "nodejs",
117
+ codeSampleLanguage: "JavaScript",
113
118
  logoClass: "nodejs",
114
119
  options: {
115
120
  ES6_enabled: true,
@@ -117,11 +122,12 @@ exports.languageSet = [
117
122
  trimRequestBody: true,
118
123
  },
119
124
  variant: "axios",
120
- variants: ["axios", "native", "request", "unirest"],
125
+ variants: ["axios", "native"],
121
126
  },
122
127
  {
123
128
  highlight: "ruby",
124
129
  language: "ruby",
130
+ codeSampleLanguage: "Ruby",
125
131
  logoClass: "ruby",
126
132
  options: {
127
133
  followRedirect: true,
@@ -133,6 +139,7 @@ exports.languageSet = [
133
139
  {
134
140
  highlight: "csharp",
135
141
  language: "csharp",
142
+ codeSampleLanguage: "C#",
136
143
  logoClass: "csharp",
137
144
  options: {
138
145
  followRedirect: true,
@@ -144,6 +151,7 @@ exports.languageSet = [
144
151
  {
145
152
  highlight: "php",
146
153
  language: "php",
154
+ codeSampleLanguage: "PHP",
147
155
  logoClass: "php",
148
156
  options: {
149
157
  followRedirect: true,
@@ -155,6 +163,7 @@ exports.languageSet = [
155
163
  {
156
164
  highlight: "java",
157
165
  language: "java",
166
+ codeSampleLanguage: "Java",
158
167
  logoClass: "java",
159
168
  options: {
160
169
  followRedirect: true,
@@ -166,6 +175,7 @@ exports.languageSet = [
166
175
  {
167
176
  highlight: "powershell",
168
177
  language: "powershell",
178
+ codeSampleLanguage: "PowerShell",
169
179
  logoClass: "powershell",
170
180
  options: {
171
181
  followRedirect: true,
@@ -175,7 +185,7 @@ exports.languageSet = [
175
185
  variants: ["restmethod"],
176
186
  },
177
187
  ];
178
- function CodeTab({ children, hidden, className, onClick }) {
188
+ function CodeTab({ children, hidden, className }) {
179
189
  return react_1.default.createElement(
180
190
  "div",
181
191
  { role: "tabpanel", className: className, ...{ hidden } },
@@ -208,7 +218,6 @@ function CodeSnippets({ postman, codeSamples }) {
208
218
  // Can override languageSet, change order of langs, override options and variants
209
219
  const langs = [
210
220
  ...(siteConfig?.themeConfig?.languageTabs ?? exports.languageSet),
211
- ...codeSamples,
212
221
  ];
213
222
  // Filter languageSet by user-defined langs
214
223
  const filteredLanguageSet = exports.languageSet.filter((ls) => {
@@ -217,13 +226,17 @@ function CodeSnippets({ postman, codeSamples }) {
217
226
  });
218
227
  });
219
228
  // Merge user-defined langs into languageSet
220
- const mergedLangs = (0, merge_1.default)(filteredLanguageSet, langs);
229
+ const mergedLangs = (0, languages_1.mergeCodeSampleLanguage)(
230
+ (0, merge_1.default)(filteredLanguageSet, langs),
231
+ codeSamples
232
+ );
221
233
  // Read defaultLang from localStorage
222
234
  const defaultLang = mergedLangs.filter(
223
235
  (lang) =>
224
236
  lang.language === localStorage.getItem("docusaurus.tab.code-samples")
225
237
  );
226
238
  const [selectedVariant, setSelectedVariant] = (0, react_1.useState)();
239
+ const [selectedSample, setSelectedSample] = (0, react_1.useState)();
227
240
  const [language, setLanguage] = (0, react_1.useState)(() => {
228
241
  // Return first index if only 1 user-defined language exists
229
242
  if (mergedLangs.length === 1) {
@@ -233,7 +246,20 @@ function CodeSnippets({ postman, codeSamples }) {
233
246
  return defaultLang[0] ?? mergedLangs[0];
234
247
  });
235
248
  const [codeText, setCodeText] = (0, react_1.useState)("");
249
+ const [codeSampleCodeText, setCodeSampleCodeText] = (0, react_1.useState)("");
236
250
  (0, react_1.useEffect)(() => {
251
+ // initial active language is custom code sample
252
+ if (
253
+ language &&
254
+ language.sample &&
255
+ language.samples &&
256
+ language.samplesSources
257
+ ) {
258
+ const sampleIndex = language.samples.findIndex(
259
+ (smp) => smp === language.sample
260
+ );
261
+ setCodeSampleCodeText(language.samplesSources[sampleIndex]);
262
+ }
237
263
  if (language && !!language.options) {
238
264
  const postmanRequest = (0, buildPostmanRequest_1.default)(postman, {
239
265
  queryParams,
@@ -258,8 +284,6 @@ function CodeSnippets({ postman, codeSamples }) {
258
284
  setCodeText(snippet);
259
285
  }
260
286
  );
261
- } else if (language && !!language.source) {
262
- setCodeText(language.source);
263
287
  } else if (language && !language.options) {
264
288
  const langSource = mergedLangs.filter(
265
289
  (lang) => lang.language === language.language
@@ -308,7 +332,8 @@ function CodeSnippets({ postman, codeSamples }) {
308
332
  auth,
309
333
  mergedLangs,
310
334
  ]);
311
- (0, react_1.useEffect)(() => {
335
+ // no dependencies was intentionlly set for this particular hook. it's safe as long as if conditions are set
336
+ (0, react_1.useEffect)(function onSelectedVariantUpdate() {
312
337
  if (selectedVariant && selectedVariant !== language.variant) {
313
338
  const postmanRequest = (0, buildPostmanRequest_1.default)(postman, {
314
339
  queryParams,
@@ -335,6 +360,21 @@ function CodeSnippets({ postman, codeSamples }) {
335
360
  );
336
361
  }
337
362
  });
363
+ // no dependencies was intentionlly set for this particular hook. it's safe as long as if conditions are set
364
+ // eslint-disable-next-line react-hooks/exhaustive-deps
365
+ (0, react_1.useEffect)(function onSelectedSampleUpdate() {
366
+ if (
367
+ language.samples &&
368
+ language.samplesSources &&
369
+ selectedSample &&
370
+ selectedSample !== language.sample
371
+ ) {
372
+ const sampleIndex = language.samples.findIndex(
373
+ (smp) => smp === selectedSample
374
+ );
375
+ setCodeSampleCodeText(language.samplesSources[sampleIndex]);
376
+ }
377
+ });
338
378
  if (language === undefined) {
339
379
  return null;
340
380
  }
@@ -362,6 +402,45 @@ function CodeSnippets({ postman, codeSamples }) {
362
402
  className: `openapi-tabs__code-item--${lang.logoClass}`,
363
403
  },
364
404
  },
405
+ lang.samples &&
406
+ react_1.default.createElement(
407
+ CodeTabs_1.default,
408
+ {
409
+ className: "openapi-tabs__code-container-inner",
410
+ action: {
411
+ setLanguage: setLanguage,
412
+ setSelectedSample: setSelectedSample,
413
+ },
414
+ includeSample: true,
415
+ currentLanguage: lang.language,
416
+ defaultValue: selectedSample,
417
+ lazy: true,
418
+ },
419
+ lang.samples.map((sample, index) => {
420
+ return react_1.default.createElement(
421
+ CodeTab,
422
+ {
423
+ value: sample,
424
+ label: lang.samplesLabels
425
+ ? lang.samplesLabels[index]
426
+ : sample,
427
+ key: `${lang.language}-${lang.sample}`,
428
+ attributes: {
429
+ className: `openapi-tabs__code-item--sample`,
430
+ },
431
+ },
432
+ react_1.default.createElement(
433
+ ApiCodeBlock_1.default,
434
+ {
435
+ language: lang.highlight,
436
+ className: "openapi-explorer__code-block",
437
+ showLineNumbers: true,
438
+ },
439
+ codeSampleCodeText
440
+ )
441
+ );
442
+ })
443
+ ),
365
444
  react_1.default.createElement(
366
445
  CodeTabs_1.default,
367
446
  {
@@ -375,7 +454,7 @@ function CodeSnippets({ postman, codeSamples }) {
375
454
  defaultValue: selectedVariant,
376
455
  lazy: true,
377
456
  },
378
- lang.variants.map((variant) => {
457
+ lang.variants.map((variant, index) => {
379
458
  return react_1.default.createElement(
380
459
  CodeTab,
381
460
  {
@@ -0,0 +1,2 @@
1
+ import { CodeSample, Language } from "./code-snippets-types";
2
+ export declare function mergeCodeSampleLanguage(languages: Language[], codeSamples: CodeSample[]): Language[];
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.mergeCodeSampleLanguage = void 0;
10
+ function mergeCodeSampleLanguage(languages, codeSamples) {
11
+ return languages.map((language) => {
12
+ const languageCodeSamples = codeSamples.filter(
13
+ ({ lang }) => lang === language.codeSampleLanguage
14
+ );
15
+ if (languageCodeSamples.length) {
16
+ const samples = languageCodeSamples.map(({ lang }) => lang);
17
+ const samplesLabels = languageCodeSamples.map(
18
+ ({ label, lang }) => label || lang
19
+ );
20
+ const samplesSources = languageCodeSamples.map(({ source }) => source);
21
+ return {
22
+ ...language,
23
+ sample: samples[0],
24
+ samples,
25
+ samplesSources,
26
+ samplesLabels,
27
+ };
28
+ }
29
+ return language;
30
+ });
31
+ }
32
+ exports.mergeCodeSampleLanguage = mergeCodeSampleLanguage;
@@ -94,6 +94,20 @@ body[class="ReactModal__Body--open"] {
94
94
  padding-right: 0.5rem !important;
95
95
  }
96
96
 
97
+ .openapi-tabs__code-item--sample {
98
+ color: var(--ifm-color-secondary);
99
+
100
+ &.active {
101
+ border-color: var(--ifm-toc-border-color);
102
+ }
103
+ }
104
+
105
+ .openapi-tabs__code-item--sample > span {
106
+ padding-top: unset !important;
107
+ padding-left: 0.5rem !important;
108
+ padding-right: 0.5rem !important;
109
+ }
110
+
97
111
  .openapi-tabs__code-item--python {
98
112
  color: var(--ifm-color-success);
99
113
 
@@ -19,6 +19,7 @@ function TabList({
19
19
  action,
20
20
  currentLanguage,
21
21
  includeVariant,
22
+ includeSample,
22
23
  className,
23
24
  block,
24
25
  selectedValue,
@@ -53,6 +54,12 @@ function TabList({
53
54
  )[0];
54
55
  action.setSelectedVariant(newLanguage.variant.toLowerCase());
55
56
  }
57
+
58
+ if (currentLanguage && includeSample) {
59
+ newLanguage.sample = newTabValue;
60
+ action.setSelectedSample(newTabValue.toLowerCase());
61
+ }
62
+
56
63
  action.setLanguage(newLanguage);
57
64
  }
58
65
  };
@@ -32,7 +32,7 @@ function ApiExplorer({ item, infoPath }) {
32
32
  item.method !== "event" &&
33
33
  react_1.default.createElement(CodeSnippets_1.default, {
34
34
  postman: postman,
35
- codeSamples: item["x-code-samples"] ?? [],
35
+ codeSamples: item["x-codeSamples"] ?? [],
36
36
  }),
37
37
  react_1.default.createElement(Request_1.default, { item: item }),
38
38
  react_1.default.createElement(Response_1.default, { item: item })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-theme-openapi-docs",
3
3
  "description": "OpenAPI theme for Docusaurus.",
4
- "version": "2.0.3",
4
+ "version": "2.0.4",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -43,7 +43,7 @@
43
43
  "clsx": "^1.1.1",
44
44
  "copy-text-to-clipboard": "^3.1.0",
45
45
  "crypto-js": "^4.1.1",
46
- "docusaurus-plugin-openapi-docs": "^2.0.3",
46
+ "docusaurus-plugin-openapi-docs": "^2.0.4",
47
47
  "docusaurus-plugin-sass": "^0.2.3",
48
48
  "file-saver": "^2.0.5",
49
49
  "lodash": "^4.17.20",
@@ -68,5 +68,5 @@
68
68
  "engines": {
69
69
  "node": ">=14"
70
70
  },
71
- "gitHead": "f6641c0ce01e3247a06ef8ddde7bafbe749d4ed1"
71
+ "gitHead": "71012fa9a8e21d7d83beaf6368b93381298a3e2e"
72
72
  }
@@ -0,0 +1,55 @@
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
+ // https://github.com/github-linguist/linguist/blob/master/lib/linguist/popular.yml
9
+ export type CodeSampleLanguage =
10
+ | "C"
11
+ | "C#"
12
+ | "C++"
13
+ | "CoffeeScript"
14
+ | "CSS"
15
+ | "Dart"
16
+ | "DM"
17
+ | "Elixir"
18
+ | "Go"
19
+ | "Groovy"
20
+ | "HTML"
21
+ | "Java"
22
+ | "JavaScript"
23
+ | "Kotlin"
24
+ | "Objective-C"
25
+ | "Perl"
26
+ | "PHP"
27
+ | "PowerShell"
28
+ | "Python"
29
+ | "Ruby"
30
+ | "Rust"
31
+ | "Scala"
32
+ | "Shell"
33
+ | "Swift"
34
+ | "TypeScript";
35
+
36
+ export interface Language {
37
+ highlight: string;
38
+ language: string;
39
+ codeSampleLanguage: CodeSampleLanguage;
40
+ logoClass: string;
41
+ variant: string;
42
+ variants: string[];
43
+ options?: { [key: string]: boolean };
44
+ sample?: string;
45
+ samples?: string[];
46
+ samplesSources?: string[];
47
+ samplesLabels?: string[];
48
+ }
49
+
50
+ // https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples
51
+ export interface CodeSample {
52
+ source: string;
53
+ lang: CodeSampleLanguage;
54
+ label?: string;
55
+ }