Skip to content

Commit 38b4fb1

Browse files
authored
Merge pull request #945 from PaloAltoNetworks/language-tabs
Expand language tab support
2 parents a89b417 + 433b233 commit 38b4fb1

File tree

7 files changed

+267
-123
lines changed

7 files changed

+267
-123
lines changed

demo/docusaurus.config.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ const config: Config = {
145145
"powershell",
146146
"json",
147147
"bash",
148+
"dart",
149+
"objectivec",
150+
"r",
148151
],
149152
},
150153
languageTabs: [
@@ -194,6 +197,51 @@ const config: Config = {
194197
language: "powershell",
195198
logoClass: "powershell",
196199
},
200+
{
201+
highlight: "dart",
202+
language: "dart",
203+
logoClass: "dart",
204+
},
205+
{
206+
highlight: "javascript",
207+
language: "javascript",
208+
logoClass: "javascript",
209+
},
210+
{
211+
highlight: "c",
212+
language: "c",
213+
logoClass: "c",
214+
},
215+
{
216+
highlight: "objective-c",
217+
language: "objective-c",
218+
logoClass: "objective-c",
219+
},
220+
{
221+
highlight: "ocaml",
222+
language: "ocaml",
223+
logoClass: "ocaml",
224+
},
225+
{
226+
highlight: "r",
227+
language: "r",
228+
logoClass: "r",
229+
},
230+
{
231+
highlight: "swift",
232+
language: "swift",
233+
logoClass: "swift",
234+
},
235+
{
236+
highlight: "kotlin",
237+
language: "kotlin",
238+
logoClass: "kotlin",
239+
},
240+
{
241+
highlight: "rust",
242+
language: "rust",
243+
logoClass: "rust",
244+
},
197245
],
198246
algolia: {
199247
apiKey: "441074cace987cbf4640c039ebed303c",

demo/src/css/custom.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,19 @@ div[class^="announcementBar_"] {
146146
);
147147
font-weight: bold;
148148
}
149+
150+
.openapi-tabs__code-item--python {
151+
color: var(--ifm-color-success);
152+
}
153+
.openapi-tabs__code-item--python::after {
154+
content: "";
155+
width: var(--code-tab-logo-width);
156+
height: var(--code-tab-logo-height);
157+
background: url("https://raw.githubusercontent.com/devicons/devicon/master/icons/python/python-plain.svg")
158+
no-repeat;
159+
margin-block: auto;
160+
}
161+
.openapi-tabs__code-item--python.active {
162+
box-shadow: 0 0 0 3px var(--openapi-code-tab-shadow-color-python);
163+
border-color: var(--openapi-code-tab-border-color-python);
164+
}

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/code-snippets-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ export type CodeSampleLanguage =
2222
| "JavaScript"
2323
| "Kotlin"
2424
| "Objective-C"
25+
| "OCaml"
2526
| "Perl"
2627
| "PHP"
2728
| "PowerShell"
2829
| "Python"
30+
| "R"
2931
| "Ruby"
3032
| "Rust"
3133
| "Scala"

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx

Lines changed: 6 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -27,120 +27,10 @@ import {
2727
getCodeSampleSourceFromLanguage,
2828
mergeArraysbyLanguage,
2929
mergeCodeSampleLanguage,
30+
generateLanguageSet,
3031
} from "./languages";
3132

32-
export const languageSet: Language[] = [
33-
{
34-
highlight: "bash",
35-
language: "curl",
36-
codeSampleLanguage: "Shell",
37-
logoClass: "bash",
38-
options: {
39-
longFormat: false,
40-
followRedirect: true,
41-
trimRequestBody: true,
42-
},
43-
variant: "cURL",
44-
variants: ["curl"],
45-
},
46-
{
47-
highlight: "python",
48-
language: "python",
49-
codeSampleLanguage: "Python",
50-
logoClass: "python",
51-
options: {
52-
followRedirect: true,
53-
trimRequestBody: true,
54-
},
55-
variant: "requests",
56-
variants: ["requests", "http.client"],
57-
},
58-
{
59-
highlight: "go",
60-
language: "go",
61-
codeSampleLanguage: "Go",
62-
logoClass: "go",
63-
options: {
64-
followRedirect: true,
65-
trimRequestBody: true,
66-
},
67-
variant: "native",
68-
variants: ["native"],
69-
},
70-
{
71-
highlight: "javascript",
72-
language: "nodejs",
73-
codeSampleLanguage: "JavaScript",
74-
logoClass: "nodejs",
75-
options: {
76-
ES6_enabled: true,
77-
followRedirect: true,
78-
trimRequestBody: true,
79-
},
80-
variant: "axios",
81-
variants: ["axios", "native"],
82-
},
83-
{
84-
highlight: "ruby",
85-
language: "ruby",
86-
codeSampleLanguage: "Ruby",
87-
logoClass: "ruby",
88-
options: {
89-
followRedirect: true,
90-
trimRequestBody: true,
91-
},
92-
variant: "Net::HTTP",
93-
variants: ["net::http"],
94-
},
95-
{
96-
highlight: "csharp",
97-
language: "csharp",
98-
codeSampleLanguage: "C#",
99-
logoClass: "csharp",
100-
options: {
101-
followRedirect: true,
102-
trimRequestBody: true,
103-
},
104-
variant: "RestSharp",
105-
variants: ["restsharp", "httpclient"],
106-
},
107-
{
108-
highlight: "php",
109-
language: "php",
110-
codeSampleLanguage: "PHP",
111-
logoClass: "php",
112-
options: {
113-
followRedirect: true,
114-
trimRequestBody: true,
115-
},
116-
variant: "cURL",
117-
variants: ["curl", "guzzle", "pecl_http", "http_request2"],
118-
},
119-
{
120-
highlight: "java",
121-
language: "java",
122-
codeSampleLanguage: "Java",
123-
logoClass: "java",
124-
options: {
125-
followRedirect: true,
126-
trimRequestBody: true,
127-
},
128-
variant: "OkHttp",
129-
variants: ["okhttp", "unirest"],
130-
},
131-
{
132-
highlight: "powershell",
133-
language: "powershell",
134-
codeSampleLanguage: "PowerShell",
135-
logoClass: "powershell",
136-
options: {
137-
followRedirect: true,
138-
trimRequestBody: true,
139-
},
140-
variant: "RestMethod",
141-
variants: ["restmethod"],
142-
},
143-
];
33+
export const languageSet: Language[] = generateLanguageSet();
14434

14535
export interface Props {
14636
postman: sdk.Request;
@@ -339,6 +229,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
339229

340230
return (
341231
<>
232+
{/* Outer language tabs */}
342233
<CodeTabs
343234
groupId="code-samples"
344235
action={{
@@ -347,6 +238,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
347238
setSelectedSample: setSelectedSample,
348239
}}
349240
languageSet={mergedLangs}
241+
defaultValue={defaultLang[0]?.language ?? mergedLangs[0].language}
350242
lazy
351243
>
352244
{mergedLangs.map((lang) => {
@@ -359,6 +251,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
359251
className: `openapi-tabs__code-item--${lang.logoClass}`,
360252
}}
361253
>
254+
{/* Inner x-codeSamples tabs */}
362255
{lang.samples && (
363256
<CodeTabs
364257
className="openapi-tabs__code-container-inner"
@@ -400,6 +293,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
400293
</CodeTabs>
401294
)}
402295

296+
{/* Inner generated code snippets */}
403297
<CodeTabs
404298
className="openapi-tabs__code-container-inner"
405299
action={{

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/languages.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import find from "lodash/find";
99
import isArray from "lodash/isArray";
1010
import mergeWith from "lodash/mergeWith";
1111
import unionBy from "lodash/unionBy";
12+
import codegen from "postman-code-generators";
1213

1314
import { CodeSample, Language } from "./code-snippets-types";
1415

@@ -73,3 +74,27 @@ export function getCodeSampleSourceFromLanguage(language: Language) {
7374

7475
return "";
7576
}
77+
78+
export function generateLanguageSet() {
79+
const languageSet: Language[] = [];
80+
codegen.getLanguageList().forEach((language: any) => {
81+
const variants: any = [];
82+
language.variants.forEach((variant: any) => {
83+
variants.push(variant.key);
84+
});
85+
languageSet.push({
86+
highlight: language.syntax_mode,
87+
language: language.key,
88+
codeSampleLanguage: language.label,
89+
logoClass: language.key,
90+
options: {
91+
longFormat: false,
92+
followRedirect: true,
93+
trimRequestBody: true,
94+
},
95+
variant: variants[0],
96+
variants: variants,
97+
});
98+
});
99+
return languageSet;
100+
}

0 commit comments

Comments
 (0)