@graphql-markdown/docusaurus
Version:
Docusaurus plugin for generating Markdown documentation from a GraphQL schema.
48 lines (47 loc) • 2.15 kB
JavaScript
;
/**
* @module mdx
* This module provides utilities for generating MDX index files in Docusaurus format.
*
* @packageDocumentation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.beforeGenerateIndexMetafileHook = void 0;
const node_path_1 = require("node:path");
const utils_1 = require("@graphql-markdown/utils");
const CATEGORY_YAML = "_category_.yml";
var SidebarPosition;
(function (SidebarPosition) {
SidebarPosition[SidebarPosition["FIRST"] = 1] = "FIRST";
SidebarPosition[SidebarPosition["LAST"] = 999] = "LAST";
})(SidebarPosition || (SidebarPosition = {}));
/**
* Hook that materializes a `_category_.yml` file before Docusaurus indexes
* a directory, ensuring generated bundles have labels, ordering, and
* optional generated-index metadata even when the folder was produced by the CLI.
*
* @param event - Hook payload containing the target directory, category name, and generator options.
*/
const beforeGenerateIndexMetafileHook = async (event) => {
const { dirPath, category, options } = event.data;
const filePath = (0, node_path_1.join)(dirPath, CATEGORY_YAML);
if (await (0, utils_1.fileExists)(filePath)) {
return;
}
const label = (0, utils_1.startCase)(category);
// Docusaurus 3.x uses the directory name as the id automatically
// No need to explicitly set id field in _category_.yml
const link = options?.index === true
? `\n type: generated-index\n title: '${label} overview'`
: "null";
const className = typeof options?.styleClass === "string"
? `className: ${options.styleClass}\n`
: "";
const position = typeof options?.sidebarPosition === "number"
? options.sidebarPosition
: SidebarPosition.FIRST;
const content = `label: ${label}\nposition: ${position}\n${className}link: ${link}\ncollapsible: ${Boolean(options?.collapsible ?? true)}\ncollapsed: ${Boolean(options?.collapsed ?? true)}\n`;
await (0, utils_1.ensureDir)(dirPath);
await (0, utils_1.saveFile)(filePath, content);
};
exports.beforeGenerateIndexMetafileHook = beforeGenerateIndexMetafileHook;