docusaurus-theme-openapi-docs
Version:
OpenAPI theme for Docusaurus.
111 lines (110 loc) • 3.68 kB
JavaScript
;
/* ============================================================================
* 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.
* ========================================================================== */
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (
!desc ||
("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
) {
desc = {
enumerable: true,
get: function () {
return m[k];
},
};
}
Object.defineProperty(o, k2, desc);
}
: function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
var __setModuleDefault =
(this && this.__setModuleDefault) ||
(Object.create
? function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}
: function (o, v) {
o["default"] = v;
});
var __importStar =
(this && this.__importStar) ||
(function () {
var ownKeys = function (o) {
ownKeys =
Object.getOwnPropertyNames ||
function (o) {
var ar = [];
for (var k in o)
if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k = ownKeys(mod), i = 0; i < k.length; i++)
if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ApiCodeBlock;
const react_1 = __importStar(require("react"));
const useIsBrowser_1 = __importDefault(require("@docusaurus/useIsBrowser"));
const Element_1 = __importDefault(
require("@theme/ApiExplorer/ApiCodeBlock/Content/Element")
);
const String_1 = __importDefault(
require("@theme/ApiExplorer/ApiCodeBlock/Content/String")
);
/**
* Best attempt to make the children a plain string so it is copyable. If there
* are react elements, we will not be able to copy the content, and it will
* return `children` as-is; otherwise, it concatenates the string children
* together.
*/
function maybeStringifyChildren(children) {
if (
react_1.default.Children.toArray(children).some((el) =>
(0, react_1.isValidElement)(el)
)
) {
return children;
}
// The children is now guaranteed to be one/more plain strings
return Array.isArray(children) ? children.join("") : children;
}
function ApiCodeBlock({ children: rawChildren, ...props }) {
// The Prism theme on SSR is always the default theme but the site theme can
// be in a different mode. React hydration doesn't update DOM styles that come
// from SSR. Hence force a re-render after mounting to apply the current
// relevant styles.
const isBrowser = (0, useIsBrowser_1.default)();
const children = maybeStringifyChildren(rawChildren);
const CodeBlockComp =
typeof children === "string" ? String_1.default : Element_1.default;
return react_1.default.createElement(
CodeBlockComp,
{ key: String(isBrowser), ...props },
children
);
}