forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool.js
More file actions
70 lines (64 loc) · 2 KB
/
tool.js
File metadata and controls
70 lines (64 loc) · 2 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
import { allTools } from '#src/tools/lib/all-tools.js'
import { allPlatforms } from '#src/tools/lib/all-platforms.js'
export const tags = Object.keys(allTools).concat(allPlatforms).concat(['rowheaders'])
// The trailing newline is important. Without it, the line immediately after
// the `</div>` will be considered part of the previous block, which means the Markdown following the `</div>` will not be rendered to HTML correctly. For example:
//
// <div>Here's some stuff</div>
// And *here* us also some stuff.
//
// Another **sentence** here.
//
// Will yield:
//
// <div>Here's some stuff</div>
// And *here* us also some stuff.
//
// <p>Another <b>sentence</b> here.</p>
//
// when rendering this template with unified.
// If you instead inject an extra newline after the `</div>`, you
// go from:
//
// <div>Here's some stuff</div>
//
// And *here* us also some stuff.
//
// Another **sentence** here.
//
// which yields:
//
// <div>Here's some stuff</div>
//
// <p>And <i>here</i> us also some stuff.</p>
//
// <p>Another <b>sentence</b> here.</p>
//
// The Tool Liquid tags are a little bit fragile because we hope and assume
// that the author of the Liquid+Markdown *don't* do this:
//
// {% vscode %}Bla bla.{% endvscode %}Next stuff here...
//
const template = '<div class="ghd-tool {{ tagName }}">{{ output }}</div>\n'
export const Tool = {
type: 'block',
parse(tagToken, remainTokens) {
this.tagName = tagToken.name
this.templates = []
const stream = this.liquid.parser.parseStream(remainTokens)
stream
.on(`tag:end${this.tagName}`, () => stream.stop())
.on('template', (tpl) => this.templates.push(tpl))
.on('end', () => {
throw new Error(`tag ${tagToken.getText()} not closed`)
})
stream.start()
},
render: function* (scope) {
const output = yield this.liquid.renderer.renderTemplates(this.templates, scope)
return yield this.liquid.parseAndRender(template, {
tagName: this.tagName,
output,
})
},
}