3
3
* Licensed under the MIT License.
4
4
*/
5
5
6
+ //@ts -check
7
+ /** @typedef {import("@fluid-tools/api-markdown-documenter").ApiItem } ApiItem */
8
+ /** @typedef {import("@fluid-tools/api-markdown-documenter").ApiItemTransformationConfiguration } ApiItemTransformationConfiguration */
9
+ /** @typedef {import("@fluid-tools/api-markdown-documenter").ApiPackage } ApiPackage */
10
+
6
11
import {
7
12
ApiItemKind ,
8
13
ApiItemUtilities ,
@@ -31,8 +36,19 @@ import {
31
36
const generatedContentNotice =
32
37
"<!-- Do not edit this file. It is automatically generated by @fluidtools/api-markdown-documenter. -->" ;
33
38
39
+ /**
40
+ * Type guard for identifying a package API item.
41
+ *
42
+ * @param {ApiItem } apiItem - The API item being type-checked.
43
+ * @returns {apiItem is ApiPackage } Whether the API item is a package.
44
+ */
45
+ function isPackage ( apiItem ) {
46
+ return apiItem . kind === ApiItemKind . Package ;
47
+ }
48
+
34
49
/**
35
50
* Generates a documentation suite for the API model saved under `inputDir`, saving the output to `outputDir`.
51
+ *
36
52
* @param {string } inputDir - The directory path containing the API model to be processed.
37
53
* @param {string } outputDir - The directory path under which the generated documentation suite will be saved.
38
54
* @param {string } uriRootDir - The base for all links between API members.
@@ -43,6 +59,7 @@ const generatedContentNotice =
43
59
export async function renderApiDocumentation ( inputDir , outputDir , uriRootDir , apiVersion ) {
44
60
/**
45
61
* Logs a progress message, prefaced with the API version number to help differentiate parallel logging output.
62
+ * @param {string } message - The progress message to log.
46
63
*/
47
64
function logProgress ( message ) {
48
65
console . log ( `(v${ apiVersion } ) ${ message } ` ) ;
@@ -51,6 +68,11 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
51
68
/**
52
69
* Logs the error with the specified message, prefaced with the API version number to help differentiate parallel
53
70
* logging output, and re-throws the error.
71
+ *
72
+ * @param {string } message - The progress message to log.
73
+ * @param {unknown } error - The error to log and re-throw.
74
+ *
75
+ * @returns {never } This function always throws an error.
54
76
*/
55
77
function logErrorAndRethrow ( message , error ) {
56
78
console . error ( chalk . red ( `(v${ apiVersion } ) ${ message } :` ) ) ;
@@ -70,10 +92,16 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
70
92
71
93
const apiModel = await loadModel ( { modelDirectoryPath : inputDir } ) ;
72
94
73
- // Custom renderers that utilize Docusaurus syntax for certain kinds of documentation elements.
95
+ /**
96
+ * Custom renderers that utilize Docusaurus syntax for certain kinds of documentation elements.
97
+ * @type {import("@fluid-tools/api-markdown-documenter").MarkdownRenderers }
98
+ */
74
99
const customRenderers = {
100
+ // @ts -ignore TODO: fix typing in API-Markdown-Documenter package
75
101
[ DocumentationNodeType . BlockQuote ] : renderBlockQuoteNode ,
102
+ // @ts -ignore TODO: fix typing in API-Markdown-Documenter package
76
103
[ DocumentationNodeType . Table ] : renderTableNode ,
104
+ // @ts -ignore TODO: fix typing in API-Markdown-Documenter package
77
105
[ admonitionNodeType ] : renderAdmonitionNode ,
78
106
} ;
79
107
@@ -115,7 +143,6 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
115
143
}
116
144
} ,
117
145
} ,
118
- newlineKind : "lf" ,
119
146
uriRoot : uriRootDir ,
120
147
includeBreadcrumb : false , // Docusaurus includes this by default based on file hierarchy
121
148
includeTopLevelDocumentHeading : false , // We inject `title` front-matter metadata instead
@@ -146,7 +173,7 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
146
173
} ,
147
174
exclude : ( apiItem ) => {
148
175
// Exclude packages that aren't intended for public consumption.
149
- if ( apiItem . kind === ApiItemKind . Package ) {
176
+ if ( isPackage ( apiItem ) ) {
150
177
const packageName = apiItem . name ;
151
178
const packageScope = PackageName . getScope ( packageName ) ;
152
179
@@ -181,6 +208,9 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
181
208
await Promise . all (
182
209
documents . map ( async ( document ) => {
183
210
const documentApiItem = document . apiItem ;
211
+ if ( documentApiItem === undefined ) {
212
+ throw new Error ( "Document does not have an associated API item." ) ;
213
+ }
184
214
185
215
// #region Filter documents based on site-specific requirements
186
216
@@ -225,14 +255,22 @@ export async function renderApiDocumentation(inputDir, outputDir, uriRootDir, ap
225
255
await fs . writeFile ( filePath , fileContents ) ;
226
256
} catch ( error ) {
227
257
logErrorAndRethrow (
228
- `Encountered error while writing file output for "${ document . apiItem . displayName } "` ,
258
+ `Encountered error while writing file output for "${ documentApiItem . displayName } "` ,
229
259
error ,
230
260
) ;
231
261
}
232
262
} ) ,
233
263
) ;
234
264
}
235
265
266
+ /**
267
+ * Generate `yaml`-formatted front-matter for the API item. For use by Docusaurus.
268
+ *
269
+ * @param {ApiItem } documentApiItem - The item for which front-matter is being generated.
270
+ * @param {ApiItemTransformationConfiguration } config - The transformation configuration.
271
+ *
272
+ * @returns {string } The front-matter, formatted as a string.
273
+ */
236
274
function createFrontMatter ( documentApiItem , config ) {
237
275
let title , sidebarLabel ;
238
276
if ( documentApiItem . kind === ApiItemKind . Model ) {
0 commit comments