Skip to content

Commit cdcdebd

Browse files
committed
Use markdownit to render MD files on frontend only
- MFR uses XHR to fetch MD files directly from WB - Markdownit sanitizes and parses the raw content before dispalying it in the MFR Viewer
1 parent 76bc13c commit cdcdebd

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

mfr/extensions/md/templates/viewer.mako

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
<link type="text/css" rel="stylesheet" href="${base}/css/highlightjs-default.css">
44
<link rel="stylesheet" href="${base}/css/default.css">
55

6-
<div class="mfrViewer" id="jaxified">
7-
${body}
8-
</div>
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>
918

1019
<script type="text/x-mathjax-config">
1120
MathJax.Hub.Config({
@@ -18,15 +27,48 @@
1827
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
1928
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
2029

21-
<!-- Order MATTERS! -->
2230
<script src="/static/js/mfr.js"></script>
2331
<script src="/static/js/mfr.child.js"></script>
24-
<script src="${base}/js/markdown-it.min.js"></script> <!-- Note Loading markdown-it.min.js from CDN does not work -->
32+
33+
## Note Loading markdown-it.min.js from CDN does not work
34+
<script src="${base}/js/markdown-it.min.js"></script>
2535
<script src="${base}/js/markdown-it-mathjax.js"></script>
2636
<script src="${base}/js/markdown-it-sanitizer.min.js"></script>
2737
<script src="${base}/js/markdown-it-highlightjs.js"></script>
2838
<script src="${base}/js/markdown-it-ins-del.js"></script>
2939
<script src="${base}/js/markdown-it-toc.js"></script>
30-
<script src="${base}/js/md.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>
3173

3274
<script>MathJax.Hub.Queue(["Typeset",MathJax.Hub,"jaxified"]);</script>

0 commit comments

Comments
 (0)