Skip to content

Commit

Permalink
Merge pull request #945 from PaloAltoNetworks/language-tabs
Browse files Browse the repository at this point in the history
Expand language tab support
  • Loading branch information
sserrata authored Sep 5, 2024
2 parents a89b417 + 433b233 commit 38b4fb1
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 123 deletions.
48 changes: 48 additions & 0 deletions demo/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const config: Config = {
"powershell",
"json",
"bash",
"dart",
"objectivec",
"r",
],
},
languageTabs: [
Expand Down Expand Up @@ -194,6 +197,51 @@ const config: Config = {
language: "powershell",
logoClass: "powershell",
},
{
highlight: "dart",
language: "dart",
logoClass: "dart",
},
{
highlight: "javascript",
language: "javascript",
logoClass: "javascript",
},
{
highlight: "c",
language: "c",
logoClass: "c",
},
{
highlight: "objective-c",
language: "objective-c",
logoClass: "objective-c",
},
{
highlight: "ocaml",
language: "ocaml",
logoClass: "ocaml",
},
{
highlight: "r",
language: "r",
logoClass: "r",
},
{
highlight: "swift",
language: "swift",
logoClass: "swift",
},
{
highlight: "kotlin",
language: "kotlin",
logoClass: "kotlin",
},
{
highlight: "rust",
language: "rust",
logoClass: "rust",
},
],
algolia: {
apiKey: "441074cace987cbf4640c039ebed303c",
Expand Down
16 changes: 16 additions & 0 deletions demo/src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,19 @@ div[class^="announcementBar_"] {
);
font-weight: bold;
}

.openapi-tabs__code-item--python {
color: var(--ifm-color-success);
}
.openapi-tabs__code-item--python::after {
content: "";
width: var(--code-tab-logo-width);
height: var(--code-tab-logo-height);
background: url("https://raw.githubusercontent.com/devicons/devicon/master/icons/python/python-plain.svg")
no-repeat;
margin-block: auto;
}
.openapi-tabs__code-item--python.active {
box-shadow: 0 0 0 3px var(--openapi-code-tab-shadow-color-python);
border-color: var(--openapi-code-tab-border-color-python);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export type CodeSampleLanguage =
| "JavaScript"
| "Kotlin"
| "Objective-C"
| "OCaml"
| "Perl"
| "PHP"
| "PowerShell"
| "Python"
| "R"
| "Ruby"
| "Rust"
| "Scala"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,120 +27,10 @@ import {
getCodeSampleSourceFromLanguage,
mergeArraysbyLanguage,
mergeCodeSampleLanguage,
generateLanguageSet,
} from "./languages";

export const languageSet: Language[] = [
{
highlight: "bash",
language: "curl",
codeSampleLanguage: "Shell",
logoClass: "bash",
options: {
longFormat: false,
followRedirect: true,
trimRequestBody: true,
},
variant: "cURL",
variants: ["curl"],
},
{
highlight: "python",
language: "python",
codeSampleLanguage: "Python",
logoClass: "python",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "requests",
variants: ["requests", "http.client"],
},
{
highlight: "go",
language: "go",
codeSampleLanguage: "Go",
logoClass: "go",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "native",
variants: ["native"],
},
{
highlight: "javascript",
language: "nodejs",
codeSampleLanguage: "JavaScript",
logoClass: "nodejs",
options: {
ES6_enabled: true,
followRedirect: true,
trimRequestBody: true,
},
variant: "axios",
variants: ["axios", "native"],
},
{
highlight: "ruby",
language: "ruby",
codeSampleLanguage: "Ruby",
logoClass: "ruby",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "Net::HTTP",
variants: ["net::http"],
},
{
highlight: "csharp",
language: "csharp",
codeSampleLanguage: "C#",
logoClass: "csharp",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "RestSharp",
variants: ["restsharp", "httpclient"],
},
{
highlight: "php",
language: "php",
codeSampleLanguage: "PHP",
logoClass: "php",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "cURL",
variants: ["curl", "guzzle", "pecl_http", "http_request2"],
},
{
highlight: "java",
language: "java",
codeSampleLanguage: "Java",
logoClass: "java",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "OkHttp",
variants: ["okhttp", "unirest"],
},
{
highlight: "powershell",
language: "powershell",
codeSampleLanguage: "PowerShell",
logoClass: "powershell",
options: {
followRedirect: true,
trimRequestBody: true,
},
variant: "RestMethod",
variants: ["restmethod"],
},
];
export const languageSet: Language[] = generateLanguageSet();

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

return (
<>
{/* Outer language tabs */}
<CodeTabs
groupId="code-samples"
action={{
Expand All @@ -347,6 +238,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
setSelectedSample: setSelectedSample,
}}
languageSet={mergedLangs}
defaultValue={defaultLang[0]?.language ?? mergedLangs[0].language}
lazy
>
{mergedLangs.map((lang) => {
Expand All @@ -359,6 +251,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
className: `openapi-tabs__code-item--${lang.logoClass}`,
}}
>
{/* Inner x-codeSamples tabs */}
{lang.samples && (
<CodeTabs
className="openapi-tabs__code-container-inner"
Expand Down Expand Up @@ -400,6 +293,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
</CodeTabs>
)}

{/* Inner generated code snippets */}
<CodeTabs
className="openapi-tabs__code-container-inner"
action={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import find from "lodash/find";
import isArray from "lodash/isArray";
import mergeWith from "lodash/mergeWith";
import unionBy from "lodash/unionBy";
import codegen from "postman-code-generators";

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

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

return "";
}

export function generateLanguageSet() {
const languageSet: Language[] = [];
codegen.getLanguageList().forEach((language: any) => {
const variants: any = [];
language.variants.forEach((variant: any) => {
variants.push(variant.key);
});
languageSet.push({
highlight: language.syntax_mode,
language: language.key,
codeSampleLanguage: language.label,
logoClass: language.key,
options: {
longFormat: false,
followRedirect: true,
trimRequestBody: true,
},
variant: variants[0],
variants: variants,
});
});
return languageSet;
}
Loading

0 comments on commit 38b4fb1

Please sign in to comment.