fumadocs-core
Version:
The React.js library for building a documentation website
31 lines (30 loc) • 1.05 kB
JavaScript
import Slugger from "github-slugger";
// src/mdx-plugins/remark-heading.runtime.ts
function createSlugger() {
const slugger = new Slugger();
return (text) => slugger.slug(text);
}
/**
* The IDs of the headings of a page for its props.
*
* @param ids - list the ID of each heading in document order: `slug` takes the parts of the text of a heading that
* shows, or its custom ID, a hidden heading has an empty ID
* @param slugger - create the slug function of a page, called with the text or custom ID of each heading that shows in
* document order, so it can count duplicates
*/
function createHeadingIds(ids, slugger = createSlugger) {
const cache = /* @__PURE__ */ new WeakMap();
return (props) => {
let list = cache.get(props);
if (list) return list;
const slug = slugger();
list = ids(props, (parts) => {
let text = "";
for (const part of parts) if (typeof part === "string" || typeof part === "number") text += part;
return slug(text.trim());
});
cache.set(props, list);
return list;
};
}
export { createHeadingIds };