Skip to content

Commit 9a093e3

Browse files
committed
Implement naive autolinking for simple type references only
1 parent 6fc5909 commit 9a093e3

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

apps/api-documenter/src/yaml/YamlDocumenter.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,48 @@ const yamlApiSchema: JsonSchema = JsonSchema.fromFile(path.join(__dirname, 'type
4040
*/
4141
export class YamlDocumenter {
4242
private _docItemSet: DocItemSet;
43+
44+
// This is used by the _linkToUidIfPossible() workaround.
45+
// It stores a mapping from type name (e.g. "MyClass") to the corresponding DocItem.
46+
// If the mapping would be ambiguous (e.g. "MyClass" is defined by multiple packages)
47+
// then it is excluded from the mapping. Also excluded are DocItems (such as package
48+
// and function) which are not typically used as a data type.
49+
private _docItemsByTypeName: Map<string, DocItem>;
50+
4351
private _outputFolder: string;
4452

4553
public constructor(docItemSet: DocItemSet) {
4654
this._docItemSet = docItemSet;
55+
this._docItemsByTypeName = new Map<string, DocItem>();
56+
57+
// Collect the _docItemsByTypeName table
58+
const ambiguousNames: Set<string> = new Set<string>();
59+
60+
this._docItemSet.forEach((docItem: DocItem) => {
61+
switch (docItem.kind) {
62+
case DocItemKind.Class:
63+
case DocItemKind.Enum:
64+
case DocItemKind.Interface:
65+
const typeName: string = docItem.name;
66+
if (ambiguousNames.has(typeName)) {
67+
break;
68+
}
69+
70+
if (this._docItemsByTypeName.has(typeName)) {
71+
// We saw this name before, so it's an ambiguous match
72+
ambiguousNames.add(typeName);
73+
break;
74+
}
75+
76+
this._docItemsByTypeName.set(typeName, docItem);
77+
break;
78+
}
79+
});
80+
81+
// Remove the ambiguous matches
82+
for (const ambiguousName of ambiguousNames) {
83+
this._docItemsByTypeName.delete(ambiguousName);
84+
}
4785
}
4886

4987
public generateFiles(outputFolder: string): void { // virtual
@@ -330,7 +368,7 @@ export class YamlDocumenter {
330368
.replace(/^\s*-\s+/, ''); // temporary workaround for people who mistakenly add a hyphen, e.g. "@returns - blah"
331369

332370
syntax.return = {
333-
type: [ apiMethod.returnValue.type ],
371+
type: [ this._linkToUidIfPossible(apiMethod.returnValue.type) ],
334372
description: returnDescription
335373
};
336374
}
@@ -342,7 +380,7 @@ export class YamlDocumenter {
342380
{
343381
id: parameterName,
344382
description: this._renderMarkdown(apiParameter.description, docItem),
345-
type: [ apiParameter.type || '' ]
383+
type: [ this._linkToUidIfPossible(apiParameter.type || '') ]
346384
} as IYamlParameter
347385
);
348386
}
@@ -364,7 +402,7 @@ export class YamlDocumenter {
364402

365403
if (apiProperty.type) {
366404
syntax.return = {
367-
type: [ apiProperty.type ]
405+
type: [ this._linkToUidIfPossible(apiProperty.type) ]
368406
};
369407
}
370408
}
@@ -450,6 +488,25 @@ export class YamlDocumenter {
450488
return result;
451489
}
452490

491+
/**
492+
* This is a temporary workaround to enable limited autolinking of API item types
493+
* until the YAML file format is enhanced to support general hyperlinks.
494+
* @remarks
495+
* In the current version, fields such as IApiProperty.type allow either:
496+
* (1) a UID identifier such as "node-core-library.JsonFile" which will be rendered
497+
* as a hyperlink to that type name, or (2) a block of freeform text that must not
498+
* contain any Markdown links. The _substituteUidForSimpleType() function assumes
499+
* it is given #2 but substitutes #1 if the name can be matched to a DocItem.
500+
*/
501+
private _linkToUidIfPossible(typeName: string): string {
502+
const docItem: DocItem | undefined = this._docItemsByTypeName.get(typeName.trim());
503+
if (docItem) {
504+
// Substitute the UID
505+
return this._getUid(docItem);
506+
}
507+
return typeName;
508+
}
509+
453510
private _getYamlItemName(docItem: DocItem): string {
454511
if (docItem.parent && docItem.parent.kind === DocItemKind.Namespace) {
455512
// For members a namespace, show the full name excluding the package part:

0 commit comments

Comments
 (0)