Skip to content

Commit b178bc1

Browse files
committed
docs: added custom typedoc plugins
1 parent 4b58417 commit b178bc1

File tree

5 files changed

+151
-12
lines changed

5 files changed

+151
-12
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ module.exports = {
1010
env: {
1111
node: true,
1212
},
13+
overrides: [
14+
{
15+
files: ["typedoc-plugins/**/*.js"],
16+
rules: {
17+
"@typescript-eslint/no-var-requires": "off",
18+
},
19+
},
20+
],
1321
};

package-lock.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint": "tsc --noEmit --pretty && eslint . --ignore-path .gitignore",
1616
"watch": "parcel watch",
1717
"build": "parcel build",
18-
"generate-docs": "typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-no-inherit --plugin typedoc-plugin-expand-object-like-types --plugin typedoc-plugin-include-example --out docs src/"
18+
"generate-docs": "typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-no-inherit --plugin ./typedoc-plugins/expand-object.js --plugin ./typedoc-plugins/include-example --out docs src/"
1919
},
2020
"files": [
2121
"dist/"
@@ -51,7 +51,6 @@
5151
"parcel": "^2.11.0",
5252
"prettier": "3.2.4",
5353
"typedoc": "^0.25.12",
54-
"typedoc-plugin-expand-object-like-types": "^0.1.2",
5554
"typedoc-plugin-include-example": "^1.2.0",
5655
"typedoc-plugin-markdown": "^3.17.1",
5756
"typedoc-plugin-no-inherit": "^1.4.0",

typedoc-plugins/expand-object.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const td = require("typedoc");
2+
3+
/** @param {td.Application} app */
4+
exports.load = function (app) {
5+
app.converter.on(
6+
td.Converter.EVENT_CREATE_DECLARATION,
7+
function (context, reflection) {
8+
expandObjectLikeTypes(context, reflection, app);
9+
},
10+
);
11+
};
12+
13+
/**
14+
* The reflection types affected by this plugin.
15+
*/
16+
const TYPES_TO_EXPAND = ["mapped", "intersection", "reference"];
17+
18+
/**
19+
* @param {td.Context} context
20+
* @param {td.DeclarationReflection} reflection
21+
* @param {td.Application} app
22+
*/
23+
function expandObjectLikeTypes(context, reflection, app) {
24+
const hasInternalModule =
25+
app.options.getDeclaration("internalModule") !== undefined;
26+
const internalModule = hasInternalModule
27+
? app.options.getValue("internalModule") || "<internal>"
28+
: "<internal>";
29+
30+
if (
31+
reflection.kind !== td.ReflectionKind.TypeAlias ||
32+
!reflection.type?.type ||
33+
!TYPES_TO_EXPAND.includes(reflection.type.type) ||
34+
reflection.parent.originalName === internalModule
35+
)
36+
return;
37+
38+
const symbol = context.project.getSymbolFromReflection(reflection);
39+
const declaration = symbol?.declarations?.[0];
40+
41+
if (!symbol || !declaration) return; // Will probably never happen in practice
42+
const reflectionType = context.checker.getDeclaredTypeOfSymbol(symbol);
43+
44+
const typeRefl = context
45+
.withScope(reflection)
46+
.createDeclarationReflection(
47+
td.ReflectionKind.TypeLiteral,
48+
undefined,
49+
undefined,
50+
"__type",
51+
);
52+
context.finalizeDeclarationReflection(typeRefl);
53+
const typeContext = context.withScope(typeRefl);
54+
55+
for (const propertySymbol of reflectionType.getProperties()) {
56+
const propertyType =
57+
propertySymbol &&
58+
context.checker.getTypeOfSymbolAtLocation(propertySymbol, declaration);
59+
const resolvedReflection = resolvePropertyReflection(
60+
context,
61+
reflectionType,
62+
propertySymbol,
63+
);
64+
65+
const element = typeContext.createDeclarationReflection(
66+
td.ReflectionKind.Property,
67+
undefined,
68+
undefined,
69+
propertySymbol.name,
70+
);
71+
72+
if (resolvedReflection) {
73+
element.comment = resolvedReflection.comment;
74+
element.flags = resolvedReflection.flags;
75+
element.sources = resolvedReflection.sources;
76+
77+
if (resolvedReflection instanceof td.DeclarationReflection) {
78+
element.defaultValue = resolvedReflection.defaultValue;
79+
}
80+
}
81+
82+
element.type = typeContext.converter.convertType(typeContext, propertyType);
83+
typeContext.finalizeDeclarationReflection(element);
84+
}
85+
86+
reflection.type = new td.ReflectionType(typeRefl);
87+
}
88+
89+
/**
90+
* @param {td.Context} context
91+
* @param {td.TypeScript.Type} objectType
92+
* @param {td.TypeScript.Symbol} propertySymbol
93+
*/
94+
function resolvePropertyReflection(context, objectType, propertySymbol) {
95+
const resolvedType = context.checker.getPropertyOfType(
96+
objectType,
97+
propertySymbol.name,
98+
);
99+
const resolvedDeclaration = resolvedType?.declarations?.[0];
100+
const resolvedSymbol =
101+
resolvedDeclaration && context.getSymbolAtLocation(resolvedDeclaration);
102+
103+
return (
104+
resolvedSymbol && context.project.getReflectionFromSymbol(resolvedSymbol)
105+
);
106+
}

typedoc-plugins/include-example.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { Converter } = require("typedoc");
2+
const {
3+
iterateProjectComments,
4+
} = require("../node_modules/typedoc-plugin-include-example/src/iterate-project-comments");
5+
const {
6+
findExample,
7+
} = require("../node_modules/typedoc-plugin-include-example/src/find-example");
8+
9+
function includeExample(comment, example) {
10+
comment.summary.push({
11+
kind: "code",
12+
text: `\n\n### Example\n\`\`\`ts\n${example}\`\`\``,
13+
});
14+
15+
comment.blockTags = comment.blockTags.filter(
16+
(tag) => tag.tag !== "@includeExample",
17+
);
18+
}
19+
20+
function processComments(context) {
21+
for (const comment of iterateProjectComments(context)) {
22+
const example = findExample(comment);
23+
24+
if (example !== null) {
25+
const exampleWithPackageImport = example.replace(
26+
/import {(.+)} from "\.\.\/\.\.\/src\/index"/g,
27+
'import {$1} from "clarifai-nodejs"',
28+
);
29+
includeExample(comment, exampleWithPackageImport);
30+
}
31+
}
32+
}
33+
34+
exports.load = function (application) {
35+
application.converter.on(Converter.EVENT_RESOLVE_BEGIN, processComments);
36+
};

0 commit comments

Comments
 (0)