typedoc-plugin-markdown
Version:
A TypeDoc plugin that generates TypeScript API documentation as Markdown.
68 lines (67 loc) • 3.15 kB
JavaScript
import { ReflectionKind } from 'typedoc';
export function getFlattenedDeclarations(model, options) {
const getFlattenedDeclarations = (current) => {
return current.type?.declaration?.children?.reduce((acc, child) => {
child.originalName = child.name;
const childObj = copyDeclaration(child, {
name: `${current.name}.${child.name}`,
});
return parseDeclarations(childObj, acc);
}, []);
};
const parseDeclarations = (current, acc) => {
const shouldFlatten = current.type?.declaration?.children;
const isAccessor = current.kind === ReflectionKind.Accessor;
if (options?.includeSignatures) {
if (isAccessor) {
const accessors = [];
if (current.getSignature) {
accessors.push(copyDeclaration(current, {
getFullName: () => `${current.getFullName()}`,
getFriendlyFullName: () => `${current.getFriendlyFullName()}`,
name: `get ${current.name}`,
type: current.getSignature.type,
comment: current.getSignature?.comment,
}));
}
if (current.setSignature) {
accessors.push(copyDeclaration(current, {
getFullName: () => `${current.getFullName()}`,
getFriendlyFullName: () => `${current.getFriendlyFullName()}`,
name: `set ${current.name}`,
type: current.setSignature.type,
comment: current.setSignature?.comment,
}));
}
return [...acc, ...accessors];
}
if (current.signatures?.length) {
const signatures = current.signatures.map((signature) => {
return copyDeclaration(current, {
getFullName: () => `${current.getFullName()}`,
getFriendlyFullName: () => `${current.getFriendlyFullName()}`,
// Keep the (possibly prefixed) member name, and only this
// signature, so each overload renders as its own row.
name: current.name,
signatures: [signature],
type: signature.type,
comment: signature.comment,
});
});
return [...acc, ...signatures];
}
}
return shouldFlatten
? [...acc, current, ...getFlattenedDeclarations(current)]
: [...acc, current];
};
return model.reduce((acc, current) => parseDeclarations(current, acc), []);
}
/**
* Copies a declaration with some fields replaced, keeping its prototype so
* the copy still has the reflection's methods (e.g. `isDeprecated()`). A plain
* object spread drops them.
*/
function copyDeclaration(source, overrides) {
return Object.assign(Object.create(Object.getPrototypeOf(source)), source, overrides);
}