dumi
Version:
📖 Documentation Generator of React Component
188 lines (186 loc) • 7.37 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/loaders/markdown/transformer/rehypeSlug.ts
var rehypeSlug_exports = {};
__export(rehypeSlug_exports, {
HEADING_TAGS: () => HEADING_TAGS,
default: () => rehypeSlug,
stripCustomAnchorMarker: () => stripCustomAnchorMarker
});
module.exports = __toCommonJS(rehypeSlug_exports);
var import_utils = require("@umijs/utils");
var import_github_slugger = __toESM(require("github-slugger"));
var import_rehypeDemo = require("./rehypeDemo");
var visit;
var toString;
(async () => {
({ visit } = await import("unist-util-visit"));
({ toString } = await import("hast-util-to-string"));
})();
var HEADING_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6"];
var CUSTOM_ANCHOR_RE = /\s*\{#([^}]+)\}\s*$/;
function getCustomAnchorId(text) {
var _a;
return ((_a = text.match(CUSTOM_ANCHOR_RE)) == null ? void 0 : _a[1].trim()) || void 0;
}
function stripCustomAnchorMarker(text) {
return getCustomAnchorId(text) ? text.replace(CUSTOM_ANCHOR_RE, "") : text;
}
function extractCustomAnchor(node) {
const lastChild = node.children[node.children.length - 1];
if ((lastChild == null ? void 0 : lastChild.type) !== "text" || !lastChild.value.includes("{#"))
return;
const rawId = getCustomAnchorId(lastChild.value);
if (!rawId)
return;
lastChild.value = stripCustomAnchorMarker(lastChild.value);
if (!lastChild.value)
node.children.pop();
return rawId;
}
function rehypeSlug() {
return async (tree, vFile) => {
var _a;
const slugger = new import_github_slugger.default();
const customAnchors = /* @__PURE__ */ new Map();
const reservedIds = /* @__PURE__ */ new Set();
const demoIds = /* @__PURE__ */ new Set();
const filename = (_a = vFile.data.frontmatter) == null ? void 0 : _a.filename;
visit(tree, "element", (node) => {
var _a2, _b;
if (![import_rehypeDemo.DUMI_DEMO_TAG, import_rehypeDemo.DUMI_DEMO_GRID_TAG].includes(node.tagName) || !((_a2 = node.data) == null ? void 0 : _a2[import_rehypeDemo.DEMO_PROP_VALUE_KEY])) {
return;
}
const demos = [].concat(
(_b = node.data) == null ? void 0 : _b[import_rehypeDemo.DEMO_PROP_VALUE_KEY]
);
demos.forEach(({ demo }) => {
if (demo.inline)
return;
demoIds.add(demo.id);
slugger.slug(demo.id);
});
});
const existingIds = /* @__PURE__ */ new Set();
const markedHeadings = [];
visit(tree, "element", (node) => {
var _a2;
if (!HEADING_TAGS.includes(node.tagName))
return;
const existingId = (_a2 = node.properties) == null ? void 0 : _a2.id;
const rawId = extractCustomAnchor(node);
if (rawId) {
markedHeadings.push({ node, rawId, existingId });
} else if (existingId) {
existingIds.add(existingId);
reservedIds.add(existingId);
}
});
markedHeadings.forEach(({ node, rawId, existingId }) => {
let id = rawId;
let suffix = 1;
while (reservedIds.has(id) || demoIds.has(id)) {
id = `${rawId}-${suffix++}`;
}
if (id !== rawId) {
const reason = demoIds.has(rawId) ? "conflicts with demo id" : existingIds.has(rawId) ? "conflicts with an existing heading id" : "is duplicated";
import_utils.logger.warn(
`Custom anchor "${rawId}" ${reason}, fallback to "${id}" instead. at ${filename}`
);
}
if (existingId && existingId !== id) {
import_utils.logger.warn(
`Custom anchor "${id}" overrides the existing id "${existingId}" of heading "${toString(
node
)}". at ${filename}`
);
}
reservedIds.add(id);
customAnchors.set(node, id);
});
vFile.data.toc = [];
visit(tree, "element", (node) => {
var _a2, _b;
if (HEADING_TAGS.includes(node.tagName)) {
const customAnchor = customAnchors.get(node);
const title = toString({
type: "element",
tagName: node.tagName,
// discard text within Badge tag
children: node.children.filter(
(child) => child.type !== "element" || child.tagName !== "Badge"
)
}).trim();
const depth = Number(node.tagName.slice(1));
node.properties ?? (node.properties = {});
if (customAnchor) {
node.properties.id = customAnchor;
} else if (!node.properties.id) {
let id = slugger.slug(title);
const conflictedId = id;
while (reservedIds.has(id)) {
id = slugger.slug(title);
}
if (id !== conflictedId) {
import_utils.logger.warn(
`Heading "${title}" conflicts with the anchor "${conflictedId}", fallback to "${id}" instead. at ${filename}`
);
}
node.properties.id = id;
}
vFile.data.toc.push({
id: node.properties.id,
depth,
title
});
} else if ([import_rehypeDemo.DUMI_DEMO_TAG, import_rehypeDemo.DUMI_DEMO_GRID_TAG].includes(node.tagName) && ((_a2 = node.data) == null ? void 0 : _a2[import_rehypeDemo.DEMO_PROP_VALUE_KEY])) {
const demos = [].concat(
(_b = node.data) == null ? void 0 : _b[import_rehypeDemo.DEMO_PROP_VALUE_KEY]
);
demos.forEach(({ demo, previewerProps }) => {
var _a3, _b2;
if (!demo.inline && previewerProps.title) {
vFile.data.toc.push({
// must keep the same id as previewer rendered anchor
id: demo.id,
depth: ((_b2 = (_a3 = vFile.data.frontmatter) == null ? void 0 : _a3.demo) == null ? void 0 : _b2.tocDepth) || 3,
title: previewerProps.title,
// put debug flag to control hide/show in toc
...previewerProps.debug ? { _debug_demo: true } : {}
});
}
});
}
});
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
HEADING_TAGS,
stripCustomAnchorMarker
});