diff --git a/demo/examples/petstore.yaml b/demo/examples/petstore.yaml index c27f0d903..2241e02b4 100644 --- a/demo/examples/petstore.yaml +++ b/demo/examples/petstore.yaml @@ -147,6 +147,7 @@ paths: Console.WriteLine(response.getRawResponse()); } - lang: PHP + label: Custom source: | $form = new \PetStore\Entities\Pet(); $form->setPetType("Dog"); diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/code-snippets-types.ts b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/code-snippets-types.ts new file mode 100644 index 000000000..e46ed106d --- /dev/null +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/code-snippets-types.ts @@ -0,0 +1,55 @@ +/* ============================================================================ + * Copyright (c) Palo Alto Networks + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * ========================================================================== */ + +// https://github.com/github-linguist/linguist/blob/master/lib/linguist/popular.yml +export 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"; + +export interface Language { + highlight: string; + language: string; + codeSampleLanguage: CodeSampleLanguage; + logoClass: string; + variant: string; + variants: string[]; + options?: { [key: string]: boolean }; + sample?: string; + samples?: string[]; + samplesSources?: string[]; + samplesLabels?: string[]; +} + +// https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples +export interface CodeSample { + source: string; + lang: CodeSampleLanguage; + label?: string; +} diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx index 9bd90e344..e0fe433d6 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx @@ -16,20 +16,14 @@ import CodeTabs from "@theme/ApiExplorer/CodeTabs"; import { useTypedSelector } from "@theme/ApiItem/hooks"; import merge from "lodash/merge"; -export interface Language { - highlight: string; - language: string; - logoClass: string; - variant: string; - variants: string[]; - options: { [key: string]: boolean }; - source?: string; -} +import { CodeSample, Language } from "./code-snippets-types"; +import { mergeCodeSampleLanguage } from "./languages"; export const languageSet: Language[] = [ { highlight: "bash", language: "curl", + codeSampleLanguage: "Shell", logoClass: "bash", options: { longFormat: false, @@ -42,6 +36,7 @@ export const languageSet: Language[] = [ { highlight: "python", language: "python", + codeSampleLanguage: "Python", logoClass: "python", options: { followRedirect: true, @@ -53,6 +48,7 @@ export const languageSet: Language[] = [ { highlight: "go", language: "go", + codeSampleLanguage: "Go", logoClass: "go", options: { followRedirect: true, @@ -64,6 +60,7 @@ export const languageSet: Language[] = [ { highlight: "javascript", language: "nodejs", + codeSampleLanguage: "JavaScript", logoClass: "nodejs", options: { ES6_enabled: true, @@ -71,11 +68,12 @@ export const languageSet: Language[] = [ trimRequestBody: true, }, variant: "axios", - variants: ["axios", "native", "request", "unirest"], + variants: ["axios", "native"], }, { highlight: "ruby", language: "ruby", + codeSampleLanguage: "Ruby", logoClass: "ruby", options: { followRedirect: true, @@ -87,6 +85,7 @@ export const languageSet: Language[] = [ { highlight: "csharp", language: "csharp", + codeSampleLanguage: "C#", logoClass: "csharp", options: { followRedirect: true, @@ -98,6 +97,7 @@ export const languageSet: Language[] = [ { highlight: "php", language: "php", + codeSampleLanguage: "PHP", logoClass: "php", options: { followRedirect: true, @@ -109,6 +109,7 @@ export const languageSet: Language[] = [ { highlight: "java", language: "java", + codeSampleLanguage: "Java", logoClass: "java", options: { followRedirect: true, @@ -120,6 +121,7 @@ export const languageSet: Language[] = [ { highlight: "powershell", language: "powershell", + codeSampleLanguage: "PowerShell", logoClass: "powershell", options: { followRedirect: true, @@ -132,10 +134,10 @@ export const languageSet: Language[] = [ export interface Props { postman: sdk.Request; - codeSamples: any; // TODO: Type this... + codeSamples: CodeSample[]; } -function CodeTab({ children, hidden, className, onClick }: any): JSX.Element { +function CodeTab({ children, hidden, className }: any): JSX.Element { return (