docusaurus-theme-openapi-docs 1.1.4 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,260 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import React, {
9
+ Children,
10
+ cloneElement,
11
+ isValidElement,
12
+ useEffect,
13
+ useRef,
14
+ useState,
15
+ } from "react";
16
+
17
+ import { duplicates } from "@docusaurus/theme-common";
18
+ import useIsBrowser from "@docusaurus/useIsBrowser";
19
+ import clsx from "clsx";
20
+
21
+ import styles from "./styles.module.css"; // A very rough duck type, but good enough to guard against mistakes while
22
+
23
+ const {
24
+ useScrollPositionBlocker,
25
+ useTabGroupChoice,
26
+ } = require("@docusaurus/theme-common/internal");
27
+
28
+ // allowing customization
29
+
30
+ function isTabItem(comp) {
31
+ return typeof comp.props.value !== "undefined";
32
+ }
33
+
34
+ function MimeTabsComponent(props) {
35
+ const {
36
+ lazy,
37
+ block,
38
+ defaultValue: defaultValueProp,
39
+ values: valuesProp,
40
+ groupId,
41
+ className,
42
+ } = props;
43
+ const children = Children.map(props.children, (child) => {
44
+ if (isValidElement(child) && isTabItem(child)) {
45
+ return child;
46
+ } // child.type.name will give non-sensical values in prod because of
47
+ // minification, but we assume it won't throw in prod.
48
+
49
+ throw new Error(
50
+ `Docusaurus error: Bad <Tabs> child <${
51
+ // @ts-expect-error: guarding against unexpected cases
52
+ typeof child.type === "string" ? child.type : child.type.name
53
+ }>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.`
54
+ );
55
+ });
56
+ const values =
57
+ valuesProp ?? // We only pick keys that we recognize. MDX would inject some keys by default
58
+ children.map(({ props: { value, label, attributes } }) => ({
59
+ value,
60
+ label,
61
+ attributes,
62
+ }));
63
+ const dup = duplicates(values, (a, b) => a.value === b.value);
64
+
65
+ if (dup.length > 0) {
66
+ throw new Error(
67
+ `Docusaurus error: Duplicate values "${dup
68
+ .map((a) => a.value)
69
+ .join(", ")}" found in <Tabs>. Every value needs to be unique.`
70
+ );
71
+ } // When defaultValueProp is null, don't show a default tab
72
+
73
+ const defaultValue =
74
+ defaultValueProp === null
75
+ ? defaultValueProp
76
+ : defaultValueProp ??
77
+ children.find((child) => child.props.default)?.props.value ??
78
+ children[0]?.props.value;
79
+
80
+ if (defaultValue !== null && !values.some((a) => a.value === defaultValue)) {
81
+ throw new Error(
82
+ `Docusaurus error: The <Tabs> has a defaultValue "${defaultValue}" but none of its children has the corresponding value. Available values are: ${values
83
+ .map((a) => a.value)
84
+ .join(
85
+ ", "
86
+ )}. If you intend to show no default tab, use defaultValue={null} instead.`
87
+ );
88
+ }
89
+
90
+ const { tabGroupChoices, setTabGroupChoices } = useTabGroupChoice();
91
+ const [selectedValue, setSelectedValue] = useState(defaultValue);
92
+ const tabRefs = [];
93
+ const { blockElementScrollPositionUntilNextRender } =
94
+ useScrollPositionBlocker();
95
+
96
+ if (groupId != null) {
97
+ const relevantTabGroupChoice = tabGroupChoices[groupId];
98
+
99
+ if (
100
+ relevantTabGroupChoice != null &&
101
+ relevantTabGroupChoice !== selectedValue &&
102
+ values.some((value) => value.value === relevantTabGroupChoice)
103
+ ) {
104
+ setSelectedValue(relevantTabGroupChoice);
105
+ }
106
+ }
107
+
108
+ const handleTabChange = (event) => {
109
+ const newTab = event.currentTarget;
110
+ const newTabIndex = tabRefs.indexOf(newTab);
111
+ const newTabValue = values[newTabIndex].value;
112
+
113
+ if (newTabValue !== selectedValue) {
114
+ blockElementScrollPositionUntilNextRender(newTab);
115
+ setSelectedValue(newTabValue);
116
+
117
+ if (groupId != null) {
118
+ setTabGroupChoices(groupId, newTabValue);
119
+ }
120
+ }
121
+ };
122
+
123
+ const handleKeydown = (event) => {
124
+ let focusElement = null;
125
+
126
+ switch (event.key) {
127
+ case "ArrowRight": {
128
+ const nextTab = tabRefs.indexOf(event.currentTarget) + 1;
129
+ focusElement = tabRefs[nextTab] || tabRefs[0];
130
+ break;
131
+ }
132
+
133
+ case "ArrowLeft": {
134
+ const prevTab = tabRefs.indexOf(event.currentTarget) - 1;
135
+ focusElement = tabRefs[prevTab] || tabRefs[tabRefs.length - 1];
136
+ break;
137
+ }
138
+
139
+ default:
140
+ break;
141
+ }
142
+
143
+ focusElement?.focus();
144
+ };
145
+
146
+ const tabItemListContainerRef = useRef(null);
147
+ const [showTabArrows, setShowTabArrows] = useState(false);
148
+
149
+ useEffect(() => {
150
+ const tabOffsetWidth = tabItemListContainerRef.current.offsetWidth;
151
+ const tabScrollWidth = tabItemListContainerRef.current.scrollWidth;
152
+
153
+ if (tabOffsetWidth < tabScrollWidth) {
154
+ setShowTabArrows(true);
155
+ }
156
+ }, []);
157
+
158
+ const handleRightClick = () => {
159
+ tabItemListContainerRef.current.scrollLeft += 90;
160
+ };
161
+
162
+ const handleLeftClick = () => {
163
+ tabItemListContainerRef.current.scrollLeft -= 90;
164
+ };
165
+
166
+ return (
167
+ <div className="tabs__container">
168
+ <div className={styles.mimeTabsTopSection}>
169
+ {/* <strong>MIME Type</strong> */}
170
+ <div className={styles.mimeTabsContainer}>
171
+ {showTabArrows && (
172
+ <button
173
+ className={clsx(styles.tabArrow, styles.tabArrowLeft)}
174
+ onClick={handleLeftClick}
175
+ />
176
+ )}
177
+ <ul
178
+ ref={tabItemListContainerRef}
179
+ role="tablist"
180
+ aria-orientation="horizontal"
181
+ className={clsx(
182
+ styles.mimeTabsListContainer,
183
+ "tabs",
184
+ {
185
+ "tabs--block": block,
186
+ },
187
+ className
188
+ )}
189
+ >
190
+ {values.map(({ value, label, attributes }) => {
191
+ return (
192
+ <li
193
+ role="tab"
194
+ tabIndex={selectedValue === value ? 0 : -1}
195
+ aria-selected={selectedValue === value}
196
+ key={value}
197
+ ref={(tabControl) => tabRefs.push(tabControl)}
198
+ onKeyDown={handleKeydown}
199
+ onFocus={handleTabChange}
200
+ onClick={handleTabChange}
201
+ {...attributes}
202
+ className={clsx(
203
+ "tabs__item",
204
+ styles.tabItem,
205
+ attributes?.className,
206
+ {
207
+ [styles.mimeTabActive]: selectedValue === value,
208
+ }
209
+ )}
210
+ >
211
+ {/* <div
212
+ className={clsx(styles.mimeTabDot, mimeTabDotStyle)}
213
+ /> */}
214
+ {label ?? value}
215
+ </li>
216
+ );
217
+ })}
218
+ </ul>
219
+ {showTabArrows && (
220
+ <button
221
+ className={clsx(styles.tabArrow, styles.tabArrowRight)}
222
+ onClick={handleRightClick}
223
+ />
224
+ )}
225
+ </div>
226
+ </div>
227
+ {/* <hr /> */}
228
+ {lazy ? (
229
+ cloneElement(
230
+ children.filter(
231
+ (tabItem) => tabItem.props.value === selectedValue
232
+ )[0],
233
+ {
234
+ className: clsx("margin-vert--md", styles.mimeSchemaContainer),
235
+ }
236
+ )
237
+ ) : (
238
+ <div className={clsx("margin-vert--md", styles.mimeSchemaContainer)}>
239
+ {children.map((tabItem, i) =>
240
+ cloneElement(tabItem, {
241
+ key: i,
242
+ hidden: tabItem.props.value !== selectedValue,
243
+ })
244
+ )}
245
+ </div>
246
+ )}
247
+ </div>
248
+ );
249
+ }
250
+
251
+ export default function MimeTabs(props) {
252
+ const isBrowser = useIsBrowser();
253
+ return (
254
+ <MimeTabsComponent // Remount tabs after hydration
255
+ // Temporary fix for https://github.com/facebook/docusaurus/issues/5653
256
+ key={String(isBrowser)}
257
+ {...props}
258
+ />
259
+ );
260
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .tabItem {
9
+ display: flex;
10
+ align-items: center;
11
+ justify-content: center;
12
+ height: 2rem;
13
+ margin-top: 0 !important;
14
+ margin-right: 0.5rem;
15
+ /* border: 1px solid var(--openapi-code-dim-dark);
16
+ border-radius: 0; */
17
+ font-weight: var(--ifm-font-weight-normal);
18
+ /* color: var(--openapi-code-dim-dark); */
19
+ font-size: 12px;
20
+ }
21
+
22
+ .tabItem:active {
23
+ background-color: var(--ifm-color-emphasis-100);
24
+ }
25
+
26
+ /* .tabItem:hover {
27
+ color: var(--ifm-color-emphasis-500) !important;
28
+ } */
29
+
30
+ .tabItem:last-child {
31
+ margin-right: 0 !important;
32
+ }
33
+
34
+ /* Open API Response Code Tabs */
35
+ .mimeTabsTopSection {
36
+ display: flex;
37
+ justify-content: space-between;
38
+ align-items: center;
39
+ }
40
+
41
+ .mimeTabsContainer {
42
+ display: flex;
43
+ align-items: center;
44
+ max-width: 390px;
45
+ /* padding-left: 1rem; */
46
+ overflow: hidden;
47
+ }
48
+
49
+ .mimeTabsListContainer {
50
+ overflow-y: hidden;
51
+ overflow-x: scroll;
52
+ scroll-behavior: smooth;
53
+ }
54
+
55
+ .mimeTabsListContainer::-webkit-scrollbar {
56
+ display: none;
57
+ }
58
+
59
+ /* Response Code Tabs - Colored Dots */
60
+ .mimeTabDot {
61
+ width: 12.5px;
62
+ height: 12.5px;
63
+ margin-right: 5px;
64
+ border-radius: 50%;
65
+ }
66
+
67
+ .mimeTabDotSuccess {
68
+ background-color: var(--ifm-color-success);
69
+ }
70
+
71
+ .mimeTabDotDanger {
72
+ background-color: var(--ifm-color-danger);
73
+ }
74
+
75
+ .mimeTabDotInfo {
76
+ background-color: var(--ifm-color-info);
77
+ }
78
+
79
+ .mimeTabActive {
80
+ border-bottom-color: var(--ifm-tabs-color-active-border);
81
+ border-bottom-left-radius: 0;
82
+ border-bottom-right-radius: 0;
83
+ color: var(--ifm-tabs-color-active);
84
+ background-color: var(--ifm-color-emphasis-100);
85
+ }
86
+
87
+ .mimeSchemaContainer {
88
+ max-width: 600px;
89
+ }
90
+
91
+ /* Tab Arrows */
92
+ .tabArrow {
93
+ content: "";
94
+ height: 1.25rem;
95
+ width: 1.25rem;
96
+ border: none;
97
+ min-width: 1.25rem;
98
+ background: var(--ifm-menu-link-sublist-icon) 50% / 2rem 2rem;
99
+ filter: var(--ifm-menu-link-sublist-icon-filter);
100
+ }
101
+
102
+ .tabArrow:hover {
103
+ cursor: pointer;
104
+ }
105
+
106
+ .tabArrowLeft {
107
+ transform: rotate(270deg);
108
+ }
109
+
110
+ .tabArrowRight {
111
+ transform: rotate(90deg);
112
+ }
113
+
114
+ @media screen and (max-width: 500px) {
115
+ .mimeTabsTopSection {
116
+ flex-direction: column;
117
+ align-items: flex-start;
118
+ }
119
+
120
+ .mimeTabsContainer {
121
+ width: 100%;
122
+ margin-top: var(--ifm-spacing-vertical);
123
+ padding: 0;
124
+ }
125
+ }
@@ -7,6 +7,7 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
+ import CodeBlock from "@theme/CodeBlock";
10
11
  import ReactMarkdown from "react-markdown";
11
12
 
12
13
  import { createDescription } from "../../markdown/createDescription";
@@ -33,7 +34,21 @@ function ParamsItem({
33
34
 
34
35
  const renderDescription = guard(description, (description) => (
35
36
  <div>
36
- <ReactMarkdown children={createDescription(description)} />
37
+ <ReactMarkdown
38
+ children={createDescription(description)}
39
+ components={{
40
+ pre: "div",
41
+ code({ node, inline, className, children, ...props }) {
42
+ const match = /language-(\w+)/.exec(className || "");
43
+ if (inline) return <code>{children}</code>;
44
+ return !inline && match ? (
45
+ <CodeBlock className={className}>{children}</CodeBlock>
46
+ ) : (
47
+ <CodeBlock>{children}</CodeBlock>
48
+ );
49
+ },
50
+ }}
51
+ />
37
52
  </div>
38
53
  ));
39
54
 
@@ -7,6 +7,7 @@
7
7
 
8
8
  import React from "react";
9
9
 
10
+ import CodeBlock from "@theme/CodeBlock";
10
11
  import ReactMarkdown from "react-markdown";
11
12
 
12
13
  import { createDescription } from "../../markdown/createDescription";
@@ -23,13 +24,28 @@ function SchemaItem({
23
24
  schemaName,
24
25
  defaultValue,
25
26
  }) {
26
- const renderRequired = guard(required, () => (
27
- <strong className={styles.required}> required</strong>
28
- ));
27
+ const renderRequired = guard(
28
+ Array.isArray(required) ? required.includes(name) : required,
29
+ () => <strong className={styles.required}> required</strong>
30
+ );
29
31
 
30
32
  const renderSchemaDescription = guard(schemaDescription, (description) => (
31
- <div className={styles.schemaDescription}>
32
- <ReactMarkdown children={createDescription(description)} />
33
+ <div>
34
+ <ReactMarkdown
35
+ children={createDescription(description)}
36
+ components={{
37
+ pre: "div",
38
+ code({ node, inline, className, children, ...props }) {
39
+ const match = /language-(\w+)/.exec(className || "");
40
+ if (inline) return <code>{children}</code>;
41
+ return !inline && match ? (
42
+ <CodeBlock className={className}>{children}</CodeBlock>
43
+ ) : (
44
+ <CodeBlock>{children}</CodeBlock>
45
+ );
46
+ },
47
+ }}
48
+ />
33
49
  </div>
34
50
  ));
35
51
 
@@ -39,11 +55,14 @@ function SchemaItem({
39
55
  </div>
40
56
  ));
41
57
 
42
- const renderDefaultValue = guard(defaultValue, (value) => (
43
- <div className={styles.schemaQualifierMessage}>
44
- <ReactMarkdown children={`**Default value:** \`${value}\``} />
45
- </div>
46
- ));
58
+ const renderDefaultValue = guard(
59
+ typeof defaultValue === "boolean" ? defaultValue.toString() : defaultValue,
60
+ (value) => (
61
+ <div className={styles.schemaQualifierMessage}>
62
+ <ReactMarkdown children={`**Default value:** \`${value}\``} />
63
+ </div>
64
+ )
65
+ );
47
66
 
48
67
  const schemaContent = (
49
68
  <div>
@@ -39,7 +39,7 @@
39
39
  }
40
40
 
41
41
  .schemaTabsListContainer {
42
- padding: 0 0.25rem;
42
+ /* padding: 0 0.25rem; */
43
43
  overflow-y: hidden;
44
44
  overflow-x: scroll;
45
45
  scroll-behavior: smooth;
@@ -95,8 +95,4 @@
95
95
  .schemaTabsContainer {
96
96
  width: 100%;
97
97
  }
98
-
99
- .tabItem {
100
- height: 100%;
101
- }
102
98
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-theme-openapi-docs",
3
3
  "description": "OpenAPI theme for Docusaurus.",
4
- "version": "1.1.4",
4
+ "version": "1.1.7",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -50,7 +50,7 @@
50
50
  "buffer": "^6.0.3",
51
51
  "clsx": "^1.1.1",
52
52
  "crypto-js": "^4.1.1",
53
- "docusaurus-plugin-openapi-docs": "^1.1.4",
53
+ "docusaurus-plugin-openapi-docs": "^1.1.6",
54
54
  "immer": "^9.0.7",
55
55
  "lodash": "^4.17.20",
56
56
  "process": "^0.11.10",
@@ -68,5 +68,5 @@
68
68
  "engines": {
69
69
  "node": ">=14"
70
70
  },
71
- "gitHead": "dadb0aa5d3516b3020ff1d94288efd6f136e02ba"
71
+ "gitHead": "604cd3723c770fd306318d594e8fb4e68c39724c"
72
72
  }
@@ -24,9 +24,6 @@ export function guard<T>(
24
24
  value: T | undefined | string,
25
25
  cb: (value: T) => Children
26
26
  ): string {
27
- if (typeof value === "boolean") {
28
- value = value.toString();
29
- }
30
27
  if (value) {
31
28
  const children = cb(value as T);
32
29
  return render(children);
@@ -188,7 +188,7 @@ function Body({ requestBodyMetadata, jsonRequestBodyExample }: Props) {
188
188
  let language = "plaintext";
189
189
  let exampleBodyString = ""; //"body content";
190
190
 
191
- if (contentType === "application/json") {
191
+ if (contentType === "application/json" || contentType.endsWith("+json")) {
192
192
  if (jsonRequestBodyExample) {
193
193
  exampleBodyString = JSON.stringify(jsonRequestBodyExample, null, 2);
194
194
  }
@@ -175,6 +175,7 @@
175
175
 
176
176
  :global(.code__tab--python.tabs__item--active) {
177
177
  border-bottom-color: var(--ifm-color-success);
178
+ background-color: var(--ifm-color-emphasis-100);
178
179
  }
179
180
 
180
181
  :global(.language-python) {
@@ -198,6 +199,7 @@
198
199
 
199
200
  :global(.code__tab--go.tabs__item--active) {
200
201
  border-bottom-color: var(--ifm-color-info);
202
+ background-color: var(--ifm-color-emphasis-100);
201
203
  }
202
204
 
203
205
  :global(.language-go) {
@@ -221,6 +223,7 @@
221
223
 
222
224
  :global(.code__tab--javascript.tabs__item--active) {
223
225
  border-bottom-color: var(--ifm-color-warning);
226
+ background-color: var(--ifm-color-emphasis-100);
224
227
  }
225
228
 
226
229
  :global(.language-javascript) {
@@ -244,6 +247,7 @@
244
247
 
245
248
  :global(.code__tab--bash.tabs__item--active) {
246
249
  border-bottom-color: var(--ifm-color-danger);
250
+ background-color: var(--ifm-color-emphasis-100);
247
251
  }
248
252
 
249
253
  :global(.language-bash) {