-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add support to x-codeSamples (#697)
* add support to x-codeSamples * fix select variant update * remove unused cast * split code samples and variants
1 parent
ef8ab66
commit 8a42186
Showing
8 changed files
with
208 additions
and
1,311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...s/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/code-snippets-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,290 changes: 0 additions & 1,290 deletions
1,290
packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/languages.json
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/languages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* ============================================================================ | ||
* 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. | ||
* ========================================================================== */ | ||
|
||
import { CodeSample, Language } from "./code-snippets-types"; | ||
|
||
export function mergeCodeSampleLanguage( | ||
languages: Language[], | ||
codeSamples: CodeSample[] | ||
): Language[] { | ||
return languages.map((language) => { | ||
const languageCodeSamples = codeSamples.filter( | ||
({ lang }) => lang === language.codeSampleLanguage | ||
); | ||
|
||
if (languageCodeSamples.length) { | ||
const samples = languageCodeSamples.map(({ lang }) => lang); | ||
const samplesLabels = languageCodeSamples.map( | ||
({ label, lang }) => label || lang | ||
); | ||
const samplesSources = languageCodeSamples.map(({ source }) => source); | ||
|
||
return { | ||
...language, | ||
sample: samples[0], | ||
samples, | ||
samplesSources, | ||
samplesLabels, | ||
}; | ||
} | ||
|
||
return language; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters