UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

42 lines (41 loc) 1.83 kB
import { walkElements } from "../util/element.js"; import { requireSlug } from "../util/string.js"; import { Extractor } from "./Extractor.js"; /** * Extractor that builds a `kind: "module"` `DocumentationElement` from a source file or directory. * - The module's `content`, `description`, and `title` are taken from the source element (`MergingExtractor` and * `IndexFileExtractor` are expected to have run upstream so `.md` siblings and `README.md` are already folded in). * - The module's `children` are every `tree-documentation` element found by deep-walking the source — flattened across * files and subdirectories, but never descending into a `tree-documentation`'s own members. */ export class ModuleExtractor extends Extractor { extract({ name, source }) { const children = _collectChildren(source); return { type: "tree-documentation", key: requireSlug(name), props: { name, title: name, kind: "module", description: source.props.description, content: source.props.content, children: children.length ? children : undefined, }, }; } } /** Collect every `tree-documentation` element reachable inside `element`, descending through directories and files but not through documented symbols. */ function _collectChildren(element) { const result = []; for (const child of walkElements(element.props.children)) { const treeChild = child; if (treeChild.type === "tree-documentation") { result.push(treeChild); } else if (treeChild.type === "tree-directory" || treeChild.type === "tree-file") { result.push(..._collectChildren(treeChild)); } } return result; }