-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathCustomMarkdownEmitter.test.ts
More file actions
193 lines (175 loc) · 7.1 KB
/
CustomMarkdownEmitter.test.ts
File metadata and controls
193 lines (175 loc) · 7.1 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
190
191
192
193
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
DocSection,
type TSDocConfiguration,
DocPlainText,
StringBuilder,
DocParagraph,
DocSoftBreak,
DocLinkTag,
DocHtmlStartTag,
DocHtmlEndTag,
DocBlockTag
} from '@microsoft/tsdoc';
import { CustomDocNodes } from '../../nodes/CustomDocNodeKind.ts';
import { DocHeading } from '../../nodes/DocHeading.ts';
import { DocEmphasisSpan } from '../../nodes/DocEmphasisSpan.ts';
import { DocTable } from '../../nodes/DocTable.ts';
import { DocTableRow } from '../../nodes/DocTableRow.ts';
import { DocTableCell } from '../../nodes/DocTableCell.ts';
import { CustomMarkdownEmitter } from '../CustomMarkdownEmitter.ts';
import { ApiModel, type ApiItem } from '@microsoft/api-extractor-model';
test('render Markdown from TSDoc', () => {
const configuration: TSDocConfiguration = CustomDocNodes.configuration;
const output: DocSection = new DocSection({ configuration });
output.appendNodes([
new DocHeading({ configuration, title: 'Simple bold test' }),
new DocParagraph({ configuration }, [
new DocPlainText({ configuration, text: 'This is a ' }),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'bold' })]),
new DocPlainText({ configuration, text: ' word.' })
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'All whitespace bold' }),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: ' ' })])
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Newline bold' }),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: 'line 1' }),
new DocSoftBreak({ configuration }),
new DocPlainText({ configuration, text: 'line 2' })
])
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Newline bold with spaces' }),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: ' line 1 ' }),
new DocSoftBreak({ configuration }),
new DocPlainText({ configuration, text: ' line 2 ' }),
new DocSoftBreak({ configuration }),
new DocPlainText({ configuration, text: ' line 3 ' })
])
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Adjacent bold regions' }),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'one' })]),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'two' })]),
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: ' three ' })
]),
new DocPlainText({ configuration, text: '' }),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'four' })]),
new DocPlainText({ configuration, text: 'non-bold' }),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'five' })])
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Adjacent to other characters' }),
new DocParagraph({ configuration }, [
new DocLinkTag({
configuration,
tagName: '@link',
linkText: 'a link',
urlDestination: './index.md'
}),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'bold' })]),
new DocPlainText({ configuration, text: 'non-bold' }),
new DocPlainText({ configuration, text: 'more-non-bold' })
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Unknown block tag' }),
new DocParagraph({ configuration }, [
new DocBlockTag({
configuration,
tagName: '@unknown'
}),
new DocEmphasisSpan({ configuration, bold: true }, [new DocPlainText({ configuration, text: 'bold' })]),
new DocPlainText({ configuration, text: 'non-bold' }),
new DocPlainText({ configuration, text: 'more-non-bold' })
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Bad characters' }),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: '*one*two*' })
]),
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: 'three*four' })
])
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Characters that should be escaped' }),
new DocParagraph({ configuration }, [
new DocPlainText({ configuration, text: 'Double-encoded JSON: "{ \\"A\\": 123}"' })
]),
new DocParagraph({ configuration }, [
new DocPlainText({ configuration, text: 'HTML chars: <script>alert("[You] are #1!");</script>' })
]),
new DocParagraph({ configuration }, [new DocPlainText({ configuration, text: 'HTML escape: "' })]),
new DocParagraph({ configuration }, [
new DocPlainText({ configuration, text: '3 or more hyphens: - -- --- ---- ----- ------' })
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'HTML tag' }),
new DocParagraph({ configuration }, [
new DocHtmlStartTag({ configuration, name: 'b' }),
new DocPlainText({ configuration, text: 'bold' }),
new DocHtmlEndTag({ configuration, name: 'b' })
])
]);
output.appendNodes([
new DocHeading({ configuration, title: 'Table' }),
new DocTable(
{
configuration,
headerTitles: ['Header 1', 'Header 2']
},
[
new DocTableRow({ configuration }, [
new DocTableCell({ configuration }, [
new DocParagraph({ configuration }, [new DocPlainText({ configuration, text: 'Cell 1' })])
]),
new DocTableCell({ configuration }, [
new DocParagraph({ configuration }, [new DocPlainText({ configuration, text: 'Cell 2' })]),
new DocParagraph({ configuration }, [
new DocEmphasisSpan({ configuration, bold: true }, [
new DocPlainText({ configuration, text: 'bold text' })
])
])
])
])
]
)
]);
output.appendNodes([
new DocHeading({ configuration, title: 'After a table' }),
new DocParagraph({ configuration }, [
new DocPlainText({ configuration, text: 'just checking lines after a table' })
])
]);
const stringBuilder: StringBuilder = new StringBuilder();
const apiModel: ApiModel = new ApiModel();
const markdownEmitter: CustomMarkdownEmitter = new CustomMarkdownEmitter(apiModel);
markdownEmitter.emit(stringBuilder, output, {
contextApiItem: undefined,
onGetFilenameForApiItem: (apiItem: ApiItem) => {
return '#';
}
});
expect(stringBuilder.toString()).toMatchSnapshot();
console.log(stringBuilder.toString());
});