fumadocs-core
Version:
The React.js library for building a documentation website
65 lines (63 loc) • 2.08 kB
JavaScript
import { n as dirname, r as extname, t as basename } from "../../path-DHIjrDBP.js";
//#region src/source/plugins/slugs.ts
/**
* Generate slugs for pages if missing
*/
function slugsPlugin(slugFn) {
function isIndex(file) {
return basename(file, extname(file)) === "index";
}
return {
name: "fumadocs:slugs",
transformStorage({ storage }) {
const indexFiles = [];
const taken = /* @__PURE__ */ new Set();
for (const path of storage.getFiles()) {
const file = storage.read(path);
if (!file || file.format !== "page" || file.slugs) continue;
const customSlugs = slugFn?.(file);
if (customSlugs === void 0 && isIndex(path)) {
indexFiles.push(path);
continue;
}
file.slugs = customSlugs ?? getSlugs(path);
const key = file.slugs.join("/");
if (taken.has(key)) throw new Error(`Duplicated slugs: ${key}`);
taken.add(key);
}
for (const path of indexFiles) {
const file = storage.read(path);
if (file?.format !== "page") continue;
file.slugs = getSlugs(path);
if (taken.has(file.slugs.join("/"))) file.slugs.push("index");
}
}
};
}
/**
* Generate slugs from file data (e.g. frontmatter).
*
* @param key - the property name in file data to generate slugs, default to `slug`.
*/
function slugsFromData(key = "slug") {
return (file) => {
const k = key;
if (k in file.data && typeof file.data[k] === "string") return file.data[k].split("/").filter((v) => v.length > 0);
};
}
const GroupRegex = /^\(.+\)$/;
/**
* Convert file path into slugs, also encode non-ASCII characters, so they can work in pathname
*/
function getSlugs(file) {
const dir = dirname(file);
const name = basename(file, extname(file));
const slugs = [];
for (const seg of dir.split("/")) if (seg.length > 0 && !GroupRegex.test(seg)) slugs.push(encodeURI(seg));
if (GroupRegex.test(name)) throw new Error(`Cannot use folder group in file names: ${file}`);
if (name !== "index") slugs.push(encodeURI(name));
return slugs;
}
//#endregion
export { getSlugs, slugsFromData, slugsPlugin };
//# sourceMappingURL=slugs.js.map