Skip to content

Commit 280939e

Browse files
author
Martii
committed
Transform exact style tags from *markdown* generated to equivalent *bootstrap* class for tables
* Allows alignment of tables to be specified with GFM. * Briefly mentioned at OpenUserJS#221 (comment)
1 parent 1385f89 commit 280939e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

libs/htmlWhitelistPost.json

+18
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@
4747
"rt",
4848
"rp"
4949
],
50+
"allowedClasses": {
51+
"th": [
52+
"text-center",
53+
"text-left",
54+
"text-right"
55+
],
56+
"td": [
57+
"text-center",
58+
"text-left",
59+
"text-right"
60+
]
61+
},
5062
"allowedAttributes": {
63+
"th": [
64+
"class"
65+
],
66+
"td": [
67+
"class"
68+
],
5169
"a": [
5270
"href"
5371
],

libs/markdown.js

+34
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ htmlWhitelistPost.allowedTags.forEach(function (aTag) {
2727
});
2828
delete htmlWhitelistPost.allowedAttributes.all;
2929

30+
// Transform exact Github Flavored Markdown generated style tags to bootstrap custom classes
31+
// to allow the sanitizer to whitelist on th and td tags for table alignment
32+
function gfmStyleToBootstrapClass(aTagName, aAttribs) {
33+
if (aAttribs.style) {
34+
switch (aAttribs.style) {
35+
case 'text-align:center':
36+
return {
37+
tagName: aTagName,
38+
attribs: { class: 'text-center' }
39+
}
40+
case 'text-align:left':
41+
return {
42+
tagName: aTagName,
43+
attribs: { class: 'text-left' }
44+
}
45+
case 'text-align:right':
46+
return {
47+
tagName: aTagName,
48+
attribs: { class: 'text-right' }
49+
}
50+
}
51+
}
52+
53+
return {
54+
tagName: aTagName,
55+
attribs: aAttribs
56+
}
57+
}
58+
59+
htmlWhitelistPost.transformTags = {
60+
'th' : gfmStyleToBootstrapClass,
61+
'td' : gfmStyleToBootstrapClass
62+
};
63+
3064
function sanitize(aHtml) {
3165
return sanitizeHtml(aHtml, htmlWhitelistPost);
3266
}

0 commit comments

Comments
 (0)