-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension-tableCaptions.js
67 lines (58 loc) · 1.53 KB
/
extension-tableCaptions.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
/*
* This example demonstrates the use of an extension to generate HTML Table
* elements containing <caption> elements.
*/
var markedIt = require("marked-it-core");
var src = '| Color | RGB |\n| ----- |---------------|\n| Red | {255,0,0} |\n| White | {255,255,255} |\n{: caption="Table of color RGBs" caption-side="top"}\n';
var result = markedIt.generate(src, {
extensions: {
html: {
onTable: function(html, data) {
var table = data.htmlToDom(html)[0];
var captionText = table.attribs["caption"];
if (!captionText) {
return; /* nothing to do */
}
var captionSide = table.attribs["caption-side"];
delete table.attribs["caption"];
delete table.attribs["caption-side"];
var caption = data.htmlToDom("<caption>" + captionText + "</caption>")[0];
if (captionSide) {
caption.attribs["caption-side"] = captionSide;
}
/* the <caption> _must_ be the first child of the <table> */
var children = data.domUtils.getChildren(table);
data.domUtils.prepend(children[0], caption);
return data.domToHtml(table);
}
}
}
});
console.log(src + "========\n" + result.html.text);
/* Output:
| Color | RGB |
| ----- |---------------|
| Red | {255,0,0} |
| White | {255,255,255} |
{: caption="Table of color RGBs" caption-side="top"}
========
<table>
<caption caption-side="top">Table of color RGBs</caption>
<thead>
<tr>
<th>Color</th>
<th>RGB</th>
</tr>
</thead>
<tbody>
<tr>
<td>Red</td>
<td>{255,0,0}</td>
</tr>
<tr>
<td>White</td>
<td>{255,255,255}</td>
</tr>
</tbody>
</table>
*/