Skip to content

Commit 26e5b64

Browse files
MFR Markdown update + Mathjax
Updating the markdown renderer to use markdown-it instead of original markdown. Upgrade bleach
1 parent a8b135f commit 26e5b64

File tree

6 files changed

+25892
-10
lines changed

6 files changed

+25892
-10
lines changed

mfr/extensions/md/render.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import os
22

3-
import markdown
4-
from markdown.extensions import Extension
3+
import bleach
4+
# import markdown
5+
# from markdown.extensions import Extension
56

67
from mako.lookup import TemplateLookup
78

89
from mfr.core import extension
910

1011

11-
class EscapeHtml(Extension):
12-
def extendMarkdown(self, md, md_globals):
13-
del md.preprocessors['html_block']
14-
del md.inlinePatterns['html']
12+
# class EscapeHtml(Extension):
13+
# def extendMarkdown(self, md, md_globals):
14+
# del md.preprocessors['html_block']
15+
# del md.inlinePatterns['html']
1516

1617

1718
class MdRenderer(extension.BaseRenderer):
@@ -23,12 +24,12 @@ class MdRenderer(extension.BaseRenderer):
2324

2425
def __init__(self, *args, **kwargs):
2526
super().__init__(*args, **kwargs)
26-
self.metrics.add('markdown_version', markdown.version)
27+
# self.metrics.add('markdown_version', markdown.version)
2728

2829
def render(self):
2930
"""Render a markdown file to html."""
3031
with open(self.file_path, 'r') as fp:
31-
body = markdown.markdown(fp.read(), extensions=[EscapeHtml()])
32+
body = bleach.clean(fp.read())
3233
return self.TEMPLATE.render(base=self.assets_url, body=body)
3334

3435
@property

mfr/extensions/md/templates/md.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* This file is not run, but is used to build static/js/mdbundle.js which is then served in viewer.mako
2+
You need browserify to build the bundle. In order to build it, you need to npm install the proper packages.
3+
4+
npm install -g browserify
5+
npm install markdown-it
6+
npm install @centerforopenscience/markdown-it-toc
7+
npm install markdown-it-highlightjs
8+
npm install markdown-it-ins-del
9+
npm install markdown-it-sanitizer
10+
npm install markdown-it-mathjax
11+
12+
Once all these are installed, invoke this command from the top level directory
13+
14+
//browserify mfr/extensions/md/templates/md.js -o mfr/server/static/js/mdbundle.js
15+
This will build the bundle. viewer.mako will automattically use the new file if it in the right directory
16+
17+
*/
18+
19+
20+
var MarkdownIt = require('markdown-it');
21+
22+
var bootstrapTable = function(md) {
23+
md.renderer.rules.table_open = function() { return '<table class="table">'; };
24+
};
25+
26+
var markdown = new MarkdownIt('commonmark', {
27+
html: false,
28+
})
29+
// .use(require('markdown-it-video'))
30+
.use(require('@centerforopenscience/markdown-it-toc'))
31+
.use(require('markdown-it-sanitizer'))
32+
.use(require('markdown-it-ins-del'))
33+
.enable('table')
34+
.use(bootstrapTable)
35+
.use(require('markdown-it-highlightjs'), {auto: true, code: true})
36+
.use(require('markdown-it-mathjax')())
37+
.disable('strikethrough');
38+
39+
document.getElementById("jaxified").innerHTML = markdown.render(document.getElementById("jaxified").innerHTML)
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,300,700" rel="stylesheet" type="text/css">
2-
<link rel="stylesheet" href="static/css/default.css">
3-
<div style="word-wrap: break-word;" class="mfrViewer">
2+
<link type="text/css" rel="stylesheet" href="/static/css/default.css">
3+
<link type="text/css" rel="stylesheet" href="/static/css/highlightjs-default.css">
4+
5+
<script type="text/x-mathjax-config">
6+
MathJax.Hub.Config({
7+
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true},
8+
// Don't automatically typeset the whole page. Must explicitly use MathJax.Hub.Typeset
9+
messageStyle: "none",
10+
skipStartupTypeset: true
11+
});
12+
</script>
13+
14+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_CHTML"></script>
15+
16+
<div style="word-wrap: break-word;" class="mfrViewer" id="jaxified">
417
${body}
518
</div>
619

720
<script src="/static/js/mfr.js"></script>
821
<script src="/static/js/mfr.child.js"></script>
22+
<script src="/static/js/mdbundle.js"></script>
23+
24+
<script>
25+
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"jaxified"]);
26+
</script>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
3+
Original highlight.js style (c) Ivan Sagalaev <[email protected]>
4+
5+
*/
6+
7+
.hljs {
8+
display: block;
9+
overflow-x: auto;
10+
padding: 0.5em;
11+
background: #F0F0F0;
12+
}
13+
14+
15+
/* Base color: saturation 0; */
16+
17+
.hljs,
18+
.hljs-subst {
19+
color: #444;
20+
}
21+
22+
.hljs-comment {
23+
color: #888888;
24+
}
25+
26+
.hljs-keyword,
27+
.hljs-attribute,
28+
.hljs-selector-tag,
29+
.hljs-meta-keyword,
30+
.hljs-doctag,
31+
.hljs-name {
32+
font-weight: bold;
33+
}
34+
35+
36+
/* User color: hue: 0 */
37+
38+
.hljs-type,
39+
.hljs-string,
40+
.hljs-number,
41+
.hljs-selector-id,
42+
.hljs-selector-class,
43+
.hljs-quote,
44+
.hljs-template-tag,
45+
.hljs-deletion {
46+
color: #880000;
47+
}
48+
49+
.hljs-title,
50+
.hljs-section {
51+
color: #880000;
52+
font-weight: bold;
53+
}
54+
55+
.hljs-regexp,
56+
.hljs-symbol,
57+
.hljs-variable,
58+
.hljs-template-variable,
59+
.hljs-link,
60+
.hljs-selector-attr,
61+
.hljs-selector-pseudo {
62+
color: #BC6060;
63+
}
64+
65+
66+
/* Language color: hue: 90; */
67+
68+
.hljs-literal {
69+
color: #78A960;
70+
}
71+
72+
.hljs-built_in,
73+
.hljs-bullet,
74+
.hljs-code,
75+
.hljs-addition {
76+
color: #397300;
77+
}
78+
79+
80+
/* Meta color: hue: 200 */
81+
82+
.hljs-meta {
83+
color: #1f7199;
84+
}
85+
86+
.hljs-meta-string {
87+
color: #4d99bf;
88+
}
89+
90+
91+
/* Misc effects */
92+
93+
.hljs-emphasis {
94+
font-style: italic;
95+
}
96+
97+
.hljs-strong {
98+
font-weight: bold;
99+
}

mfr/server/static/js/mdbundle.js

Lines changed: 25724 additions & 0 deletions
Large diffs are not rendered by default.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ xlrd==1.0.0
4444

4545
# Md
4646
markdown==2.6.2
47+
bleach==2.0.0
4748

4849
# Issue: certifi-2015.9.6.1 and 2015.9.6.2 fail verification (https://github.com/certifi/python-certifi/issues/26)
4950
certifi==2015.4.28

0 commit comments

Comments
 (0)