@textmode/docs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -0
- package/index.d.ts +37 -0
- package/package.json +60 -0
- package/src/cli/textmode-docs.mjs +198 -0
- package/src/index.js +23 -0
- package/src/internal/config.js +380 -0
- package/src/internal/lib/api-doc-checks.mjs +147 -0
- package/src/internal/lib/api-doc-routes.mjs +93 -0
- package/src/internal/lib/example-checks.mjs +405 -0
- package/src/internal/lib/jsdoc-comments.mjs +78 -0
- package/src/internal/lib/reflection-policy.mjs +113 -0
- package/src/internal/lib/reporting.mjs +23 -0
- package/src/internal/operations.js +271 -0
- package/src/internal/reflection-kinds.js +21 -0
- package/src/internal/typedoc-plugins/accessor-docs-normalizer/README.md +45 -0
- package/src/internal/typedoc-plugins/accessor-docs-normalizer/accessor-comments.js +53 -0
- package/src/internal/typedoc-plugins/accessor-docs-normalizer/index.js +32 -0
- package/src/internal/typedoc-plugins/accessor-docs-normalizer/render-accessor.js +144 -0
- package/src/internal/typedoc-plugins/accessor-docs-normalizer/theme-patch.js +28 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/README.md +57 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/child-pages.js +65 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/constants.js +58 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/index.js +18 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/member-paths.js +48 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/member-reflections.js +164 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/namespace-paths.js +44 -0
- package/src/internal/typedoc-plugins/all-member-pages-router/router.js +58 -0
- package/src/internal/typedoc-plugins/api-frontmatter/README.md +66 -0
- package/src/internal/typedoc-plugins/api-frontmatter/constants.js +52 -0
- package/src/internal/typedoc-plugins/api-frontmatter/descriptions.js +87 -0
- package/src/internal/typedoc-plugins/api-frontmatter/frontmatter.js +56 -0
- package/src/internal/typedoc-plugins/api-frontmatter/index.js +39 -0
- package/src/internal/typedoc-plugins/api-frontmatter/library-context.js +38 -0
- package/src/internal/typedoc-plugins/api-frontmatter/reflection-metadata.js +95 -0
- package/src/internal/typedoc-plugins/include-code-examples/README.md +40 -0
- package/src/internal/typedoc-plugins/include-code-examples/code-fences.js +55 -0
- package/src/internal/typedoc-plugins/include-code-examples/example-metadata.js +41 -0
- package/src/internal/typedoc-plugins/include-code-examples/index.js +23 -0
- package/src/internal/typedoc-plugins/include-code-examples/transform-markdown.js +33 -0
- package/src/internal/typedoc-plugins/strip-api-self-links/index.js +117 -0
- package/src/internal/typedoc-runner.js +138 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const CONFIG_MARKER = Symbol.for('@textmode/docs/config');
|
|
6
|
+
const TOP_LEVEL_KEYS = new Set([
|
|
7
|
+
'entryPoints',
|
|
8
|
+
'linkEntryPoints',
|
|
9
|
+
'sourceRoots',
|
|
10
|
+
'outputDir',
|
|
11
|
+
'apiBaseUrl',
|
|
12
|
+
'tsconfig',
|
|
13
|
+
'memberPageKinds',
|
|
14
|
+
'stripApiSelfLinks',
|
|
15
|
+
'checks',
|
|
16
|
+
'examples',
|
|
17
|
+
'typedoc',
|
|
18
|
+
]);
|
|
19
|
+
const CHECK_KEYS = new Set(['apiLinks', 'docstrings', 'examples']);
|
|
20
|
+
const EXAMPLE_KEYS = new Set(['checkSketches', 'forbidGallerySourceBase']);
|
|
21
|
+
const LINK_ENTRY_POINT_KEYS = new Set(['path', 'tsconfig', 'namespaceExportsOnly']);
|
|
22
|
+
const REFLECTION_KINDS = new Set([
|
|
23
|
+
'class',
|
|
24
|
+
'interface',
|
|
25
|
+
'typeAlias',
|
|
26
|
+
'enum',
|
|
27
|
+
'variable',
|
|
28
|
+
'function',
|
|
29
|
+
'method',
|
|
30
|
+
'accessor',
|
|
31
|
+
'property',
|
|
32
|
+
]);
|
|
33
|
+
const MEMBER_PAGE_KINDS = new Set(['method', 'property', 'accessor']);
|
|
34
|
+
const PROTECTED_TYPEDOC_KEYS = new Set([
|
|
35
|
+
'entryPoints',
|
|
36
|
+
'out',
|
|
37
|
+
'tsconfig',
|
|
38
|
+
'plugin',
|
|
39
|
+
'router',
|
|
40
|
+
'emit',
|
|
41
|
+
'watch',
|
|
42
|
+
'treatWarningsAsErrors',
|
|
43
|
+
'treatValidationWarningsAsErrors',
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
export const DEFAULT_API_LINK_KINDS = [
|
|
47
|
+
'class',
|
|
48
|
+
'interface',
|
|
49
|
+
'typeAlias',
|
|
50
|
+
'enum',
|
|
51
|
+
'variable',
|
|
52
|
+
'function',
|
|
53
|
+
'method',
|
|
54
|
+
'accessor',
|
|
55
|
+
'property',
|
|
56
|
+
];
|
|
57
|
+
export const DEFAULT_DOCSTRING_KINDS = [...DEFAULT_API_LINK_KINDS];
|
|
58
|
+
export const DEFAULT_EXAMPLE_KINDS = ['function', 'method', 'accessor'];
|
|
59
|
+
export const DEFAULT_MEMBER_PAGE_KINDS = ['method', 'property', 'accessor'];
|
|
60
|
+
|
|
61
|
+
function isRecord(value) {
|
|
62
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function unknownKeys(value, allowed) {
|
|
66
|
+
return Object.keys(value).filter((key) => !allowed.has(key));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function assertNoUnknownKeys(value, allowed, label) {
|
|
70
|
+
const keys = unknownKeys(value, allowed);
|
|
71
|
+
if (keys.length > 0) {
|
|
72
|
+
throw new Error(`${label} contains unknown key${keys.length === 1 ? '' : 's'}: ${keys.join(', ')}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function assertString(value, label) {
|
|
77
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
78
|
+
throw new Error(`${label} must be a non-empty string.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function assertStringArray(value, label, { allowEmpty = false, allowedValues } = {}) {
|
|
83
|
+
if (!Array.isArray(value) || (!allowEmpty && value.length === 0)) {
|
|
84
|
+
throw new Error(`${label} must be ${allowEmpty ? 'an' : 'a non-empty'} array.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const item of value) {
|
|
88
|
+
assertString(item, `${label} entries`);
|
|
89
|
+
if (allowedValues && !allowedValues.has(item)) {
|
|
90
|
+
throw new Error(`${label} contains unsupported value "${item}".`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (new Set(value).size !== value.length) {
|
|
95
|
+
throw new Error(`${label} must not contain duplicate values.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function validateLinkEntryPoints(value) {
|
|
100
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
101
|
+
throw new Error('linkEntryPoints must be a non-empty array.');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (const [index, entryPoint] of value.entries()) {
|
|
105
|
+
if (!isRecord(entryPoint)) {
|
|
106
|
+
throw new Error(`linkEntryPoints[${index}] must be an object.`);
|
|
107
|
+
}
|
|
108
|
+
assertNoUnknownKeys(entryPoint, LINK_ENTRY_POINT_KEYS, `linkEntryPoints[${index}]`);
|
|
109
|
+
assertString(entryPoint.path, `linkEntryPoints[${index}].path`);
|
|
110
|
+
if (entryPoint.tsconfig !== undefined) {
|
|
111
|
+
assertString(entryPoint.tsconfig, `linkEntryPoints[${index}].tsconfig`);
|
|
112
|
+
}
|
|
113
|
+
if (entryPoint.namespaceExportsOnly !== undefined && typeof entryPoint.namespaceExportsOnly !== 'boolean') {
|
|
114
|
+
throw new Error(`linkEntryPoints[${index}].namespaceExportsOnly must be a boolean.`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function validateChecks(value) {
|
|
120
|
+
if (!isRecord(value)) {
|
|
121
|
+
throw new Error('checks must be an object.');
|
|
122
|
+
}
|
|
123
|
+
assertNoUnknownKeys(value, CHECK_KEYS, 'checks');
|
|
124
|
+
for (const key of CHECK_KEYS) {
|
|
125
|
+
if (value[key] !== undefined) {
|
|
126
|
+
assertStringArray(value[key], `checks.${key}`, {
|
|
127
|
+
allowEmpty: true,
|
|
128
|
+
allowedValues: REFLECTION_KINDS,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function validateExamples(value) {
|
|
135
|
+
if (value === false) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!isRecord(value)) {
|
|
139
|
+
throw new Error('examples must be false or an object.');
|
|
140
|
+
}
|
|
141
|
+
assertNoUnknownKeys(value, EXAMPLE_KEYS, 'examples');
|
|
142
|
+
for (const key of EXAMPLE_KEYS) {
|
|
143
|
+
if (value[key] !== undefined && typeof value[key] !== 'boolean') {
|
|
144
|
+
throw new Error(`examples.${key} must be a boolean.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function validateTypeDocOverrides(value) {
|
|
150
|
+
if (!isRecord(value)) {
|
|
151
|
+
throw new Error('typedoc must be an object.');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (const key of Object.keys(value)) {
|
|
155
|
+
if (PROTECTED_TYPEDOC_KEYS.has(key) || key.toLowerCase().startsWith('textmode')) {
|
|
156
|
+
throw new Error(`typedoc.${key} is owned by @textmode/docs and cannot be overridden.`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function validateDocsConfig(config) {
|
|
162
|
+
if (!isRecord(config)) {
|
|
163
|
+
throw new Error('The documentation config must export an object created by defineDocs().');
|
|
164
|
+
}
|
|
165
|
+
assertNoUnknownKeys(config, TOP_LEVEL_KEYS, 'Documentation config');
|
|
166
|
+
|
|
167
|
+
if (config.entryPoints !== undefined) {
|
|
168
|
+
assertStringArray(config.entryPoints, 'entryPoints');
|
|
169
|
+
}
|
|
170
|
+
if (config.linkEntryPoints !== undefined) {
|
|
171
|
+
validateLinkEntryPoints(config.linkEntryPoints);
|
|
172
|
+
}
|
|
173
|
+
if (config.sourceRoots !== undefined) {
|
|
174
|
+
assertStringArray(config.sourceRoots, 'sourceRoots');
|
|
175
|
+
}
|
|
176
|
+
if (config.outputDir !== undefined) {
|
|
177
|
+
assertString(config.outputDir, 'outputDir');
|
|
178
|
+
}
|
|
179
|
+
if (config.apiBaseUrl !== undefined) {
|
|
180
|
+
assertString(config.apiBaseUrl, 'apiBaseUrl');
|
|
181
|
+
try {
|
|
182
|
+
new URL(config.apiBaseUrl);
|
|
183
|
+
} catch {
|
|
184
|
+
throw new Error('apiBaseUrl must be an absolute URL.');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (config.tsconfig !== undefined) {
|
|
188
|
+
assertString(config.tsconfig, 'tsconfig');
|
|
189
|
+
}
|
|
190
|
+
if (config.memberPageKinds !== undefined) {
|
|
191
|
+
assertStringArray(config.memberPageKinds, 'memberPageKinds', {
|
|
192
|
+
allowEmpty: true,
|
|
193
|
+
allowedValues: MEMBER_PAGE_KINDS,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (
|
|
197
|
+
config.stripApiSelfLinks !== undefined &&
|
|
198
|
+
config.stripApiSelfLinks !== 'minimal' &&
|
|
199
|
+
config.stripApiSelfLinks !== 'full'
|
|
200
|
+
) {
|
|
201
|
+
throw new Error('stripApiSelfLinks must be "minimal" or "full".');
|
|
202
|
+
}
|
|
203
|
+
if (config.checks !== undefined) {
|
|
204
|
+
validateChecks(config.checks);
|
|
205
|
+
}
|
|
206
|
+
if (config.examples !== undefined) {
|
|
207
|
+
validateExamples(config.examples);
|
|
208
|
+
}
|
|
209
|
+
if (config.typedoc !== undefined) {
|
|
210
|
+
validateTypeDocOverrides(config.typedoc);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function sourceRootsForEntryPoints(entryPoints) {
|
|
215
|
+
return Array.from(
|
|
216
|
+
new Set(
|
|
217
|
+
entryPoints.map((entryPoint) => {
|
|
218
|
+
const normalized = entryPoint.path.replaceAll('\\', '/').replace(/^\.\//, '');
|
|
219
|
+
const firstSegment = normalized.split('/')[0];
|
|
220
|
+
return firstSegment || path.dirname(normalized);
|
|
221
|
+
})
|
|
222
|
+
)
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function resolveProjectPath(projectRoot, value) {
|
|
227
|
+
return path.resolve(projectRoot, value);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function origin(config, key, fallback) {
|
|
231
|
+
return Object.hasOwn(config, key) ? 'textmode.docs.config.mjs' : fallback;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function loadProjectConfig(projectRoot, requestedConfigPath) {
|
|
235
|
+
const configPath = requestedConfigPath
|
|
236
|
+
? resolveProjectPath(projectRoot, requestedConfigPath)
|
|
237
|
+
: path.join(projectRoot, 'textmode.docs.config.mjs');
|
|
238
|
+
|
|
239
|
+
if (!fs.existsSync(configPath)) {
|
|
240
|
+
if (requestedConfigPath) {
|
|
241
|
+
throw new Error(`Documentation config does not exist: ${configPath}`);
|
|
242
|
+
}
|
|
243
|
+
return { config: {}, configPath: null };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const module = await import(pathToFileURL(configPath).href);
|
|
247
|
+
const config = module.default;
|
|
248
|
+
if (!config?.[CONFIG_MARKER]) {
|
|
249
|
+
throw new Error(`${configPath} must default-export defineDocs({ ... }).`);
|
|
250
|
+
}
|
|
251
|
+
validateDocsConfig(config);
|
|
252
|
+
return { config, configPath };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export async function resolveDocsConfig({ cwd = process.cwd(), configPath: requestedConfigPath, out } = {}) {
|
|
256
|
+
const projectRoot = path.resolve(cwd);
|
|
257
|
+
const packageJsonPath = path.join(projectRoot, 'package.json');
|
|
258
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
259
|
+
throw new Error(`Missing package.json in documentation project root: ${projectRoot}`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
263
|
+
assertString(packageJson.name, 'package.json#name');
|
|
264
|
+
const packageDescription =
|
|
265
|
+
typeof packageJson.description === 'string' && packageJson.description.trim()
|
|
266
|
+
? packageJson.description.trim()
|
|
267
|
+
: `API reference for ${packageJson.name}`;
|
|
268
|
+
const { config, configPath } = await loadProjectConfig(projectRoot, requestedConfigPath);
|
|
269
|
+
|
|
270
|
+
const entryPoints = config.entryPoints ?? ['src/index.ts'];
|
|
271
|
+
const tsconfig = config.tsconfig ?? 'tsconfig.json';
|
|
272
|
+
const linkEntryPoints =
|
|
273
|
+
config.linkEntryPoints ??
|
|
274
|
+
entryPoints.map((entryPoint) => ({
|
|
275
|
+
path: entryPoint,
|
|
276
|
+
tsconfig,
|
|
277
|
+
namespaceExportsOnly: false,
|
|
278
|
+
}));
|
|
279
|
+
const sourceRoots = config.sourceRoots ?? sourceRootsForEntryPoints(linkEntryPoints);
|
|
280
|
+
const checks = {
|
|
281
|
+
apiLinks: config.checks?.apiLinks ?? DEFAULT_API_LINK_KINDS,
|
|
282
|
+
docstrings: config.checks?.docstrings ?? DEFAULT_DOCSTRING_KINDS,
|
|
283
|
+
examples: config.checks?.examples ?? DEFAULT_EXAMPLE_KINDS,
|
|
284
|
+
};
|
|
285
|
+
const examples =
|
|
286
|
+
config.examples === false
|
|
287
|
+
? false
|
|
288
|
+
: {
|
|
289
|
+
checkSketches: config.examples?.checkSketches ?? true,
|
|
290
|
+
forbidGallerySourceBase: config.examples?.forbidGallerySourceBase ?? true,
|
|
291
|
+
};
|
|
292
|
+
const outputDir = out ?? config.outputDir ?? `api/${packageJson.name}`;
|
|
293
|
+
|
|
294
|
+
const resolved = {
|
|
295
|
+
projectRoot,
|
|
296
|
+
configPath,
|
|
297
|
+
packageJsonPath,
|
|
298
|
+
packageName: packageJson.name,
|
|
299
|
+
packageDescription,
|
|
300
|
+
entryPoints: entryPoints.map((entryPoint) => resolveProjectPath(projectRoot, entryPoint)),
|
|
301
|
+
linkEntryPoints: linkEntryPoints.map((entryPoint) => ({
|
|
302
|
+
path: resolveProjectPath(projectRoot, entryPoint.path),
|
|
303
|
+
tsconfig: resolveProjectPath(projectRoot, entryPoint.tsconfig ?? tsconfig),
|
|
304
|
+
namespaceExportsOnly: entryPoint.namespaceExportsOnly ?? false,
|
|
305
|
+
})),
|
|
306
|
+
sourceRoots: sourceRoots.map((sourceRoot) => resolveProjectPath(projectRoot, sourceRoot)),
|
|
307
|
+
outputDir: resolveProjectPath(projectRoot, outputDir),
|
|
308
|
+
apiBaseUrl: config.apiBaseUrl ?? `https://code.textmode.art/api/${packageJson.name}`,
|
|
309
|
+
tsconfig: resolveProjectPath(projectRoot, tsconfig),
|
|
310
|
+
memberPageKinds: config.memberPageKinds ?? DEFAULT_MEMBER_PAGE_KINDS,
|
|
311
|
+
stripApiSelfLinks: config.stripApiSelfLinks ?? 'full',
|
|
312
|
+
checks,
|
|
313
|
+
examples,
|
|
314
|
+
typedoc: config.typedoc ?? {},
|
|
315
|
+
origins: {
|
|
316
|
+
packageName: 'package.json#name',
|
|
317
|
+
packageDescription:
|
|
318
|
+
typeof packageJson.description === 'string' && packageJson.description.trim()
|
|
319
|
+
? 'package.json#description'
|
|
320
|
+
: 'default',
|
|
321
|
+
entryPoints: origin(config, 'entryPoints', 'default'),
|
|
322
|
+
linkEntryPoints: origin(config, 'linkEntryPoints', origin(config, 'entryPoints', 'default')),
|
|
323
|
+
sourceRoots: origin(config, 'sourceRoots', 'derived:linkEntryPoints'),
|
|
324
|
+
outputDir: out ? 'command:--out' : origin(config, 'outputDir', 'derived:package.json#name'),
|
|
325
|
+
apiBaseUrl: origin(config, 'apiBaseUrl', 'derived:package.json#name'),
|
|
326
|
+
tsconfig: origin(config, 'tsconfig', 'default'),
|
|
327
|
+
memberPageKinds: origin(config, 'memberPageKinds', 'default'),
|
|
328
|
+
stripApiSelfLinks: origin(config, 'stripApiSelfLinks', 'default'),
|
|
329
|
+
checks: {
|
|
330
|
+
apiLinks: config.checks?.apiLinks ? 'textmode.docs.config.mjs' : 'default',
|
|
331
|
+
docstrings: config.checks?.docstrings ? 'textmode.docs.config.mjs' : 'default',
|
|
332
|
+
examples: config.checks?.examples ? 'textmode.docs.config.mjs' : 'default',
|
|
333
|
+
},
|
|
334
|
+
examples:
|
|
335
|
+
config.examples === false
|
|
336
|
+
? 'textmode.docs.config.mjs'
|
|
337
|
+
: {
|
|
338
|
+
checkSketches:
|
|
339
|
+
config.examples?.checkSketches !== undefined ? 'textmode.docs.config.mjs' : 'default',
|
|
340
|
+
forbidGallerySourceBase:
|
|
341
|
+
config.examples?.forbidGallerySourceBase !== undefined
|
|
342
|
+
? 'textmode.docs.config.mjs'
|
|
343
|
+
: 'default',
|
|
344
|
+
},
|
|
345
|
+
typedoc: origin(config, 'typedoc', 'default'),
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
return resolved;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function displayPath(projectRoot, value) {
|
|
353
|
+
const relative = path.relative(projectRoot, value);
|
|
354
|
+
return relative && !relative.startsWith('..') ? relative.split(path.sep).join('/') : value;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function formatResolvedConfig(config) {
|
|
358
|
+
return {
|
|
359
|
+
projectRoot: config.projectRoot,
|
|
360
|
+
configPath: config.configPath,
|
|
361
|
+
packageName: config.packageName,
|
|
362
|
+
packageDescription: config.packageDescription,
|
|
363
|
+
entryPoints: config.entryPoints.map((value) => displayPath(config.projectRoot, value)),
|
|
364
|
+
linkEntryPoints: config.linkEntryPoints.map((entryPoint) => ({
|
|
365
|
+
path: displayPath(config.projectRoot, entryPoint.path),
|
|
366
|
+
tsconfig: displayPath(config.projectRoot, entryPoint.tsconfig),
|
|
367
|
+
namespaceExportsOnly: entryPoint.namespaceExportsOnly,
|
|
368
|
+
})),
|
|
369
|
+
sourceRoots: config.sourceRoots.map((value) => displayPath(config.projectRoot, value)),
|
|
370
|
+
outputDir: displayPath(config.projectRoot, config.outputDir),
|
|
371
|
+
apiBaseUrl: config.apiBaseUrl,
|
|
372
|
+
tsconfig: displayPath(config.projectRoot, config.tsconfig),
|
|
373
|
+
memberPageKinds: config.memberPageKinds,
|
|
374
|
+
stripApiSelfLinks: config.stripApiSelfLinks,
|
|
375
|
+
checks: config.checks,
|
|
376
|
+
examples: config.examples,
|
|
377
|
+
typedoc: config.typedoc,
|
|
378
|
+
origins: config.origins,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { hostedApiUrl, routeForReflection } from './api-doc-routes.mjs';
|
|
2
|
+
import { getPrimaryLocation, getRelevantComments } from './reflection-policy.mjs';
|
|
3
|
+
|
|
4
|
+
export const ISSUE_KIND = {
|
|
5
|
+
MISSING_DOCSTRING: 'missing-docstring',
|
|
6
|
+
MISSING_EXAMPLE: 'missing-example',
|
|
7
|
+
MISSING_API_LINK: 'missing-api-link',
|
|
8
|
+
INVALID_API_LINK: 'invalid-api-link',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function hasDocstring(reflection) {
|
|
12
|
+
return getRelevantComments(reflection).length > 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function hasExample(reflection) {
|
|
16
|
+
return getRelevantComments(reflection).some(({ comment }) =>
|
|
17
|
+
comment.getTags('@example').some((tag) => tag.content.some((part) => (part.text ?? '').trim().length > 0))
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getApiLinkTargets(comment, apiBaseUrl) {
|
|
22
|
+
return comment
|
|
23
|
+
.getTags('@see')
|
|
24
|
+
.flatMap((tag) => tag.content)
|
|
25
|
+
.filter((part) => part.kind === 'inline-tag' && part.tag === '@link')
|
|
26
|
+
.map((part) => part.target)
|
|
27
|
+
.filter((target) => typeof target === 'string' && target.startsWith(`${apiBaseUrl}/`));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getInvalidApiLinkTargets(comment, routes, apiBaseUrl) {
|
|
31
|
+
return getApiLinkTargets(comment, apiBaseUrl).filter((target) => {
|
|
32
|
+
const routeWithAnchor = target.slice(`${apiBaseUrl}/`.length);
|
|
33
|
+
if (!routeWithAnchor || routeWithAnchor === 'index') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const [route] = routeWithAnchor.split('#');
|
|
38
|
+
return !routes.has(route);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function hasApiLink(comment, apiBaseUrl) {
|
|
43
|
+
return getApiLinkTargets(comment, apiBaseUrl).length > 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createApiLinkTargets(reflection, routes, apiBaseUrl) {
|
|
47
|
+
const route = routeForReflection(reflection, routes);
|
|
48
|
+
if (!route) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const routeWithoutAnchor = route.split('#')[0];
|
|
53
|
+
if (!routes.has(routeWithoutAnchor)) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const url = hostedApiUrl(apiBaseUrl, route);
|
|
58
|
+
const label = `${reflection.getFriendlyFullName()} API reference`;
|
|
59
|
+
const seeLine = `@see {@link ${url} | ${label}}`;
|
|
60
|
+
|
|
61
|
+
return getRelevantComments(reflection).map(({ source }) => ({
|
|
62
|
+
reflection,
|
|
63
|
+
source,
|
|
64
|
+
seeLine,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function createIssue(reflection, kind) {
|
|
69
|
+
const location = getPrimaryLocation(reflection);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
kind,
|
|
73
|
+
name: reflection.getFriendlyFullName(),
|
|
74
|
+
file: location.file,
|
|
75
|
+
line: location.line,
|
|
76
|
+
column: location.column,
|
|
77
|
+
location: location.printable,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function createCommentIssue(reflection, commentSource, kind, detail) {
|
|
82
|
+
const location = commentSource
|
|
83
|
+
? {
|
|
84
|
+
file: commentSource.fullFileName || getPrimaryLocation(reflection).file,
|
|
85
|
+
line: commentSource.line || 1,
|
|
86
|
+
column: (commentSource.character ?? 0) + 1,
|
|
87
|
+
}
|
|
88
|
+
: getPrimaryLocation(reflection);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
kind,
|
|
92
|
+
name: reflection.getFriendlyFullName(),
|
|
93
|
+
file: location.file,
|
|
94
|
+
line: location.line,
|
|
95
|
+
column: location.column,
|
|
96
|
+
location: `${location.file}:${location.line}:${location.column}`,
|
|
97
|
+
detail,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function collectApiDocIssues({
|
|
102
|
+
apiBaseUrl,
|
|
103
|
+
apiLinkReflections,
|
|
104
|
+
docstringReflections,
|
|
105
|
+
exampleReflections,
|
|
106
|
+
routes,
|
|
107
|
+
}) {
|
|
108
|
+
const missingDocstrings = docstringReflections
|
|
109
|
+
.filter((reflection) => !hasDocstring(reflection))
|
|
110
|
+
.map((reflection) => createIssue(reflection, ISSUE_KIND.MISSING_DOCSTRING))
|
|
111
|
+
.sort(compareIssues);
|
|
112
|
+
|
|
113
|
+
const missingExamples = exampleReflections
|
|
114
|
+
.filter((reflection) => hasDocstring(reflection) && !hasExample(reflection))
|
|
115
|
+
.map((reflection) => createIssue(reflection, ISSUE_KIND.MISSING_EXAMPLE))
|
|
116
|
+
.sort(compareIssues);
|
|
117
|
+
|
|
118
|
+
const comments = apiLinkReflections.flatMap((reflection) =>
|
|
119
|
+
getRelevantComments(reflection).map(({ comment, source }) => ({ reflection, comment, source }))
|
|
120
|
+
);
|
|
121
|
+
const missingApiLinks = comments
|
|
122
|
+
.filter(({ comment }) => !hasApiLink(comment, apiBaseUrl))
|
|
123
|
+
.map(({ reflection, source }) => createCommentIssue(reflection, source, ISSUE_KIND.MISSING_API_LINK))
|
|
124
|
+
.sort(compareIssues);
|
|
125
|
+
const invalidApiLinks = comments
|
|
126
|
+
.flatMap(({ reflection, comment, source }) =>
|
|
127
|
+
getInvalidApiLinkTargets(comment, routes, apiBaseUrl).map((target) =>
|
|
128
|
+
createCommentIssue(reflection, source, ISSUE_KIND.INVALID_API_LINK, target)
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
.sort(compareIssues);
|
|
132
|
+
|
|
133
|
+
return { missingDocstrings, missingExamples, missingApiLinks, invalidApiLinks };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function countApiDocIssues(issueGroups) {
|
|
137
|
+
return Object.values(issueGroups).reduce((total, issues) => total + issues.length, 0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function compareIssues(left, right) {
|
|
141
|
+
return (
|
|
142
|
+
left.file.localeCompare(right.file) ||
|
|
143
|
+
left.line - right.line ||
|
|
144
|
+
left.column - right.column ||
|
|
145
|
+
left.name.localeCompare(right.name)
|
|
146
|
+
);
|
|
147
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { ReflectionKind } from 'typedoc';
|
|
5
|
+
|
|
6
|
+
export const KIND_DIRECTORIES = new Map([
|
|
7
|
+
[ReflectionKind.Class, 'classes'],
|
|
8
|
+
[ReflectionKind.Interface, 'interfaces'],
|
|
9
|
+
[ReflectionKind.TypeAlias, 'type-aliases'],
|
|
10
|
+
[ReflectionKind.Enum, 'enumerations'],
|
|
11
|
+
[ReflectionKind.Variable, 'variables'],
|
|
12
|
+
[ReflectionKind.Function, 'functions'],
|
|
13
|
+
[ReflectionKind.Method, 'methods'],
|
|
14
|
+
[ReflectionKind.Accessor, 'accessors'],
|
|
15
|
+
[ReflectionKind.Property, 'properties'],
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export function readMarkdownRoutes(apiDocsDir) {
|
|
19
|
+
const routes = new Set();
|
|
20
|
+
|
|
21
|
+
function walk(dir) {
|
|
22
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
23
|
+
const fullPath = path.join(dir, entry.name);
|
|
24
|
+
if (entry.isDirectory()) {
|
|
25
|
+
walk(fullPath);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!entry.name.endsWith('.md')) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const relative = path.relative(apiDocsDir, fullPath).replaceAll(path.sep, '/');
|
|
34
|
+
const withoutExtension = relative.replace(/\.md$/, '');
|
|
35
|
+
const route = withoutExtension.endsWith('/index')
|
|
36
|
+
? withoutExtension.slice(0, -'/index'.length)
|
|
37
|
+
: withoutExtension;
|
|
38
|
+
routes.add(route);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
walk(apiDocsDir);
|
|
43
|
+
return routes;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function slugAnchor(name) {
|
|
47
|
+
return name
|
|
48
|
+
.toLowerCase()
|
|
49
|
+
.replace(/[^a-z0-9_-]+/g, '-')
|
|
50
|
+
.replace(/^-+|-+$/g, '');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function routeForReflection(reflection, routes) {
|
|
54
|
+
if (reflection.isProject()) {
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (reflection.kind === ReflectionKind.Namespace) {
|
|
59
|
+
const parentRoute =
|
|
60
|
+
reflection.parent && !reflection.parent.isProject() ? routeForReflection(reflection.parent, routes) : '';
|
|
61
|
+
return [parentRoute, 'namespaces', reflection.name].filter(Boolean).join('/');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
reflection.kind === ReflectionKind.Method ||
|
|
66
|
+
reflection.kind === ReflectionKind.Accessor ||
|
|
67
|
+
reflection.kind === ReflectionKind.Property
|
|
68
|
+
) {
|
|
69
|
+
const ownerRoute = routeForReflection(reflection.parent, routes);
|
|
70
|
+
const ownPageRoute = [ownerRoute, KIND_DIRECTORIES.get(reflection.kind), reflection.name]
|
|
71
|
+
.filter(Boolean)
|
|
72
|
+
.join('/');
|
|
73
|
+
|
|
74
|
+
if (routes.has(ownPageRoute)) {
|
|
75
|
+
return ownPageRoute;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return `${ownerRoute}#${slugAnchor(reflection.name)}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const directory = KIND_DIRECTORIES.get(reflection.kind);
|
|
82
|
+
if (!directory) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const parentRoute =
|
|
87
|
+
reflection.parent && !reflection.parent.isProject() ? routeForReflection(reflection.parent, routes) : '';
|
|
88
|
+
return [parentRoute, directory, reflection.name].filter(Boolean).join('/');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function hostedApiUrl(apiBaseUrl, route) {
|
|
92
|
+
return `${apiBaseUrl}/${route}`;
|
|
93
|
+
}
|