forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-emitter.js
227 lines (198 loc) · 7.04 KB
/
markdown-emitter.js
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
const { MarkdownEmitter } = require("@fluid-tools/api-markdown-documenter");
/**
* Custom {@link MarkdownEmitter} that generates HTML tables.
*
* @remarks Used by `./api-markdown-documenter.js`.
*/
class HugoMarkdownEmitter extends MarkdownEmitter {
/**
* @param {ApiModel} apiModel - See {@link @fluid-tools/api-markdown-documenter#MarkdownEmitter.apiModel}
* @param {((contextApiItem: ApiItem) => string) | undefined} generateFrontMatter - See
* {@link @fluid-tools/api-markdown-documenter#MarkdownEmitter.generateFrontMatter}
*/
constructor(apiModel, generateFrontMatter) {
super(apiModel, generateFrontMatter);
}
/**
* Override base logic to handle plain-text rendering in HTML context.
* Namely, we have to avoid escaping symbols.
*
* @override
*/
writePlainText(text, context) {
const writer = context.writer;
// split out the [ leading whitespace, content, trailing whitespace ]
const parts = text.match(/^(\s*)(.*?)(\s*)$/) || [];
writer.write(parts[1]); // write leading whitespace
const middle = 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('<b>');
}
if (context.italicRequested) {
writer.write('<i>');
}
if (context.insideHTML) {
writer.write(middle);
} else {
writer.write(this.getEscapedText(middle));
}
if (context.italicRequested) {
writer.write('</i>');
}
if (context.boldRequested) {
writer.write('</b>');
}
}
writer.write(parts[3]); // write trailing whitespace
}
/**
* Override base logic to make use of Hugo callouts for note boxes.
*
* @override
*/
writeAlert(docAlert, context) {
const writer = context.writer;
writer.ensureNewLine();
writer.writeLine(`{{% callout ${docAlert.type ?? 'note'} ${docAlert.title ?? ''} %}}`);
this.writeNode(docAlert.content, context, false);
writer.ensureNewLine();
writer.writeLine('{{% /callout %}}');
writer.writeLine();
}
/**
* Override base logic to make use of Hugo callouts for note boxes.
*
* @override
*/
writeNoteBox(docNoteBox, context) {
const writer = context.writer;
writer.ensureNewLine();
writer.writeLine('{{% callout note %}}');
this.writeNode(docNoteBox.content, context, false);
writer.ensureNewLine();
writer.writeLine('{{% /callout %}}');
writer.writeLine();
}
/**
* Overrides base logic to write the provided table in HTML format.
*
* @param {DocTable} docTable - The table to be written.
* @param {MarkdownEmitterContext} context - The Emitter context.
*
* @override
*/
writeTable(docTable, context) {
const writer = context.writer;
const childContext = {
...context,
insideHTML: true,
insideTable: true,
}
let columnCount = 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
writer.writeLine(`<table class="table table-striped table-hover">`);
writer.increaseIndent();
writer.writeLine('<thead>');
writer.increaseIndent();
writer.writeLine('<tr>');
writer.increaseIndent();
for (let i = 0; i < columnCount; ++i) {
if (docTable.header) {
const cell = docTable.header.cells[i];
if (cell) {
writer.writeLine('<th scope="col">');
writer.increaseIndent();
this.writeNode(cell.content, childContext, false);
writer.ensureNewLine();
writer.decreaseIndent();
writer.writeLine('</th>');
}
}
}
writer.decreaseIndent();
writer.writeLine('</tr>');
writer.decreaseIndent();
writer.writeLine('</thead>');
writer.writeLine('<tbody>');
writer.increaseIndent();
for (const row of docTable.rows) {
writer.writeLine('<tr>');
writer.increaseIndent();
for (const cell of row.cells) {
writer.writeLine('<td>');
writer.increaseIndent();
this.writeNode(cell.content, childContext, false);
writer.ensureNewLine();
writer.decreaseIndent();
writer.writeLine('</td>');
}
writer.decreaseIndent();
writer.writeLine('</tr>');
}
writer.decreaseIndent();
writer.writeLine('</tbody>');
writer.decreaseIndent();
writer.writeLine('</table>')
writer.writeLine();
}
/**
* Writes the specified link.
* If in an HTML context, the link will be written in HTML format.
* Otherwise, it will be written as a standard Markdown-format link.
*
* @param {string} linkText - The display text of the link being written.
* @param {string} linkTarget - The target URL of the link being written.
* @param {MarkdownEmitterContext} context - The Emitter context.
*
* @override
*/
writeLink(linkText, linkTarget, context) {
if (context.insideHTML) {
this.writeHtmlLink(linkText, linkTarget, context);
} else {
if(context.insideTable) {
console.error("---MD LINK IN TABLE---");
}
super.writeLink(linkText, linkTarget, context);
}
}
/**
* Writes an HTML-formatted link for the given target and text.
*
* @param {string} linkText - The display text of the link being written.
* @param {string} linkTarget - The target URL of the link being written.
* @param {MarkdownEmitterContext} context - The Emitter context.
*/
writeHtmlLink(linkText, linkTarget, context) {
context.writer.write(`<a href='${linkTarget}'>${linkText}</a>`);
}
}
module.exports = HugoMarkdownEmitter;