Skip to content

Commit

Permalink
add support to x-codeSamples
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Jan 11, 2024
1 parent f6641c0 commit 7e84b6e
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1,409 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ============================================================================
* 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 };
labels?: string[];
source?: string;
}

// https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples
export interface CodeSample {
source: string;
lang: CodeSampleLanguage;
label?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +36,7 @@ export const languageSet: Language[] = [
{
highlight: "python",
language: "python",
codeSampleLanguage: "Python",
logoClass: "python",
options: {
followRedirect: true,
Expand All @@ -53,6 +48,7 @@ export const languageSet: Language[] = [
{
highlight: "go",
language: "go",
codeSampleLanguage: "Go",
logoClass: "go",
options: {
followRedirect: true,
Expand All @@ -64,6 +60,7 @@ export const languageSet: Language[] = [
{
highlight: "javascript",
language: "nodejs",
codeSampleLanguage: "JavaScript",
logoClass: "nodejs",
options: {
ES6_enabled: true,
Expand All @@ -76,6 +73,7 @@ export const languageSet: Language[] = [
{
highlight: "ruby",
language: "ruby",
codeSampleLanguage: "Ruby",
logoClass: "ruby",
options: {
followRedirect: true,
Expand All @@ -87,6 +85,7 @@ export const languageSet: Language[] = [
{
highlight: "csharp",
language: "csharp",
codeSampleLanguage: "C#",
logoClass: "csharp",
options: {
followRedirect: true,
Expand All @@ -98,6 +97,7 @@ export const languageSet: Language[] = [
{
highlight: "php",
language: "php",
codeSampleLanguage: "PHP",
logoClass: "php",
options: {
followRedirect: true,
Expand All @@ -109,6 +109,7 @@ export const languageSet: Language[] = [
{
highlight: "java",
language: "java",
codeSampleLanguage: "Java",
logoClass: "java",
options: {
followRedirect: true,
Expand All @@ -120,6 +121,7 @@ export const languageSet: Language[] = [
{
highlight: "powershell",
language: "powershell",
codeSampleLanguage: "PowerShell",
logoClass: "powershell",
options: {
followRedirect: true,
Expand All @@ -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 (
<div role="tabpanel" className={className} {...{ hidden }}>
{children}
Expand Down Expand Up @@ -165,25 +167,27 @@ function CodeSnippets({ postman, codeSamples }: Props) {
const langs = [
...((siteConfig?.themeConfig?.languageTabs as Language[] | undefined) ??
languageSet),
...codeSamples,
];

// Filter languageSet by user-defined langs
const filteredLanguageSet = languageSet.filter((ls) => {
return langs.some((lang) => {
return lang.language === ls.language;
return (lang as Language).language === ls.language;
});
});

// Merge user-defined langs into languageSet
const mergedLangs = merge(filteredLanguageSet, langs);
const mergedLangs = mergeCodeSampleLanguage(
merge(filteredLanguageSet, langs),
codeSamples
);

// Read defaultLang from localStorage
const defaultLang: Language[] = mergedLangs.filter(
(lang) =>
lang.language === localStorage.getItem("docusaurus.tab.code-samples")
);
const [selectedVariant, setSelectedVariant] = useState();
const [selectedVariant, setSelectedVariant] = useState<string | undefined>();
const [language, setLanguage] = useState(() => {
// Return first index if only 1 user-defined language exists
if (mergedLangs.length === 1) {
Expand All @@ -192,10 +196,17 @@ function CodeSnippets({ postman, codeSamples }: Props) {
// Fall back to language in localStorage or first user-defined language
return defaultLang[0] ?? mergedLangs[0];
});
const [codeText, setCodeText] = useState("");
const [codeText, setCodeText] = useState<string>("");

useEffect(() => {
if (language && !!language.options) {
// initial active language is custom code sample
if (
language &&
!!language.source &&
language.codeSampleLanguage.toLowerCase() === language.variant
) {
setCodeText(language.source);
} else if (language && !!language.options) {
const postmanRequest = buildPostmanRequest(postman, {
queryParams,
pathParams,
Expand All @@ -219,8 +230,6 @@ function CodeSnippets({ postman, codeSamples }: Props) {
setCodeText(snippet);
}
);
} else if (language && !!language.source) {
setCodeText(language.source);
} else if (language && !language.options) {
const langSource = mergedLangs.filter(
(lang) => lang.language === language.language
Expand Down Expand Up @@ -272,7 +281,17 @@ function CodeSnippets({ postman, codeSamples }: Props) {
mergedLangs,
]);

useEffect(() => {
useEffect(function onSelectedVariantUpdate() {
if (
language.source &&
selectedVariant === language.codeSampleLanguage.toLowerCase()
) {
console.log("entered");

setCodeText(language.source);
return;
}

if (selectedVariant && selectedVariant !== language.variant) {
const postmanRequest = buildPostmanRequest(postman, {
queryParams,
Expand Down Expand Up @@ -335,11 +354,13 @@ function CodeSnippets({ postman, codeSamples }: Props) {
defaultValue={selectedVariant}
lazy
>
{lang.variants.map((variant) => {
{lang.variants.map((variant, index) => {
return (
<CodeTab
value={variant.toLowerCase()}
label={variant.toUpperCase()}
label={
lang.labels ? lang.labels[index] : variant.toUpperCase()
}
key={`${lang.language}-${lang.variant}`}
attributes={{
className: `openapi-tabs__code-item--variant`,
Expand Down
Loading

0 comments on commit 7e84b6e

Please sign in to comment.