UNPKG

6.32 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.ExperimentalYamlDocumenter = void 0;
6const tsdoc_1 = require("@microsoft/tsdoc");
7const api_extractor_model_1 = require("@microsoft/api-extractor-model");
8const YamlDocumenter_1 = require("./YamlDocumenter");
9/**
10 * EXPERIMENTAL - This documenter is a prototype of a new config file driven mode of operation for
11 * API Documenter. It is not ready for general usage yet. Its design may change in the future.
12 */
13class ExperimentalYamlDocumenter extends YamlDocumenter_1.YamlDocumenter {
14 constructor(apiModel, documenterConfig) {
15 super(apiModel, documenterConfig.configFile.newDocfxNamespaces);
16 this._config = documenterConfig.configFile.tableOfContents;
17 this._tocPointerMap = {};
18 this._generateTocPointersMap(this._config.tocConfig);
19 }
20 /** @override */
21 buildYamlTocFile(apiItems) {
22 this._buildTocItems2(apiItems);
23 return this._config.tocConfig;
24 }
25 _buildTocItems2(apiItems) {
26 const tocItems = [];
27 for (const apiItem of apiItems) {
28 let tocItem;
29 if (apiItem.kind === api_extractor_model_1.ApiItemKind.Namespace && !this.newDocfxNamespaces) {
30 tocItem = {
31 name: this._getTocItemName(apiItem)
32 };
33 }
34 else {
35 if (this._shouldEmbed(apiItem.kind)) {
36 // Don't generate table of contents items for embedded definitions
37 continue;
38 }
39 tocItem = {
40 name: this._getTocItemName(apiItem),
41 uid: this._getUid(apiItem)
42 };
43 if (apiItem.kind !== api_extractor_model_1.ApiItemKind.Package) {
44 this._filterItem(apiItem, tocItem);
45 }
46 }
47 tocItems.push(tocItem);
48 const children = this._getLogicalChildren(apiItem);
49 const childItems = this._buildTocItems2(children);
50 if (childItems.length > 0) {
51 tocItem.items = childItems;
52 }
53 }
54 return tocItems;
55 }
56 // Parses the tocConfig object to build a pointers map of nodes where we want to sort out the API items
57 _generateTocPointersMap(tocConfig) {
58 const { catchAllCategory } = this._config;
59 if (tocConfig.items) {
60 for (const tocItem of tocConfig.items) {
61 if (tocItem.items && tocItem.items.length > 0 && this._shouldNotIncludeInPointersMap(tocItem)) {
62 this._generateTocPointersMap(tocItem);
63 }
64 else {
65 // check for presence of the `catchAllCategory` config option
66 if (catchAllCategory && tocItem.name === catchAllCategory) {
67 this._catchAllPointer = tocItem;
68 }
69 else {
70 this._tocPointerMap[tocItem.name] = tocItem;
71 }
72 }
73 }
74 }
75 }
76 /**
77 * Filtering out the api-item by inlineTags or category name presence in the item name.
78 */
79 _filterItem(apiItem, tocItem) {
80 const { categoryInlineTag, categorizeByName } = this._config;
81 const { name: itemName } = tocItem;
82 let filtered = false;
83 // First we attempt to filter by inline tag if provided.
84 if (apiItem instanceof api_extractor_model_1.ApiDocumentedItem) {
85 const docInlineTag = categoryInlineTag
86 ? this._findInlineTagByName(categoryInlineTag, apiItem.tsdocComment)
87 : undefined;
88 const tagContent = docInlineTag && docInlineTag.tagContent && docInlineTag.tagContent.trim();
89 if (tagContent && this._tocPointerMap[tagContent]) {
90 // null assertion used because when pointer map was created we checked for presence of empty `items` array
91 this._tocPointerMap[tagContent].items.push(tocItem);
92 filtered = true;
93 }
94 }
95 // If not filtered by inline tag and `categorizeByName` config is enabled attempt to filter it by category name.
96 if (!filtered && categorizeByName) {
97 const pointers = Object.keys(this._tocPointerMap);
98 for (let i = 0, length = pointers.length; i < length; i++) {
99 if (itemName.indexOf(pointers[i]) !== -1) {
100 // null assertion used because when pointer map was created we checked for presence of empty `items` array
101 this._tocPointerMap[pointers[i]].items.push(tocItem);
102 filtered = true;
103 break;
104 }
105 }
106 }
107 // If item still not filtered and a `catchAllCategory` config provided push it to it.
108 if (!filtered && this._catchAllPointer && this._catchAllPointer.items) {
109 this._catchAllPointer.items.push(tocItem);
110 }
111 }
112 // This is a direct copy of a @docCategory inline tag finder in office-ui-fabric-react,
113 // but is generic enough to be used for any inline tag
114 _findInlineTagByName(tagName, docComment) {
115 const tagNameToCheck = `@${tagName}`;
116 if (docComment instanceof tsdoc_1.DocInlineTag) {
117 if (docComment.tagName === tagNameToCheck) {
118 return docComment;
119 }
120 }
121 if (docComment) {
122 for (const childNode of docComment.getChildNodes()) {
123 const result = this._findInlineTagByName(tagName, childNode);
124 if (result !== undefined) {
125 return result;
126 }
127 }
128 }
129 return undefined;
130 }
131 _shouldNotIncludeInPointersMap(item) {
132 const { nonEmptyCategoryNodeNames } = this._config;
133 if (nonEmptyCategoryNodeNames && nonEmptyCategoryNodeNames.length) {
134 return nonEmptyCategoryNodeNames.indexOf(item.name) === -1;
135 }
136 return true;
137 }
138}
139exports.ExperimentalYamlDocumenter = ExperimentalYamlDocumenter;
140//# sourceMappingURL=ExperimentalYamlDocumenter.js.map
\No newline at end of file