UNPKG

5.95 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.
4var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5 if (k2 === undefined) k2 = k;
6 var desc = Object.getOwnPropertyDescriptor(m, k);
7 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8 desc = { enumerable: true, get: function() { return m[k]; } };
9 }
10 Object.defineProperty(o, k2, desc);
11}) : (function(o, m, k, k2) {
12 if (k2 === undefined) k2 = k;
13 o[k2] = m[k];
14}));
15var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16 Object.defineProperty(o, "default", { enumerable: true, value: v });
17}) : function(o, v) {
18 o["default"] = v;
19});
20var __importStar = (this && this.__importStar) || function (mod) {
21 if (mod && mod.__esModule) return mod;
22 var result = {};
23 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24 __setModuleDefault(result, mod);
25 return result;
26};
27var __importDefault = (this && this.__importDefault) || function (mod) {
28 return (mod && mod.__esModule) ? mod : { "default": mod };
29};
30Object.defineProperty(exports, "__esModule", { value: true });
31exports.OfficeYamlDocumenter = void 0;
32const colors_1 = __importDefault(require("colors"));
33const path = __importStar(require("path"));
34const yaml = require("js-yaml");
35const node_core_library_1 = require("@rushstack/node-core-library");
36const YamlDocumenter_1 = require("./YamlDocumenter");
37/**
38 * Extends YamlDocumenter with some custom logic that is specific to Office Add-ins.
39 */
40class OfficeYamlDocumenter extends YamlDocumenter_1.YamlDocumenter {
41 constructor(apiModel, inputFolder, newDocfxNamespaces) {
42 super(apiModel, newDocfxNamespaces);
43 // Default API Set URL when no product match is found.
44 this._apiSetUrlDefault = '/office/dev/add-ins/reference/javascript-api-for-office';
45 // Hash set of API Set URLs based on product.
46 this._apiSetUrls = {
47 Excel: '/javascript/api/requirement-sets/excel/excel-api-requirement-sets',
48 OneNote: '/javascript/api/requirement-sets/onenote/onenote-api-requirement-sets',
49 Outlook: '/javascript/api/requirement-sets/outlook/outlook-api-requirement-sets',
50 PowerPoint: '/javascript/api/requirement-sets/powerpoint/powerpoint-api-requirement-sets',
51 Visio: '/office/dev/add-ins/reference/overview/visio-javascript-reference-overview',
52 Word: '/javascript/api/requirement-sets/word/word-api-requirement-sets'
53 };
54 const snippetsFilePath = path.join(inputFolder, 'snippets.yaml');
55 console.log('Loading snippets from ' + snippetsFilePath);
56 const snippetsContent = node_core_library_1.FileSystem.readFile(snippetsFilePath);
57 this._snippets = yaml.load(snippetsContent, { filename: snippetsFilePath });
58 this._snippetsAll = yaml.load(snippetsContent, { filename: snippetsFilePath });
59 }
60 /** @override */
61 generateFiles(outputFolder) {
62 super.generateFiles(outputFolder);
63 // After we generate everything, check for any unused snippets
64 console.log();
65 for (const apiName of Object.keys(this._snippets)) {
66 console.error(colors_1.default.yellow('Warning: Unused snippet ' + apiName));
67 }
68 }
69 /** @override */
70 onGetTocRoot() {
71 // override
72 return {
73 name: 'API reference',
74 href: 'overview.md',
75 items: []
76 };
77 }
78 /** @override */
79 onCustomizeYamlItem(yamlItem) {
80 const nameWithoutPackage = yamlItem.uid.replace(/^[^.]+\!/, '');
81 if (yamlItem.summary) {
82 yamlItem.summary = this._fixupApiSet(yamlItem.summary, yamlItem.uid);
83 }
84 if (yamlItem.remarks) {
85 yamlItem.remarks = this._fixupApiSet(yamlItem.remarks, yamlItem.uid);
86 }
87 const snippets = this._snippetsAll[nameWithoutPackage];
88 if (snippets) {
89 delete this._snippets[nameWithoutPackage];
90 const snippetText = this._generateExampleSnippetText(snippets);
91 if (yamlItem.remarks) {
92 yamlItem.remarks += snippetText;
93 }
94 else if (yamlItem.syntax && yamlItem.syntax.return) {
95 if (!yamlItem.syntax.return.description) {
96 yamlItem.syntax.return.description = '';
97 }
98 yamlItem.syntax.return.description += snippetText;
99 }
100 else {
101 yamlItem.remarks = snippetText;
102 }
103 }
104 }
105 _fixupApiSet(markup, uid) {
106 // Search for a pattern such as this:
107 // \[Api set: ExcelApi 1.1\]
108 //
109 // Hyperlink it like this:
110 // \[ [API set: ExcelApi 1.1](http://bing.com?type=excel) \]
111 markup = markup.replace(/Api/, 'API');
112 return markup.replace(/\\\[(API set:[^\]]+)\\\]/, '\\[ [$1](' + this._getApiSetUrl(uid) + ') \\]');
113 }
114 // Gets the link to the API set based on product context. Seeks a case-insensitive match in the hash set.
115 _getApiSetUrl(uid) {
116 for (const key of Object.keys(this._apiSetUrls)) {
117 const regexp = new RegExp(key, 'i');
118 if (regexp.test(uid)) {
119 return this._apiSetUrls[key];
120 }
121 }
122 return this._apiSetUrlDefault; // match not found.
123 }
124 _generateExampleSnippetText(snippets) {
125 const text = ['\n\n#### Examples\n'];
126 for (const snippet of snippets) {
127 text.push(`\`\`\`TypeScript\n${snippet}\n\`\`\``);
128 }
129 return text.join('\n');
130 }
131}
132exports.OfficeYamlDocumenter = OfficeYamlDocumenter;
133//# sourceMappingURL=OfficeYamlDocumenter.js.map
\No newline at end of file