Skip to content

Commit 9aa897b

Browse files
committed
Render MD files at the frontend, similar to OSF
- MFR frontend makes XHR to fetch the raw content directly from WB with CORS enforced. Markdownit and its plugins sanitize and render the content before updating the MFR viewer.
1 parent 8292825 commit 9aa897b

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,74 @@
1-
<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">
4-
${body}
5-
</div>
1+
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,300' rel='stylesheet' type='text/css'>
2+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
3+
<link type="text/css" rel="stylesheet" href="${base}/css/highlightjs-default.css">
4+
<link rel="stylesheet" href="${base}/css/default.css">
5+
6+
## Quirks:
7+
##
8+
## ``markdownit`` and its plugins take in teh raw MD content and outputs renderable HTML that can be
9+
## directly embedded. However, for security conerns, MFR mustn't put the raw content onto the page.
10+
## It uses XMLHttpRequest to fetch the file content from WB and let ``markdownit`` santize and parse
11+
## it into the renderable HTML.
12+
##
13+
## Cross Origin Resource Sharing (CORS) is enforced for the XMLHttpRequest. One one hand, WB has
14+
## already been configured to respond with header "Access-Control-Allow-Origin", which allows the
15+
## MFR domain. On the other hand, OSF hasn't. This is why the request must goes to WB directly.
16+
##
17+
<div class="mfrViewer" id="mdViewer"></div>
18+
19+
<script type="text/x-mathjax-config">
20+
MathJax.Hub.Config({
21+
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true},
22+
messageStyle: "none",
23+
skipStartupTypeset: true
24+
});
25+
</script>
26+
27+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
28+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
629

730
<script src="/static/js/mfr.js"></script>
831
<script src="/static/js/mfr.child.js"></script>
32+
33+
## Note Loading markdown-it.min.js from CDN does not work
34+
<script src="${base}/js/markdown-it.min.js"></script>
35+
<script src="${base}/js/markdown-it-mathjax.js"></script>
36+
<script src="${base}/js/markdown-it-sanitizer.min.js"></script>
37+
<script src="${base}/js/markdown-it-highlightjs.js"></script>
38+
<script src="${base}/js/markdown-it-ins-del.js"></script>
39+
<script src="${base}/js/markdown-it-toc.js"></script>
40+
41+
<script>
42+
43+
## How to load ``markdown-it``: https://github.com/markdown-it/markdown-it#simple
44+
var MarkdownIt = window.markdownit;
45+
46+
var bootstrapTable = function(md) {
47+
md.renderer.rules.table_open = function() { return "<table class=\"table\">"; };
48+
};
49+
50+
var markdown = new MarkdownIt("commonmark", {html: true, linkify: true})
51+
.use(window.markdownitToc)
52+
.use(window.markdownitIns)
53+
.use(window.markdownitHightlightjs)
54+
.use(window.markdownitSanitizer)
55+
.use(window.markdownitMathjax())
56+
.enable("table")
57+
.enable("linkify")
58+
.use(bootstrapTable)
59+
.disable("strikethrough");
60+
61+
var wb_request = new XMLHttpRequest();
62+
var wb_download_url = "${url}";
63+
wb_request.open("GET", wb_download_url);
64+
wb_request.responseType = "text";
65+
wb_request.withCredentials = true;
66+
wb_request.onload = function () {
67+
document.getElementById("mdViewer").innerHTML = markdown.render(wb_request.response);
68+
## Force the host to resize
69+
window.pymChild.sendHeight();
70+
};
71+
wb_request.send();
72+
</script>
73+
74+
<script>MathJax.Hub.Queue(["Typeset",MathJax.Hub,"jaxified"]);</script>

0 commit comments

Comments
 (0)