UNPKG

@storybook/addon-docs

Version:
123 lines (91 loc) 4.55 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } require("core-js/modules/es.array.concat"); require("core-js/modules/es.string.trim"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Description = exports.getDescriptionProps = exports.DescriptionType = void 0; var _react = _interopRequireWildcard(require("react")); var _components = require("@storybook/components"); var _DocsContext = require("./DocsContext"); var _shared = require("./shared"); var _docgen = require("../lib/docgen"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var DescriptionType; exports.DescriptionType = DescriptionType; (function (DescriptionType) { DescriptionType["INFO"] = "info"; DescriptionType["NOTES"] = "notes"; DescriptionType["DOCGEN"] = "docgen"; DescriptionType["LEGACY_5_2"] = "legacy-5.2"; DescriptionType["AUTO"] = "auto"; })(DescriptionType || (exports.DescriptionType = DescriptionType = {})); var getNotes = function getNotes(notes) { return notes && (typeof notes === 'string' ? notes : (0, _docgen.str)(notes.markdown) || (0, _docgen.str)(notes.text)); }; var getInfo = function getInfo(info) { return info && (typeof info === 'string' ? info : (0, _docgen.str)(info.text)); }; var noDescription = function noDescription(component) { return null; }; var getDescriptionProps = function getDescriptionProps(_ref, _ref2) { var of = _ref.of, type = _ref.type, markdown = _ref.markdown, children = _ref.children; var parameters = _ref2.parameters; if (children || markdown) { return { markdown: children || markdown }; } var component = parameters.component, notes = parameters.notes, info = parameters.info, docs = parameters.docs; var _ref3 = docs || {}, _ref3$extractComponen = _ref3.extractComponentDescription, extractComponentDescription = _ref3$extractComponen === void 0 ? noDescription : _ref3$extractComponen; var target = of === _shared.CURRENT_SELECTION ? component : of; switch (type) { case DescriptionType.INFO: return { markdown: getInfo(info) }; case DescriptionType.NOTES: return { markdown: getNotes(notes) }; // FIXME: remove in 6.0 case DescriptionType.LEGACY_5_2: return { markdown: "\n".concat(getNotes(notes) || getInfo(info) || '', "\n\n").concat(extractComponentDescription(target) || '', "\n").trim() }; case DescriptionType.DOCGEN: case DescriptionType.AUTO: default: return { markdown: extractComponentDescription(target, parameters) }; } }; exports.getDescriptionProps = getDescriptionProps; var DescriptionContainer = function DescriptionContainer(props) { var context = (0, _react.useContext)(_DocsContext.DocsContext); var slot = props.slot; var _getDescriptionProps = getDescriptionProps(props, context), markdown = _getDescriptionProps.markdown; if (slot) { markdown = slot(markdown, context); } return markdown ? _react["default"].createElement(_components.Description, { markdown: markdown }) : null; }; // since we are in the docs blocks, assume default description if for primary component story exports.Description = DescriptionContainer; DescriptionContainer.defaultProps = { of: '.' };