forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMarkdownEmitter.ts
More file actions
189 lines (157 loc) · 6.09 KB
/
CustomMarkdownEmitter.ts
File metadata and controls
189 lines (157 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as colors from 'colors';
import {
DocNode, DocLinkTag, StringBuilder
} from '@microsoft/tsdoc';
import {
ApiModel,
IResolveDeclarationReferenceResult,
ApiItem
} from '@microsoft/api-extractor-model';
import { CustomDocNodeKind } from '../nodes/CustomDocNodeKind';
import { DocHeading } from '../nodes/DocHeading';
import { DocNoteBox } from '../nodes/DocNoteBox';
import { DocTable } from '../nodes/DocTable';
import { DocTableCell } from '../nodes/DocTableCell';
import { DocEmphasisSpan } from '../nodes/DocEmphasisSpan';
import {
MarkdownEmitter,
IMarkdownEmitterContext,
IMarkdownEmitterOptions
} from './MarkdownEmitter';
import { IndentedWriter } from '../utils/IndentedWriter';
export interface ICustomMarkdownEmitterOptions extends IMarkdownEmitterOptions {
contextApiItem: ApiItem | undefined;
onGetFilenameForApiItem: (apiItem: ApiItem) => string | undefined;
}
export class CustomMarkdownEmitter extends MarkdownEmitter {
private _apiModel: ApiModel;
public constructor (apiModel: ApiModel) {
super();
this._apiModel = apiModel;
}
public emit(stringBuilder: StringBuilder, docNode: DocNode, options: ICustomMarkdownEmitterOptions): string {
return super.emit(stringBuilder, docNode, options);
}
/** @override */
protected writeNode(docNode: DocNode, context: IMarkdownEmitterContext, docNodeSiblings: boolean): void {
const writer: IndentedWriter = context.writer;
switch (docNode.kind) {
case CustomDocNodeKind.Heading: {
const docHeading: DocHeading = docNode as DocHeading;
writer.ensureSkippedLine();
let prefix: string;
switch (docHeading.level) {
case 1: prefix = '##'; break;
case 2: prefix = '###'; break;
case 3: prefix = '###'; break;
default:
prefix = '####';
}
writer.writeLine(prefix + ' ' + this.getEscapedText(docHeading.title));
writer.writeLine();
break;
}
case CustomDocNodeKind.NoteBox: {
const docNoteBox: DocNoteBox = docNode as DocNoteBox;
writer.ensureNewLine();
writer.increaseIndent('> ');
this.writeNode(docNoteBox.content, context, false);
writer.ensureNewLine();
writer.decreaseIndent();
writer.writeLine();
break;
}
case CustomDocNodeKind.Table: {
const docTable: DocTable = docNode as DocTable;
// GitHub's markdown renderer chokes on tables that don't have a blank line above them,
// whereas VS Code's renderer is totally fine with it.
writer.ensureSkippedLine();
context.insideTable = true;
// Markdown table rows can have inconsistent cell counts. Size the table based on the longest row.
let columnCount: number = 0;
if (docTable.header) {
columnCount = docTable.header.cells.length;
}
for (const row of docTable.rows) {
if (row.cells.length > columnCount) {
columnCount = row.cells.length;
}
}
// write the table header (which is required by Markdown)
writer.write('| ');
for (let i: number = 0; i < columnCount; ++i) {
writer.write(' ');
if (docTable.header) {
const cell: DocTableCell | undefined = docTable.header.cells[i];
if (cell) {
this.writeNode(cell.content, context, false);
}
}
writer.write(' |');
}
writer.writeLine();
// write the divider
writer.write('| ');
for (let i: number = 0; i < columnCount; ++i) {
writer.write(' --- |');
}
writer.writeLine();
for (const row of docTable.rows) {
writer.write('| ');
for (const cell of row.cells) {
writer.write(' ');
this.writeNode(cell.content, context, false);
writer.write(' |');
}
writer.writeLine();
}
writer.writeLine();
context.insideTable = false;
break;
}
case CustomDocNodeKind.EmphasisSpan: {
const docEmphasisSpan: DocEmphasisSpan = docNode as DocEmphasisSpan;
const oldBold: boolean = context.boldRequested;
const oldItalic: boolean = context.italicRequested;
context.boldRequested = docEmphasisSpan.bold;
context.italicRequested = docEmphasisSpan.italic;
this.writeNodes(docEmphasisSpan.nodes, context);
context.boldRequested = oldBold;
context.italicRequested = oldItalic;
break;
}
default:
super.writeNode(docNode, context, false);
}
}
/** @override */
protected writeLinkTagWithCodeDestination(docLinkTag: DocLinkTag,
context: IMarkdownEmitterContext<ICustomMarkdownEmitterOptions>): void {
const options: ICustomMarkdownEmitterOptions = context.options;
const result: IResolveDeclarationReferenceResult
= this._apiModel.resolveDeclarationReference(docLinkTag.codeDestination!, options.contextApiItem);
if (result.resolvedApiItem) {
const filename: string | undefined = options.onGetFilenameForApiItem(result.resolvedApiItem);
if (filename) {
let linkText: string = docLinkTag.linkText || '';
if (linkText.length === 0) {
// Generate a name such as Namespace1.Namespace2.MyClass.myMethod()
linkText = result.resolvedApiItem.getScopedNameWithinPackage();
}
if (linkText.length > 0) {
const encodedLinkText: string = this.getEscapedText(linkText.replace(/\s+/g, ' '));
context.writer.write('[');
context.writer.write(encodedLinkText);
context.writer.write(`](${filename!})`);
} else {
console.log(colors.yellow('WARNING: Unable to determine link text'));
}
}
} else if (result.errorMessage) {
console.log(colors.yellow(`WARNING: Unable to resolve reference "${docLinkTag.codeDestination!.emitAsTsdoc()}": `
+ result.errorMessage));
}
}
}