-
Notifications
You must be signed in to change notification settings - Fork 607
/
MarkdownEmitter.ts
247 lines (213 loc) · 7.47 KB
/
MarkdownEmitter.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
type DocNode,
DocNodeKind,
type StringBuilder,
type DocPlainText,
type DocHtmlStartTag,
type DocHtmlEndTag,
type DocCodeSpan,
type DocLinkTag,
type DocParagraph,
type DocFencedCode,
type DocSection,
DocNodeTransforms,
type DocEscapedText,
type DocErrorText,
type DocBlockTag
} from '@microsoft/tsdoc';
import { InternalError } from '@rushstack/node-core-library';
import { IndentedWriter } from '../utils/IndentedWriter';
export interface IMarkdownEmitterOptions {}
export interface IMarkdownEmitterContext<TOptions = IMarkdownEmitterOptions> {
writer: IndentedWriter;
boldRequested: boolean;
italicRequested: boolean;
writingBold: boolean;
writingItalic: boolean;
options: TOptions;
}
/**
* Renders MarkupElement content in the Markdown file format.
* For more info: https://en.wikipedia.org/wiki/Markdown
*/
export class MarkdownEmitter {
public emit(stringBuilder: StringBuilder, docNode: DocNode, options: IMarkdownEmitterOptions): string {
const writer: IndentedWriter = new IndentedWriter(stringBuilder);
const context: IMarkdownEmitterContext = {
writer,
boldRequested: false,
italicRequested: false,
writingBold: false,
writingItalic: false,
options
};
this.writeNode(docNode, context, false);
writer.ensureNewLine(); // finish the last line
return writer.toString();
}
protected getEscapedText(text: string): string {
const textWithBackslashes: string = text
.replace(/\\/g, '\\\\') // first replace the escape character
.replace(/[*#[\]_|`~]/g, (x) => '\\' + x) // then escape any special characters
.replace(/---/g, '\\-\\-\\-') // hyphens only if it's 3 or more
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
return textWithBackslashes;
}
protected getTableEscapedText(text: string): string {
return text
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\|/g, '|');
}
/**
* @virtual
*/
protected writeNode(docNode: DocNode, context: IMarkdownEmitterContext, docNodeSiblings: boolean): void {
const writer: IndentedWriter = context.writer;
switch (docNode.kind) {
case DocNodeKind.PlainText: {
const docPlainText: DocPlainText = docNode as DocPlainText;
this.writePlainText(docPlainText.text, context);
break;
}
case DocNodeKind.HtmlStartTag:
case DocNodeKind.HtmlEndTag: {
const docHtmlTag: DocHtmlStartTag | DocHtmlEndTag = docNode as DocHtmlStartTag | DocHtmlEndTag;
// write the HTML element verbatim into the output
writer.write(docHtmlTag.emitAsHtml());
break;
}
case DocNodeKind.CodeSpan: {
const docCodeSpan: DocCodeSpan = docNode as DocCodeSpan;
writer.write('`');
writer.write(docCodeSpan.code);
writer.write('`');
break;
}
case DocNodeKind.LinkTag: {
const docLinkTag: DocLinkTag = docNode as DocLinkTag;
if (docLinkTag.codeDestination) {
this.writeLinkTagWithCodeDestination(docLinkTag, context);
} else if (docLinkTag.urlDestination) {
this.writeLinkTagWithUrlDestination(docLinkTag, context);
} else if (docLinkTag.linkText) {
this.writePlainText(docLinkTag.linkText, context);
}
break;
}
case DocNodeKind.Paragraph: {
const docParagraph: DocParagraph = docNode as DocParagraph;
const trimmedParagraph: DocParagraph = DocNodeTransforms.trimSpacesInParagraph(docParagraph);
this.writeNodes(trimmedParagraph.nodes, context);
writer.ensureNewLine();
writer.writeLine();
break;
}
case DocNodeKind.FencedCode: {
const docFencedCode: DocFencedCode = docNode as DocFencedCode;
writer.ensureNewLine();
writer.write('```');
writer.write(docFencedCode.language);
writer.writeLine();
writer.write(docFencedCode.code);
writer.ensureNewLine();
writer.writeLine('```');
break;
}
case DocNodeKind.Section: {
const docSection: DocSection = docNode as DocSection;
this.writeNodes(docSection.nodes, context);
break;
}
case DocNodeKind.SoftBreak: {
if (!/^\s?$/.test(writer.peekLastCharacter())) {
writer.write(' ');
}
break;
}
case DocNodeKind.EscapedText: {
const docEscapedText: DocEscapedText = docNode as DocEscapedText;
this.writePlainText(docEscapedText.decodedText, context);
break;
}
case DocNodeKind.ErrorText: {
const docErrorText: DocErrorText = docNode as DocErrorText;
this.writePlainText(docErrorText.text, context);
break;
}
case DocNodeKind.InlineTag: {
break;
}
case DocNodeKind.BlockTag: {
const tagNode: DocBlockTag = docNode as DocBlockTag;
console.warn('Unsupported block tag: ' + tagNode.tagName);
break;
}
default:
throw new InternalError('Unsupported DocNodeKind kind: ' + docNode.kind);
}
}
/** @virtual */
protected writeLinkTagWithCodeDestination(docLinkTag: DocLinkTag, context: IMarkdownEmitterContext): void {
// The subclass needs to implement this to support code destinations
throw new InternalError('writeLinkTagWithCodeDestination()');
}
/** @virtual */
protected writeLinkTagWithUrlDestination(docLinkTag: DocLinkTag, context: IMarkdownEmitterContext): void {
const linkText: string =
docLinkTag.linkText !== undefined ? docLinkTag.linkText : docLinkTag.urlDestination!;
const encodedLinkText: string = this.getEscapedText(linkText.replace(/\s+/g, ' '));
context.writer.write('[');
context.writer.write(encodedLinkText);
context.writer.write(`](${docLinkTag.urlDestination!})`);
}
protected writePlainText(text: string, context: IMarkdownEmitterContext): void {
const writer: IndentedWriter = context.writer;
// split out the [ leading whitespace, content, trailing whitespace ]
const parts: string[] = text.match(/^(\s*)(.*?)(\s*)$/) || [];
writer.write(parts[1]); // write leading whitespace
const middle: string = parts[2];
if (middle !== '') {
switch (writer.peekLastCharacter()) {
case '':
case '\n':
case ' ':
case '[':
case '>':
// okay to put a symbol
break;
default:
// This is no problem: "**one** *two* **three**"
// But this is trouble: "**one***two***three**"
// The most general solution: "**one**<!-- -->*two*<!-- -->**three**"
writer.write('<!-- -->');
break;
}
if (context.boldRequested) {
writer.write('**');
}
if (context.italicRequested) {
writer.write('_');
}
writer.write(this.getEscapedText(middle));
if (context.italicRequested) {
writer.write('_');
}
if (context.boldRequested) {
writer.write('**');
}
}
writer.write(parts[3]); // write trailing whitespace
}
protected writeNodes(docNodes: ReadonlyArray<DocNode>, context: IMarkdownEmitterContext): void {
for (const docNode of docNodes) {
this.writeNode(docNode, context, docNodes.length > 1);
}
}
}