Skip to content

Commit edd9756

Browse files
sserratablindaa121
andauthored
[Enhancement] Expand supported CodeTab languages (#235)
* Generate available languages object and add C# and Ruby to languageSet * Fetch dev icons by URL * Write languages metadata to JSON file * Switch back to non-forked postman-code-generators * Add languageTabs and additionalLanguages * Add eslint rule to ignore empty array callback return * Refactor languageSet and add support for PHP * Switch back to forked @paloaltonetworks/postman-code-generators * Update languageTabs example * Use space-between for code tabs ul * Use table-row-group to align tabs to left and auto wrap (removes scroll) * Increase size of dev icons and padding * Support generating code snippet when only one language is defined and for merging user config with languageSet * Update languageTabs example Co-authored-by: Bryan <blindaa121@gmail.com>
1 parent c915373 commit edd9756

File tree

12 files changed

+1643
-53
lines changed

12 files changed

+1643
-53
lines changed

demo/docusaurus.config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,45 @@ const config = {
135135
prism: {
136136
theme: lightCodeTheme,
137137
darkTheme: darkCodeTheme,
138+
additionalLanguages: ["ruby", "csharp", "php"],
138139
},
140+
languageTabs: [
141+
{
142+
highlight: "bash",
143+
language: "curl",
144+
logoClass: "bash",
145+
},
146+
{
147+
highlight: "python",
148+
language: "python",
149+
logoClass: "python",
150+
},
151+
{
152+
highlight: "go",
153+
language: "go",
154+
logoClass: "go",
155+
},
156+
{
157+
highlight: "javascript",
158+
language: "nodejs",
159+
logoClass: "nodejs",
160+
},
161+
// {
162+
// highlight: "ruby",
163+
// language: "ruby",
164+
// logoClass: "ruby",
165+
// },
166+
{
167+
highlight: "csharp",
168+
language: "csharp",
169+
logoClass: "csharp",
170+
},
171+
// {
172+
// highlight: "php",
173+
// language: "php",
174+
// logoClass: "php",
175+
// },
176+
],
139177
}),
140178

141179
plugins: [

packages/docusaurus-theme-openapi-docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"dependencies": {
4545
"@docusaurus/theme-common": "2.0.1",
4646
"@mdx-js/react": "^1.6.21",
47-
"@paloaltonetworks/postman-code-generators": "1.1.5-hotfix.5",
47+
"@paloaltonetworks/postman-code-generators": "^1.1.9",
4848
"@paloaltonetworks/postman-collection": "^4.1.0",
4949
"@reduxjs/toolkit": "^1.7.1",
5050
"buffer": "^6.0.3",

packages/docusaurus-theme-openapi-docs/src/theme/ApiDemoPanel/CodeTabs/styles.module.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@
77
}
88

99
.code__tabs {
10-
justify-content: space-around;
10+
display: table-row-group;
11+
}
12+
13+
@media only screen and (min-width: 768px) and (max-width: 996px) {
14+
.code__tabs {
15+
justify-content: space-around;
16+
}
1117
}

packages/docusaurus-theme-openapi-docs/src/theme/ApiDemoPanel/Curl/index.tsx

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,86 @@ import buildPostmanRequest from "./../buildPostmanRequest";
1919
import styles from "./styles.module.css";
2020

2121
interface Language {
22-
tabName: string;
23-
highlight: string;
22+
highlight?: string;
2423
language: string;
25-
codeLanguage?: string;
26-
variant: string;
27-
options: { [key: string]: boolean };
24+
logoClass?: string;
25+
variant?: string;
26+
options?: { [key: string]: boolean };
2827
source?: string;
2928
}
3029

3130
export const languageSet: Language[] = [
3231
{
33-
tabName: "cURL",
3432
highlight: "bash",
3533
language: "curl",
36-
codeLanguage: "bash",
37-
variant: "curl",
34+
logoClass: "bash",
3835
options: {
3936
longFormat: false,
4037
followRedirect: true,
4138
trimRequestBody: true,
4239
},
40+
variant: "cURL",
4341
},
4442
{
45-
tabName: "Python",
4643
highlight: "python",
4744
language: "python",
48-
codeLanguage: "python",
49-
variant: "requests",
45+
logoClass: "python",
5046
options: {
5147
followRedirect: true,
5248
trimRequestBody: true,
5349
},
50+
variant: "requests",
5451
},
5552
{
56-
tabName: "Go",
5753
highlight: "go",
5854
language: "go",
59-
codeLanguage: "go",
60-
variant: "native",
55+
logoClass: "go",
6156
options: {
6257
followRedirect: true,
6358
trimRequestBody: true,
6459
},
60+
variant: "native",
6561
},
6662
{
67-
tabName: "Node",
6863
highlight: "javascript",
6964
language: "nodejs",
70-
codeLanguage: "javascript",
71-
variant: "axios",
65+
logoClass: "nodejs",
7266
options: {
7367
ES6_enabled: true,
7468
followRedirect: true,
7569
trimRequestBody: true,
7670
},
71+
variant: "axios",
72+
},
73+
{
74+
highlight: "ruby",
75+
language: "ruby",
76+
logoClass: "ruby",
77+
options: {
78+
followRedirect: true,
79+
trimRequestBody: true,
80+
},
81+
variant: "Net::HTTP",
82+
},
83+
{
84+
highlight: "csharp",
85+
language: "csharp",
86+
logoClass: "csharp",
87+
options: {
88+
followRedirect: true,
89+
trimRequestBody: true,
90+
},
91+
variant: "RestSharp",
92+
},
93+
{
94+
highlight: "php",
95+
language: "php",
96+
logoClass: "php",
97+
options: {
98+
followRedirect: true,
99+
trimRequestBody: true,
100+
},
101+
variant: "cURL",
77102
},
78103
];
79104

@@ -120,10 +145,15 @@ function Curl({ postman, codeSamples }: Props) {
120145

121146
const defaultLang: Language[] = languageSet.filter(
122147
(lang) =>
123-
lang.codeLanguage === localStorage.getItem("docusaurus.tab.code-samples")
148+
lang.language === localStorage.getItem("docusaurus.tab.code-samples")
124149
);
125150

126-
const [language, setLanguage] = useState(defaultLang[0] ?? langs[0]);
151+
const [language, setLanguage] = useState(() => {
152+
if (langs.length === 1) {
153+
return langs[0];
154+
}
155+
return defaultLang[0] ?? langs[0];
156+
});
127157

128158
const [codeText, setCodeText] = useState("");
129159

@@ -155,6 +185,39 @@ function Curl({ postman, codeSamples }: Props) {
155185
);
156186
} else if (language && !!language.source) {
157187
setCodeText(language.source);
188+
} else if (language && !language.options) {
189+
const langSource = languageSet.filter(
190+
(lang) => lang.language === language.language
191+
);
192+
193+
// Merges user-defined language with default languageSet
194+
// This allows users to define only the minimal properties necessary in languageTabs
195+
// User-defined properties should override languageSet properties
196+
const mergedLanguage = { ...langSource[0], ...language };
197+
const postmanRequest = buildPostmanRequest(postman, {
198+
queryParams,
199+
pathParams,
200+
cookieParams,
201+
contentType,
202+
accept,
203+
headerParams,
204+
body,
205+
server,
206+
auth,
207+
});
208+
209+
codegen.convert(
210+
mergedLanguage.language,
211+
mergedLanguage.variant,
212+
postmanRequest,
213+
mergedLanguage.options,
214+
(error: any, snippet: string) => {
215+
if (error) {
216+
return;
217+
}
218+
setCodeText(snippet);
219+
}
220+
);
158221
} else {
159222
setCodeText("");
160223
}
@@ -184,10 +247,14 @@ function Curl({ postman, codeSamples }: Props) {
184247
<CodeTab
185248
value={lang.language}
186249
label={""}
187-
key={lang.tabName || lang.label}
188-
attributes={{ className: `code__tab--${lang.codeLanguage}` }}
250+
key={
251+
lang.variant
252+
? `${lang.language}-${lang.variant}`
253+
: lang.language
254+
}
255+
attributes={{ className: `code__tab--${lang.logoClass}` }}
189256
>
190-
<CodeBlock language={lang.codeLanguage}>{codeText}</CodeBlock>
257+
<CodeBlock language={lang.highlight}>{codeText}</CodeBlock>
191258
</CodeTab>
192259
);
193260
})}

0 commit comments

Comments
 (0)