forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-content.js
More file actions
270 lines (235 loc) · 7.92 KB
/
render-content.js
File metadata and controls
270 lines (235 loc) · 7.92 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const cheerio = require('cheerio')
const renderContent = require('../../lib/render-content/renderContent')
const { EOL } = require('os')
// Use platform-specific line endings for realistic tests when templates have
// been loaded from disk
const nl = str => str.replace(/\n/g, EOL)
describe('renderContent', () => {
test(
'takes a template and a context and returns a string (async)',
async () => {
const template = 'my favorite color is {{ color }}.'
const context = { color: 'orange' }
const output = await renderContent(template, context)
expect(output, '<p>my favorite color is orange.</p>')
}
)
test('preserves content within {% raw %} tags', async () => {
const template = nl(
'For example: {% raw %}{% include cool_header.html %}{% endraw %}.'
)
const expected = '<p>For example: {% include cool_header.html %}.</p>'
const output = await renderContent(template)
expect(output).toBe(expected)
})
test(
'removes extra newlines to prevent lists from breaking',
async () => {
const template = nl(`
1. item one
1. item two
1. item three`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($('ol').length).toBe(1)
expect($('ol > li').length).toBe(3)
}
)
test('removes extra newlines from lists of links', async () => {
const template = nl(`- <a>item</a>
- <a>item</a>`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($('ul p').length, 0)
})
test('renders text only', async () => {
const template = 'my favorite color is {{ color }}.'
const context = { color: 'orange' }
const output = await renderContent(template, context, { textOnly: true })
expect(output, 'my favorite color is orange.')
})
test('throws on rendering errors', async () => {
const template = 1
const context = {}
let err
try {
await renderContent(template, context)
} catch (_err) {
err = _err
}
expect(err).toBeTruthy()
})
test(
'warns and throws on rendering errors when the file name is passed',
async () => {
const template = 1
const context = {}
let err
let warned = false
const error = console.error
console.error = message => {
expect(message, 'renderContent failed on file: name')
console.error = error
warned = true
}
try {
await renderContent(template, context, { filename: 'name' })
} catch (_err) {
err = _err
}
expect(err).toBeTruthy()
expect(warned).toBeTruthy()
}
)
test('renders empty templates', async () => {
const template = ''
const context = {}
const output = await renderContent(template, context)
expect(output).toBe('')
})
test('encodes entities', async () => {
const template = '<beep></beep>'
const context = {}
const output = await renderContent(template, context, {
encodeEntities: true
})
expect(output).toBe('<p><beep></beep></p>')
})
test('does not render newlines around links in tables', async () => {
const template = nl(`
| Keyboard shortcut | Description
|-----------|------------
|<kbd>g</kbd> <kbd>c</kbd> | Go to the **Code** tab
|<kbd>g</kbd> <kbd>i</kbd> | Go to the **Issues** tab. For more information, see "[About issues](/articles/about-issues)."
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect(
$.html().includes(
'"<a href="/articles/about-issues">About issues</a>."'
)
).toBeTruthy()
})
test(
'does not render newlines around inline code in tables',
async () => {
const template = nl(`
| Package manager | formats |
| --- | --- |
| Python | \`requirements.txt\`, \`pipfile.lock\`
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect(
$.html().includes(
'<code>requirements.txt</code>, <code>pipfile.lock</code>'
)
).toBeTruthy()
}
)
test('does not render newlines around emphasis in code', async () => {
const template = nl(`
| Qualifier | Example
| ------------- | -------------
| <code>user:<em>USERNAME</em></code> | [**user:defunkt ubuntu**](https://github.com/search?q=user%3Adefunkt+ubuntu&type=Issues) matches issues with the word "ubuntu" from repositories owned by @defunkt.
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($.html().includes('<code>user:<em>USERNAME</em></code>')).toBeTruthy()
})
test('renders code blocks with # comments', async () => {
const template = nl(`
1. This is a list item with code containing a comment:
\`\`\`shell
$ foo the bar
# some comment here
\`\`\`
1. This is another list item.
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($('ol').length).toBe(1)
expect($.html().includes('# some comment here')).toBeTruthy()
expect($.html().includes('<h1 id="some-comment-here">')).toBeFalsy()
expect($.html().includes('<a href="#some-comment-here">')).toBeFalsy()
})
test('renders headings at the right level', async () => {
const template = nl(`
# This is a level one
## This is a level two
### This is a level three
#### This is a level four
##### This is a level five
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect(
$.html().includes(
'<h1 id="this-is-a-level-one"><a href="#this-is-a-level-one">This is a level one</a></h1>'
)
).toBeTruthy()
expect(
$.html().includes(
'<h2 id="this-is-a-level-two"><a href="#this-is-a-level-two">This is a level two</a></h2>'
)
).toBeTruthy()
expect(
$.html().includes(
'<h3 id="this-is-a-level-three"><a href="#this-is-a-level-three">This is a level three</a></h3>'
)
).toBeTruthy()
expect(
$.html().includes(
'<h4 id="this-is-a-level-four"><a href="#this-is-a-level-four">This is a level four</a></h4>'
)
).toBeTruthy()
expect(
$.html().includes(
'<h5 id="this-is-a-level-five"><a href="#this-is-a-level-five">This is a level five</a></h5>'
)
).toBeTruthy()
})
test('does syntax highlighting', async () => {
const template = nl(`
\`\`\`js
const example = true
\`\`\`\`
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($.html().includes('<pre><code class="hljs language-js">')).toBeTruthy()
})
test('does not autoguess code block language', async () => {
const template = nl(`
\`\`\`
some code
\`\`\`\
`)
const html = await renderContent(template)
const $ = cheerio.load(html, { xmlMode: true })
expect($.html().includes('<pre><code>some code\n</code></pre>')).toBeTruthy()
})
test('renders a line break in a table', async () => {
const content = `| Webhook event payload | Activity types |
| --------------------- | -------------- |
| [\`issues\`](/webhooks/event-payloads/#issues) | - \`opened\`<br/>- \`edited\`<br/>- \`other\` |`
const file = await renderContent(content)
expect(file).toBe(
'<table><thead><tr><th>Webhook event payload</th><th>Activity types</th></tr></thead><tbody><tr><td><a href="/webhooks/event-payloads/#issues"><code>issues</code></a></td><td>- <code>opened</code><br>- <code>edited</code><br>- <code>other</code></td></tr></tbody></table>'
)
})
test(
'renders a copy button for code blocks with {:copy} annotation',
async () => {
const template = nl(`
\`\`\`js{:copy}
some code
\`\`\`\
`)
const html = await renderContent(template)
const $ = cheerio.load(html)
const el = $('button.js-btn-copy')
expect(el.data('clipboard-text')).toBe('some code')
}
)
})