typedoc-better-json
Version:
Transforms typedoc's json output to a format that is better for creating custom documentation website
982 lines (961 loc) • 32.7 kB
JavaScript
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/getSignature/getTypeParamSignature.ts
function getTypeParamSignature(typeParameters) {
const tokens = [];
const collect = (typeInfo) => {
if (typeInfo == null ? void 0 : typeInfo.tokens) {
tokens.push(...typeInfo.tokens);
}
};
const generic = typeParameters ? `<${typeParameters.map((t) => {
collect(t.defaultType);
collect(t.extendsType);
const defaultVal = t.defaultType ? ` = ${t.defaultType.code}` : "";
return (t.extendsType ? `${t.name} extends ${t.extendsType.code}` : t.name) + defaultVal;
}).join(", ")}>` : "";
return {
code: generic,
tokens
};
}
// src/getSignature/getFunctionSignature.ts
function getFunctionSignature(name, signature) {
var _a, _b, _c, _d, _e, _f;
const tokens = [];
const collectTokens = (typeInfo) => {
var _a2;
(_a2 = typeInfo == null ? void 0 : typeInfo.tokens) == null ? void 0 : _a2.forEach((t) => tokens.push(t));
};
const returnType = (_c = (_b = (_a = signature.returns) == null ? void 0 : _a.type) == null ? void 0 : _b.code) != null ? _c : "void";
collectTokens((_d = signature.returns) == null ? void 0 : _d.type);
let paramList = "";
if (signature.parameters) {
const output = getParametersSignature(signature.parameters);
paramList = output.code;
(_e = output.tokens) == null ? void 0 : _e.forEach((t) => tokens.push(t));
}
let typeParams = "";
if (signature.typeParameters) {
const output = getTypeParamSignature(signature.typeParameters);
typeParams = output.code;
(_f = output.tokens) == null ? void 0 : _f.forEach((t) => tokens.push(t));
}
return {
code: `function ${name}${typeParams}(${paramList}) : ${returnType}`,
tokens
};
}
function getParametersSignature(parameters) {
const tokens = [];
const collectTokens = (typeInfo) => {
var _a;
(_a = typeInfo == null ? void 0 : typeInfo.tokens) == null ? void 0 : _a.forEach((t) => tokens.push(t));
};
const paramCode = parameters.map((param) => {
var _a, _b, _c;
const postfix = ((_a = param.flags) == null ? void 0 : _a.isOptional) ? "?" : "";
const prefix = ((_b = param.flags) == null ? void 0 : _b.isRest) ? "..." : "";
collectTokens(param.type);
return `${prefix}${param.name}${postfix}: ${(_c = param.type) == null ? void 0 : _c.code}`;
}).join(", ");
return {
code: paramCode,
tokens
};
}
// src/utils/getReadableType.ts
function getTypeInfo(typeObj) {
const tokens = [];
const collectTokens = (typeInfo) => {
if (typeInfo == null ? void 0 : typeInfo.tokens) {
tokens.push(...typeInfo.tokens);
}
};
function getCode() {
var _a;
switch (typeObj.type) {
case "intrinsic": {
return typeObj.name;
}
case "reflection": {
let inner = "";
if (typeObj.declaration.children) {
inner = `${(_a = typeObj.declaration.children) == null ? void 0 : _a.map((child) => {
const keyName = createValidKey(child.name);
if (child.type) {
const typeInfo = getTypeInfo(child.type);
collectTokens(typeInfo);
const postfix = child.flags.isOptional ? "?" : "";
let prefix = "";
if (child.flags.isReadonly) {
prefix = "readonly ";
} else if (child.flags.isRest) {
prefix = "...";
} else if (child.flags.isAbstract) {
prefix = "abstract ";
} else if (child.flags.isPrivate) {
prefix = "private ";
} else if (child.flags.isStatic) {
prefix = "static ";
} else if (child.flags.isProtected) {
prefix = "protected ";
}
return `${prefix}${keyName}${postfix}: ${typeInfo.code}`;
}
if (child.signatures) {
const typeInfo = child.signatures.map(
(sig) => getFunctionSignatureTypeInfo(
sig,
child.signatures.length > 1
)
);
typeInfo.forEach(collectTokens);
let sigCode = typeInfo.map((s) => s.code).join("; ");
if (child.signatures.length > 1) {
sigCode = `{ ${sigCode} }`;
}
return `${keyName}: ${sigCode} `;
}
return "";
}).join("; ")}`;
}
if (typeObj.declaration.signatures) {
const isMuli = typeObj.declaration.signatures.length > 1 || inner.length > 0;
const typeInfo = typeObj.declaration.signatures.map(
(sig) => getFunctionSignatureTypeInfo(sig, isMuli)
);
typeInfo.forEach(collectTokens);
const sigCode = typeInfo.map((s) => s.code).join("; ");
if (!inner) {
if (typeObj.declaration.signatures.length === 1) {
return sigCode;
}
return `{ ${sigCode} }`;
}
inner = `${sigCode} ; ${inner}`;
}
return `{ ${inner} }`;
}
case "reference": {
tokens.push({
name: typeObj.name,
package: typeObj.package
});
if (typeObj.typeArguments) {
const typeInfos = typeObj.typeArguments.map(getTypeInfo);
typeInfos.forEach(collectTokens);
return `${typeObj.name}<${typeInfos.map((t) => t.code).join(", ")}>`;
}
return typeObj.name;
}
case "union": {
return typeObj.types.map((t) => {
const typeInfo = getTypeInfo(t);
collectTokens(typeInfo);
if (t.type === "literal" || t.type === "intrinsic") {
return typeInfo.code;
}
return `(${typeInfo.code})`;
}).join(" | ");
}
case "literal": {
if (typeof typeObj.value === "string") {
return `"${typeObj.value}"`;
}
return typeObj.value + "";
}
case "array": {
const typeInfo = getTypeInfo(typeObj.elementType);
collectTokens(typeInfo);
return `Array<${typeInfo.code}>`;
}
case "conditional": {
const checktypeInfo = getTypeInfo(typeObj.checkType);
const extendstypeInfo = getTypeInfo(typeObj.extendsType);
const truetypeInfo = getTypeInfo(typeObj.trueType);
const falsetypeInfo = getTypeInfo(typeObj.falseType);
collectTokens(checktypeInfo);
collectTokens(extendstypeInfo);
collectTokens(truetypeInfo);
collectTokens(falsetypeInfo);
return `${checktypeInfo.code} extends ${extendstypeInfo.code} ? ${truetypeInfo.code} : ${falsetypeInfo.code}`;
}
case "indexedAccess": {
const ObjtypeInfo = getTypeInfo(typeObj.objectType);
const ValuetypeInfo = getTypeInfo(typeObj.indexType);
collectTokens(ObjtypeInfo);
collectTokens(ValuetypeInfo);
return `${ObjtypeInfo.code}[${ValuetypeInfo.code}]`;
}
case "intersection": {
return typeObj.types.map((t) => {
const typeInfo = getTypeInfo(t);
collectTokens(typeInfo);
if (t.type === "literal" || t.type === "intrinsic") {
return typeInfo.code;
}
return `(${typeInfo.code})`;
}).join(" & ");
}
case "mapped": {
const typeInfo = getTypeInfo(typeObj.parameterType);
const templatetypeInfo = getTypeInfo(typeObj.templateType);
collectTokens(typeInfo);
collectTokens(templatetypeInfo);
return `{[${typeObj.parameter} in ${typeInfo.code}] : ${templatetypeInfo.code}}`;
}
case "tuple": {
if (typeObj.elements) {
return `[${typeObj.elements.map((el) => {
const typeInfo = getTypeInfo(el);
collectTokens(typeInfo);
return typeInfo.code;
}).join(", ")}]`;
}
return `[]`;
}
case "query": {
const typeInfo = getTypeInfo(typeObj.queryType);
collectTokens(typeInfo);
return `typeof ${typeInfo.code}`;
}
case "typeOperator": {
const typeInfo = getTypeInfo(typeObj.target);
collectTokens(typeInfo);
return `${typeObj.operator} ${typeInfo.code}`;
}
case "templateLiteral": {
return "`" + typeObj.head + typeObj.tail.map((t) => {
const typeInfo = getTypeInfo(t[0]);
collectTokens(typeInfo);
return `\${${typeInfo.code}}` + t[1];
}).join("") + "`";
}
case "inferred": {
tokens.push({
name: typeObj.name
});
return `infer ${typeObj.name}`;
}
case "rest": {
const typeInfo = getTypeInfo(typeObj.elementType);
collectTokens(typeInfo);
return `...(${typeInfo.code})`;
}
case "unknown": {
return typeObj.name;
}
case "predicate": {
if (typeObj.targetType) {
const typeInfo = getTypeInfo(typeObj.targetType);
collectTokens(typeInfo);
return `${typeObj.name} is (${typeInfo.code})`;
}
throw new Error("Failed to get readable type of type 'predicate' ");
}
case "namedTupleMember": {
const typeInfo = getTypeInfo(typeObj.element);
collectTokens(typeInfo);
return `${typeObj.name}: ${typeInfo.code}`;
}
case "optional": {
const typeInfo = getTypeInfo(typeObj.elementType);
collectTokens(typeInfo);
return `${typeInfo.code}?`;
}
default: {
throw new Error("Failed to create a readable type for type");
}
}
}
return {
code: getCode(),
tokens
};
}
function getFunctionSignatureTypeInfo(signature, hasMultipleSig) {
var _a, _b;
const tokens = [];
let parametersCode = "";
if (signature.parameters) {
const functionParams = getFunctionParametersDoc(signature.parameters);
const paramTypeInfo = getParametersSignature(functionParams);
parametersCode = paramTypeInfo.code;
(_a = paramTypeInfo.tokens) == null ? void 0 : _a.forEach((t) => tokens.push(t));
}
const returntypeInfo = signature.type ? getTypeInfo(signature.type) : void 0;
(_b = returntypeInfo == null ? void 0 : returntypeInfo.tokens) == null ? void 0 : _b.forEach((r) => tokens.push(r));
const returnType = returntypeInfo ? `${hasMultipleSig ? ":" : "=>"} ${returntypeInfo.code}` : "";
return {
code: `(${parametersCode}) ${returnType}`,
tokens
};
}
function createValidKey(str) {
if (str.includes(" ") || str.includes("-")) {
return `"${str}"`;
}
return str;
}
// src/utils/markdown.ts
import { fromMarkdown } from "mdast-util-from-markdown";
function simplifyNode(node) {
delete node.position;
if ("children" in node) {
node.children.forEach(simplifyNode);
}
return node;
}
function parseMarkdown(markdown) {
const tree = fromMarkdown(markdown, {});
tree.children.forEach(simplifyNode);
return tree.children;
}
// src/nodes/summary.ts
function getSummaryDoc(summary) {
if (!summary)
return void 0;
const summaryJoin = summary.map((s) => {
if (s.kind === "inline-tag") {
if (s.tag === "@link") {
const target = s.target;
if (target) {
if (typeof target === "string") {
return `[${s.text}](${target})`;
}
}
}
}
return s.text;
}).join("");
return parseMarkdown(summaryJoin);
}
// src/nodes/blockTag.ts
function getBlockTag(blockTag) {
return {
tag: blockTag.tag,
name: blockTag.name,
summary: getSummaryDoc(blockTag.content)
};
}
// src/nodes/function.ts
function getFunctionDoc(data) {
var _a, _b, _c;
return {
kind: "function",
name: data.name,
signatures: (_a = data.signatures) == null ? void 0 : _a.map(getFunctionSignatureDoc),
source: (_c = (_b = data.sources) == null ? void 0 : _b[0]) == null ? void 0 : _c.url,
flags: Object.keys(data.flags).length > 0 ? data.flags : void 0
};
}
function getFunctionSignatureDoc(signature) {
var _a, _b, _c, _d, _e, _f, _g;
const output = {
flags: Object.keys(signature.flags).length > 0 ? signature.flags : void 0,
summary: getSummaryDoc((_a = signature.comment) == null ? void 0 : _a.summary),
parameters: signature.parameters ? getFunctionParametersDoc(signature.parameters) : void 0,
returns: {
type: signature.type ? getTypeInfo(signature.type) : void 0,
summary: getSummaryDoc(
(_d = (_c = (_b = signature.comment) == null ? void 0 : _b.blockTags) == null ? void 0 : _c.find((tag) => tag.tag === "@returns")) == null ? void 0 : _d.content
)
},
typeParameters: (_e = signature.typeParameter) == null ? void 0 : _e.map((param) => {
const typeParam = {
name: param.name,
extendsType: param.type ? getTypeInfo(param.type) : void 0,
defaultType: param.default ? getTypeInfo(param.default) : void 0
};
return typeParam;
}),
inheritedFrom: signature.inheritedFrom ? {
name: signature.inheritedFrom.name
} : void 0,
blockTags: (_g = (_f = signature.comment) == null ? void 0 : _f.blockTags) == null ? void 0 : _g.filter((w) => w.tag !== "@returns").map(getBlockTag)
};
return output;
}
function getFunctionParametersDoc(parameters) {
if (parameters.length === 1 && parameters[0] && parameters[0].flags.isRest) {
const type = parameters[0] && parameters[0].type;
if ((type == null ? void 0 : type.type) === "tuple" && type.elements) {
const output = [];
type.elements.forEach((member) => {
if (member.type === "namedTupleMember") {
const typeInfo = getTypeInfo(member.element);
output.push({
name: member.name,
type: typeInfo
});
}
});
return output;
}
}
return parameters.map((param) => {
var _a, _b, _c;
const arg = {
name: param.name,
type: param.type ? getTypeInfo(param.type) : void 0,
summary: getSummaryDoc((_a = param.comment) == null ? void 0 : _a.summary),
flags: Object.keys(param.flags).length > 0 ? param.flags : void 0,
blockTags: (_c = (_b = param.comment) == null ? void 0 : _b.blockTags) == null ? void 0 : _c.map(getBlockTag)
};
return arg;
});
}
// src/utils/isComponentType.ts
function isComponentType(data) {
var _a;
return data.signatures && data.signatures[0] && ((_a = data.signatures[0].type) == null ? void 0 : _a.type) === "reference" && data.signatures[0].type.name && isComponentName(data.signatures[0].type.name) && (data.signatures[0].type.name === "ReactNode" || data.signatures[0].type.name === "Element");
}
function isComponentName(str) {
const firstChar = str[0];
if (firstChar && firstChar === firstChar.toUpperCase()) {
return true;
}
return false;
}
// src/nodes/interface.ts
function getInterfaceDoc(data) {
var _a, _b, _c, _d, _e, _f, _g;
function getType() {
if (data.type) {
return getTypeInfo(data.type);
}
if (data.children) {
const tokens = [];
const code = `{${data.children.map((child) => {
var _a2;
if (child.type) {
const typeInfo = getTypeInfo(child.type);
(_a2 = typeInfo.tokens) == null ? void 0 : _a2.forEach((r) => tokens.push(r));
return `${child.name} : ${typeInfo.code}`;
}
if (child.signatures) {
const isMulti = child.signatures.length > 1;
let sigCode = child.signatures.map((sig) => {
var _a3;
const fRef = getFunctionSignatureTypeInfo(sig, isMulti);
(_a3 = fRef.tokens) == null ? void 0 : _a3.forEach((r) => tokens.push(r));
return fRef.code;
}).join(" ; ");
if (isMulti) {
sigCode = `{ ${sigCode} }`;
}
return `${child.name} : ${sigCode}`;
}
throw new Error(`Unknown type declaration node ${child.name}`);
})}}`;
return {
code
};
}
}
return {
kind: "type",
name: data.name,
summary: getSummaryDoc((_a = data.comment) == null ? void 0 : _a.summary),
blockTags: (_c = (_b = data.comment) == null ? void 0 : _b.blockTags) == null ? void 0 : _c.map(getBlockTag),
source: (_e = (_d = data.sources) == null ? void 0 : _d[0]) == null ? void 0 : _e.url,
typeParameters: (_f = data.typeParameters) == null ? void 0 : _f.map((param) => {
const typeParam = {
name: param.name,
extendsType: param.type ? getTypeInfo(param.type) : void 0,
defaultType: param.default ? getTypeInfo(param.default) : void 0
};
return typeParam;
}),
extends: (_g = data.extendedTypes) == null ? void 0 : _g.map((t) => getTypeInfo(t)),
type: getType(),
typeDeclaration: getDeclaration(data)
};
}
function getDeclaration(data) {
const typeObj = data.type;
const children = data.children || (typeObj && "declaration" in typeObj ? typeObj.declaration.children : void 0);
if (children) {
return children.map((child) => {
var _a, _b, _c, _d;
if (child.signatures) {
return getFunctionDoc(child);
}
if (((_a = child.type) == null ? void 0 : _a.type) === "reflection" && child.type.declaration.signatures) {
const output = getFunctionDoc(
child.type.declaration
);
output.name = child.name;
return output;
}
if (child.type) {
const output = {
kind: "subtype",
name: child.name,
type: getTypeInfo(child.type),
summary: getSummaryDoc((_b = child.comment) == null ? void 0 : _b.summary),
blockTags: (_d = (_c = child.comment) == null ? void 0 : _c.blockTags) == null ? void 0 : _d.map(getBlockTag)
};
return output;
}
throw new Error(`Unknown type declaration node ${child.name}`);
});
}
}
// src/nodes/enum.ts
function getEnumDoc(data) {
var _a, _b, _c, _d, _e;
return {
kind: "enum",
name: data.name,
summary: getSummaryDoc((_a = data.comment) == null ? void 0 : _a.summary),
source: (_c = (_b = data.sources) == null ? void 0 : _b[0]) == null ? void 0 : _c.url,
members: getMembers(data),
blockTags: (_e = (_d = data.comment) == null ? void 0 : _d.blockTags) == null ? void 0 : _e.map(getBlockTag)
};
}
function getMembers(data) {
if (!data.children) {
throw new Error(`Failed to get members for enum ${data.name}`);
}
const output = data.children.map((child) => {
var _a, _b, _c;
if (!child.type) {
throw new Error(`No type found for enum member ${child.name}`);
}
return {
name: child.name,
value: getTypeInfo(child.type),
summary: getSummaryDoc((_a = child.comment) == null ? void 0 : _a.summary),
blockTags: (_c = (_b = child.comment) == null ? void 0 : _b.blockTags) == null ? void 0 : _c.map(getBlockTag)
};
});
return output;
}
// src/nodes/variable.ts
function getVariableDoc(data) {
var _a, _b, _c;
return {
kind: "variable",
name: data.name,
summary: getSummaryDoc((_a = data.comment) == null ? void 0 : _a.summary),
source: (_c = (_b = data.sources) == null ? void 0 : _b[0]) == null ? void 0 : _c.url,
type: data.type ? getTypeInfo(data.type) : void 0,
typeDeclaration: data.type ? getDeclaration2(data.type, data.name) : void 0,
flags: Object.keys(data.flags).length > 0 ? data.flags : void 0
};
}
function getDeclaration2(typeObj, varName) {
var _a;
if (typeObj.type === "reflection") {
const mainOutput = [];
if (typeObj.declaration.signatures) {
const fnDoc = getFunctionDoc(typeObj.declaration);
if (varName) {
fnDoc.name = varName;
}
mainOutput.push(fnDoc);
}
if (typeObj.declaration.children) {
const output = (_a = typeObj.declaration.children) == null ? void 0 : _a.map((child) => {
var _a2, _b, _c, _d;
if (child.signatures) {
const output2 = getFunctionDoc(child);
return output2;
}
if (((_a2 = child.type) == null ? void 0 : _a2.type) === "reflection" && child.type.declaration.signatures) {
const output2 = getFunctionDoc(
child.type.declaration
);
output2.name = child.name;
return output2;
}
if (child.type) {
const output2 = {
kind: "subtype",
name: child.name,
type: getTypeInfo(child.type),
summary: getSummaryDoc((_b = child.comment) == null ? void 0 : _b.summary),
blockTags: (_d = (_c = child.comment) == null ? void 0 : _c.blockTags) == null ? void 0 : _d.map(getBlockTag)
};
return output2;
}
throw new Error(`Unknown type declaration node ${child.name}`);
});
mainOutput.push(...output);
}
if (mainOutput.length !== 0) {
return mainOutput;
}
}
if (typeObj.type === "array") {
return getDeclaration2(typeObj.elementType);
}
}
// src/nodes/accessor.ts
function getAccessorDoc(data) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const sig = data.getSignature;
return {
kind: "accessor",
name: data.name,
source: (_b = (_a = sig == null ? void 0 : sig.sources) == null ? void 0 : _a[0]) == null ? void 0 : _b.url,
summary: sig ? getSummaryDoc((_c = sig.comment) == null ? void 0 : _c.summary) : void 0,
returns: sig ? {
type: sig.type ? getTypeInfo(sig.type) : void 0,
summary: getSummaryDoc(
(_f = (_e = (_d = sig.comment) == null ? void 0 : _d.blockTags) == null ? void 0 : _e.find((tag) => tag.tag === "@returns")) == null ? void 0 : _f.content
)
} : void 0,
blockTags: (_h = (_g = sig == null ? void 0 : sig.comment) == null ? void 0 : _g.blockTags) == null ? void 0 : _h.filter((w) => w.tag !== "@returns").map(getBlockTag),
flags: sig ? Object.keys(sig.flags).length > 0 ? sig.flags : void 0 : void 0
};
}
// src/nodes/class.ts
var groupMappings = {
Constructors: "constructors",
Properties: "properties",
Methods: "methods",
Accessors: "accessors"
};
function getClassDoc(data) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
const methods = [];
const properties = [];
const constructors = [];
const accessors = [];
const childrenMap = {};
(_a = data.children) == null ? void 0 : _a.forEach((child) => {
childrenMap[child.id] = child;
});
(_b = data.groups) == null ? void 0 : _b.forEach((group) => {
var _a2;
const title = group.title;
if (group.title in groupMappings) {
(_a2 = group.children) == null ? void 0 : _a2.forEach((childId) => {
const childData = childrenMap[childId];
if (!childData) {
throw new Error(`Failed to resolve child id ${childId}`);
}
switch (groupMappings[title]) {
case "methods": {
methods.push(getFunctionDoc(childData));
break;
}
case "properties": {
properties.push(getVariableDoc(childData));
break;
}
case "constructors": {
constructors.push(getFunctionDoc(childData));
break;
}
case "accessors": {
accessors.push(getAccessorDoc(childData));
break;
}
}
});
} else {
throw new Error(`Unknown group in class ${group.title}`);
}
});
if (constructors.length > 1) {
throw new Error(`Found more than 1 constructors on ${data.name} Class`);
}
const output = {
kind: "class",
name: data.name,
source: (_d = (_c = data.sources) == null ? void 0 : _c[0]) == null ? void 0 : _d.url,
summary: getSummaryDoc((_e = data.comment) == null ? void 0 : _e.summary),
blockTags: (_g = (_f = data.comment) == null ? void 0 : _f.blockTags) == null ? void 0 : _g.map(getBlockTag),
constructor: constructors[0],
methods: methods.length > 0 ? methods : void 0,
properties: properties.length > 0 ? properties : void 0,
accessors: accessors.length > 0 ? accessors : void 0,
typeParameters: (_h = data.typeParameters) == null ? void 0 : _h.map((param) => {
const typeParam = {
name: param.name,
extendsType: param.type ? getTypeInfo(param.type) : void 0,
defaultType: param.default ? getTypeInfo(param.default) : void 0
};
return typeParam;
}),
implements: (_i = data.implementedTypes) == null ? void 0 : _i.map((t) => getTypeInfo(t)),
extends: (_j = data.extendedTypes) == null ? void 0 : _j.map((t) => getTypeInfo(t))
};
return output;
}
// package.json
var package_default = {
name: "typedoc-better-json",
version: "0.9.4",
description: "Transforms typedoc's json output to a format that is better for creating custom documentation website",
main: "dist/index.mjs",
module: "dist/index.mjs",
types: "dist/index.d.ts",
repository: "https://github.com/MananTank/typedoc-better-json",
scripts: {
prebuild: "yarn lint && yarn typecheck && yarn test",
build: "yarn create-build",
dev: "yarn create-build --watch",
lint: "eslint src/",
typecheck: "tsc",
prettier: "prettier . --write",
"create-build": "tsup src/index.ts --format esm --dts",
test: "vitest run",
debug: "yarn create-build && node debug/test.mjs",
convert: "yarn create-build && node convert/convert.mjs"
},
keywords: [
"typedoc",
"typescript",
"docs",
"autogenerate",
"json"
],
author: "Manan Tank",
license: "MIT",
dependencies: {
mdast: "^3.0.0",
"mdast-util-from-markdown": "^2.0.0",
typedoc: "^0.25.2"
},
devDependencies: {
"@changesets/cli": "^2.27.10",
"@types/node": "^20.8.8",
"@types/react": "^18.2.41",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
eslint: "^8.52.0",
prettier: "^3.0.3",
react: "^18.2.0",
"ts-node": "^10.9.1",
tsup: "^6.7.0",
typescript: "^5.2.2",
vitest: "^0.34.6"
}
};
// src/utils/isHook.ts
function isHook(name) {
if (!name.startsWith("use")) {
return false;
}
const fourthChar = name.charAt(3);
return fourthChar === fourthChar.toUpperCase();
}
// src/transform.ts
var groupNameMap = {
Interfaces: "types",
"Type Aliases": "types",
Variables: "variables",
Functions: "functions",
Classes: "classes",
Enumerations: "enums"
};
function transform(inputData) {
var _a, _b;
const functions = [];
const hooks = [];
const components = [];
const types = [];
const variables = [];
const enums = [];
const classes = [];
const childrenMap = {};
function createChildrenMap(children) {
children.forEach((child) => {
childrenMap[child.id] = child;
});
}
function collectChildren(group) {
var _a2;
const mappedTitle = groupNameMap[group.title];
(_a2 = group.children) == null ? void 0 : _a2.map((childId) => __async(this, null, function* () {
const childData = childrenMap[childId];
if (!childData) {
throw new Error(`Failed to resolve child id ${childId}`);
}
switch (mappedTitle) {
case "functions": {
if (isHook(childData.name)) {
hooks.push(getFunctionDoc(childData));
} else if (isComponentType(childData)) {
components.push(getFunctionDoc(childData));
} else {
functions.push(getFunctionDoc(childData));
}
break;
}
case "types": {
types.push(getInterfaceDoc(childData));
break;
}
case "variables": {
variables.push(getVariableDoc(childData));
break;
}
case "classes": {
classes.push(getClassDoc(childData));
break;
}
case "enums": {
enums.push(getEnumDoc(childData));
}
}
}));
}
if (inputData.groups && inputData.groups.length === 1 && inputData.groups[0] && inputData.groups[0].title === "Modules") {
const modules = (_a = inputData.groups[0].children) == null ? void 0 : _a.map((moduleId) => {
var _a2;
const moduleDoc = (_a2 = inputData.children) == null ? void 0 : _a2.find(
(child) => child.id === moduleId
);
if (!moduleDoc) {
throw new Error(`Failed to resolve module id ${moduleId}`);
}
if (moduleDoc.children) {
createChildrenMap(moduleDoc.children);
}
return moduleDoc;
});
modules == null ? void 0 : modules.forEach((moduleDoc) => {
var _a2;
(_a2 = moduleDoc == null ? void 0 : moduleDoc.groups) == null ? void 0 : _a2.forEach((group) => {
collectChildren(group);
});
});
} else {
if (inputData.children) {
createChildrenMap(inputData.children);
}
(_b = inputData.groups) == null ? void 0 : _b.forEach((group) => {
collectChildren(group);
});
}
const output = {
meta: {
typedocBetterJsonVersion: package_default.version
},
functions: functions.length > 0 ? functions : void 0,
hooks: hooks.length > 0 ? hooks : void 0,
variables: variables.length > 0 ? variables : void 0,
types: types.length > 0 ? types : void 0,
components: components.length > 0 ? components : void 0,
enums: enums.length > 0 ? enums : void 0,
classes: classes.length > 0 ? classes : void 0
};
return output;
}
// src/getSignature/getInterfaceSignature.ts
function getInterfaceSignature(doc) {
if (!doc.type)
return {
code: doc.name
};
let code;
const tokens = [];
const collectTokens = (typeInfo) => {
if (typeInfo == null ? void 0 : typeInfo.tokens) {
tokens.push(...typeInfo.tokens);
}
};
const generic = doc.typeParameters ? `<${doc.typeParameters.map((t) => {
collectTokens(t.extendsType);
collectTokens(t.defaultType);
const defaultVal = t.defaultType ? ` = ${t.defaultType.code}` : "";
return (t.extendsType ? `${t.name} extends ${t.extendsType.code}` : t.name) + defaultVal;
}).join(", ")}>` : "";
if (doc.extends) {
const extendsClause = doc.extends ? `extends ${doc.extends.map((ext) => {
collectTokens(ext);
return ext.code;
}).join(", ")}` : "";
code = `interface ${doc.name}${generic} ${extendsClause} ${doc.type.code}`;
} else {
code = `type ${doc.name}${generic} = ${doc.type.code}`;
}
collectTokens(doc.type);
return {
code,
tokens
};
}
// src/getSignature/getClassSignature.ts
function getClassSignature(classDoc) {
var _a, _b;
const tokens = [];
const collect = (typeInfo) => {
if (typeInfo == null ? void 0 : typeInfo.tokens) {
tokens.push(...typeInfo.tokens);
}
};
let generic = "";
if (classDoc.typeParameters) {
const typeParams = getTypeParamSignature(classDoc.typeParameters);
generic = typeParams.code;
collect(typeParams);
}
const implmentsStr = classDoc.implements ? `implements ${(_a = classDoc.implements.map((i) => {
collect(i);
return i.code;
})) == null ? void 0 : _a.join(", ")}` : "";
const extendsStr = classDoc.extends ? `extends ${(_b = classDoc.extends) == null ? void 0 : _b.map((ex) => {
collect(ex);
return ex.code;
}).join(", ")}` : "";
const code = `class ${classDoc.name}${generic} ${[
extendsStr,
implmentsStr
].join(" ")} {}`;
return {
code,
tokens
};
}
// src/getSignature/getEnumSignature.ts
function getEnumSignature(doc) {
var _a;
return {
code: `enum ${doc.name} {
${(_a = doc.members) == null ? void 0 : _a.map((member) => member.name).join(",\n")}}`,
tokens: []
};
}
// src/getSignature/getAccessorSignature.ts
function getAccessorSignature(doc) {
var _a, _b, _c, _d;
return {
code: `let ${doc.name}: ${((_b = (_a = doc.returns) == null ? void 0 : _a.type) == null ? void 0 : _b.code) || "void"}`,
tokens: (_d = (_c = doc.returns) == null ? void 0 : _c.type) == null ? void 0 : _d.tokens
};
}
export {
getAccessorSignature,
getClassSignature,
getEnumSignature,
getFunctionSignature,
getInterfaceSignature,
getParametersSignature,
getTypeParamSignature,
transform
};