Skip to content

Commit d7ef557

Browse files
Refactoring, making raw html work
more info on readme
1 parent ee73df0 commit d7ef557

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

mfr/extensions/md/README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
Most of the packages are in there to try to match what the OSF wiki is doing. The two exceptions being markdown-it-highlightjs.js and markdown-it-mathjax.js. The highlighter matches the functionality of what is on the osf however, and the markdown-it-mathjax.js increases functionality with mathjax.
55

66
to get all the libraries needed:
7+
Note: You do not need to use npm, you can easily get them off of github.
78

8-
```
9+
```bash
910
1011
npm install @centerforopenscience/[email protected]
1112
npm install [email protected]
@@ -14,30 +15,38 @@ npm install [email protected]
1415
npm install [email protected]
1516
```
1617

18+
github:
19+
20+
https://github.com/cos-forks/markdown-it-toc
21+
https://github.com/valeriangalliat/markdown-it-highlightjs
22+
https://github.com/brianjgeiger/markdown-it-ins-del
23+
https://github.com/svbergerem/markdown-it-sanitizer
24+
https://github.com/classeur/markdown-it-mathjax
25+
1726
To add a new library, you need to make sure its loadable in md.js somehow, either through exporting via `root.<name>` or some other means. Some of the markdown plugins added have custom code in them to load them into `root`.
1827

1928
Libraries should try to use the same version as the ones used on the OSF. The plugins do not matter as much, but `markdown-it` and `Mathjax` you should try to match exactly because styling can change between versions.
2029

2130
To add a new library that is not already set up to export to `root` can be a bit tricky, but the gist of it is, wrap the plugin in this code:
2231

23-
```
32+
```javascript
2433
;(function (root, factory) {
25-
if (typeof exports === 'object') {
26-
module.exports = factory()
34+
if (typeof exports === 'object') {
35+
module.exports = factory()
2736
} else {
28-
root.<PLUGIN_NAME> = factory()
29-
}
37+
root.<PLUGIN_NAME> = factory()
38+
}
3039
})(this, function () {
3140

3241

33-
return function(md){
42+
return function(md){
3443

35-
.....
36-
}
44+
.....
45+
}
3746

3847
})
3948
```
4049

4150
And then modify it to work in this context. See other plugins for examples.
4251

43-
Then, in md.js, you can add a plugin to the markdown renderer by adding a `.use(<window.<PLUGIN_NAME>)`
52+
Then, in md.js, you can add a plugin to the markdown renderer by adding a `.use(<window.<PLUGIN_NAME>)` after loading the file into `viewer.mako`

mfr/extensions/md/render.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mako.lookup import TemplateLookup
55

66
from mfr.core import extension
7-
7+
from mfr.extensions.md.settings import BLEACH_WHITELIST
88

99
class MdRenderer(extension.BaseRenderer):
1010

@@ -19,7 +19,9 @@ def __init__(self, *args, **kwargs):
1919
def render(self):
2020
"""Render a markdown file to html."""
2121
with open(self.file_path, 'r') as fp:
22-
body = fp.read()
22+
# Bleach will bug out on unclosed tags. OSF wiki does not. This is
23+
# Due to versioning problems: https://github.com/mozilla/bleach/issues/271
24+
body = bleach.clean(fp.read(), **BLEACH_WHITELIST)
2325
return self.TEMPLATE.render(base=self.assets_url, body=body)
2426

2527
@property

mfr/extensions/md/settings.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
BLEACH_WHITELIST = {
2+
'tags': [
3+
'a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'blockquote', 'br',
4+
'center', 'cite', 'code',
5+
'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'embed', 'font',
6+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'ins',
7+
'kbd', 'li', 'object', 'ol', 'param', 'pre', 'p', 'q',
8+
's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup',
9+
'table', 'tbody', 'td', 'th', 'thead', 'tr', 'tt', 'ul', 'u',
10+
'var', 'wbr',
11+
],
12+
'attributes': [
13+
'align', 'alt', 'border', 'cite', 'class', 'dir',
14+
'height', 'href', 'id', 'src', 'style', 'title', 'type', 'width',
15+
'face', 'size', # font tags
16+
'salign', 'align', 'wmode', 'target',
17+
],
18+
'styles': [
19+
'top', 'left', 'width', 'height', 'position',
20+
'background', 'font-size', 'text-align', 'z-index',
21+
'list-style',
22+
]
23+
}

mfr/extensions/md/static/js/md.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var bootstrapTable = function(md) {
55
};
66

77
var markdown = new MarkdownIt('commonmark', {
8-
html: false,
8+
html: true,
99
linkify: true
1010
})
1111
.use(window.markdownitToc)
@@ -18,4 +18,5 @@ var markdown = new MarkdownIt('commonmark', {
1818
.use(bootstrapTable)
1919
.disable('strikethrough');
2020

21-
document.getElementById("jaxified").innerHTML = markdown.render(document.getElementById("jaxified").innerHTML)
21+
22+
document.getElementById("jaxified").innerHTML = markdown.render(document.getElementById("jaxified").innerHTML);

mfr/extensions/md/templates/viewer.mako

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,300' rel='stylesheet' type='text/css'>
2-
3-
42
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
5-
63
<link type="text/css" rel="stylesheet" href="${base}/css/highlightjs-default.css">
7-
84
<link rel="stylesheet" href="${base}/css/default.css">
95

6+
<div class="mfrViewer" id="jaxified">
7+
${body}
8+
</div>
9+
1010
<script type="text/x-mathjax-config">
1111
MathJax.Hub.Config({
1212
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true},
@@ -18,13 +18,9 @@
1818
<script type="text/javascript"
1919
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
2020
></script>
21-
2221
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
2322

24-
<div class="mfrViewer" id="jaxified">
25-
${body | h}
26-
</div>
27-
23+
<!-- Order matters here -->
2824
<script src="/static/js/mfr.js"></script>
2925
<script src="/static/js/mfr.child.js"></script>
3026
<script src="${base}/js/markdown-it.min.js"></script>

0 commit comments

Comments
 (0)