UNPKG

@telefonica/markdown-confluence-sync

Version:

Creates/updates/deletes Confluence pages based on markdown files in a directory. Supports Mermaid diagrams and per-page configuration using frontmatter metadata. Works great with Docusaurus

55 lines (54 loc) 2.06 kB
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital // SPDX-License-Identifier: Apache-2.0 import { replace } from "../../../../../lib/support/unist/unist-util-replace.js"; import { InvalidTabItemMissingLabelError } from "../../errors/InvalidTabItemMissingLabelError.js"; import { InvalidTabsFormatError } from "../../errors/InvalidTabsFormatError.js"; const mdxElement = "mdxJsxFlowElement"; /** * UnifiedPlugin to replace \<Tabs\> elements from tree. * * @throws {InvalidTabsFormatError} if \<Tabs\> tag does not have only TabItem children. * @throws {InvalidTabItemMissingLabelError} if \<TabItem\> tag does not have a label property. * @see {@link https://docusaurus.io/docs/markdown-features/tabs | Docusaurus Details} */ const remarkReplaceTabs = function remarkReplaceTabs() { return function (tree) { replace(tree, mdxElement, replaceTabsTag); }; }; function replaceTabsTag(node) { if (node.name !== "Tabs") { return node; } const tabsSections = [...node.children]; if (!tabsSections.every((child) => child.type === mdxElement && child.name === "TabItem")) { throw new InvalidTabsFormatError(); } if (!tabsSections.every((tabItem) => tabItem.attributes.find(checkLabelAttribute))) { throw new InvalidTabItemMissingLabelError(); } return { type: "list", ordered: false, children: tabsSections.map((tabItem) => processTabItem(tabItem)).flat(), }; } function processTabItem(tabItem) { return { type: "listItem", children: [ { type: "text", value: `${tabItem.attributes.find(checkLabelAttribute)?.value}`, }, ...processChildren(tabItem.children), ], }; } const checkLabelAttribute = (attr) => attr.type === "mdxJsxAttribute" && attr.name === "label"; function processChildren(children) { return children.map((child) => child.type === mdxElement ? replaceTabsTag(child) : child); } export default remarkReplaceTabs;