Skip to content

Commit 3e72b69

Browse files
committed
Update .gitignore, README.md, comments,
docstrs and style. [skip ci]
1 parent 4af4eb5 commit 3e72b69

File tree

7 files changed

+93
-87
lines changed

7 files changed

+93
-87
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Thumbs.db
1111
*.swp
1212
*~
1313

14+
# Node Modules
15+
######################
16+
/node_modules/
17+
18+
# Asynchronous HTTP
19+
######################
20+
/src/
21+
1422
# R
1523
######################
1624
.Rhistory

mfr/extensions/md/README.md

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1+
## Using Markdown-it with plugins
12

2-
## Markdown plus Mathjax readme
3-
4-
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.
5-
6-
to get all the libraries needed:
7-
Note: You do not need to use npm, you can easily get them off of github.
3+
If we had `npm`, here were the would-be configuration.
84

95
```bash
106
@@ -15,38 +11,32 @@ npm install [email protected]
1511
npm install [email protected]
1612
```
1713

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-
26-
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`.
27-
28-
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.
29-
30-
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:
31-
32-
```javascript
33-
;(function (root, factory) {
34-
if (typeof exports === 'object') {
35-
module.exports = factory()
36-
} else {
37-
root.<PLUGIN_NAME> = factory()
38-
}
39-
})(this, function () {
40-
41-
42-
return function(md){
43-
44-
.....
45-
}
46-
47-
})
48-
```
49-
50-
And then modify it to work in this context. See other plugins for examples.
51-
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`.
14+
For MFR, a customized local copy of each script is stored in the extension's static folder. There are a few issues:
15+
16+
* ES5 compatibility: use [Babel](https://babeljs.io/repl/) to convert ES6 `markdown-it-highlightjs` to ES5.
17+
18+
* `require` is NOT available. For `md.js` to be able to load these libraries, customization is necessary to export via `root.<PLUGIN_NAME>`.
19+
* `markdown-it` and `markdown-it-sanitizer` are already set up to be exported code. We load the `min` version directly.
20+
* `markdown-it-toc`, `markdown-it-highlightjs`, `markdown-it-ins-del` and `markdown-it-mathjax` are not. Here is the wrapper:
21+
```javascript
22+
(function (root, factory) {
23+
if (typeof exports === "object") {
24+
module.exports = factory();
25+
} else {
26+
root.<PLUGIN_NAME> = factory();
27+
}
28+
}) (this, function () {
29+
return function(md/*, optional arguments*/) {
30+
/* library code */
31+
}
32+
});
33+
```
34+
35+
Here is a list of original copy of the scripts we use:
36+
37+
* [markdown-it@08.4.0](https://github.com/markdown-it/markdown-it/blob/8.4.0/bin/markdown-it.js)
38+
* [markdown-it-sanitizer@0.4.3](https://github.com/svbergerem/markdown-it-sanitizer/blob/v0.4.3/dist/markdown-it-sanitizer.min.js)
39+
* [markdown-it-mathjax@2.0.0](https://github.com/classeur/markdown-it-mathjax/blob/v2.0.0/markdown-it-mathjax.js)
40+
* [markdown-it-toc@1.1.1](https://github.com/cos-forks/markdown-it-toc/blob/1.1.1/index.js)
41+
* [markdown-it-ins-del@1.0.0](https://github.com/brianjgeiger/markdown-it-ins-del/blob/1.0.0/index.js)
42+
* [markdown-it-higlightjs@3.0.0](https://github.com/cslzchen/markdown-it-highlightjs/blob/release/3.0.0/index.es5.js)

mfr/extensions/md/render.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import bleach
44
from mako.lookup import TemplateLookup
55

6-
from mfr.core import extension
7-
from mfr.extensions.md.settings import BLEACH_WHITELIST
6+
from mfr.core import extension as core_extension
7+
from mfr.extensions.md import settings as md_settings
88

9-
class MdRenderer(extension.BaseRenderer):
9+
10+
class MdRenderer(core_extension.BaseRenderer):
1011

1112
TEMPLATE = TemplateLookup(
1213
directories=[
@@ -19,9 +20,7 @@ def __init__(self, *args, **kwargs):
1920
def render(self):
2021
"""Render a markdown file to html."""
2122
with open(self.file_path, 'r') as fp:
22-
# Bleach will bug out on unclosed tags. OSF wiki does not have this issue.
23-
# This is due to versioning problems: https://github.com/mozilla/bleach/issues/271
24-
body = bleach.clean(fp.read(), **BLEACH_WHITELIST)
23+
body = bleach.clean(fp.read(), **md_settings.BLEACH_WHITELIST)
2524
return self.TEMPLATE.render(base=self.assets_url, body=body)
2625

2726
@property

mfr/extensions/md/settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# This list comes from the OSF
1+
# The bleach whitelist comes from OSF Wiki whitelist:
2+
# https://github.com/CenterForOpenScience/osf.io/blob/develop/website/settings/defaults.py
3+
24
BLEACH_WHITELIST = {
35
'tags': [
46
'a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'blockquote', 'br',
@@ -13,7 +15,7 @@
1315
'attributes': [
1416
'align', 'alt', 'border', 'cite', 'class', 'dir',
1517
'height', 'href', 'id', 'src', 'style', 'title', 'type', 'width',
16-
'face', 'size', # font tags
18+
'face', 'size',
1719
'salign', 'align', 'wmode', 'target',
1820
],
1921
'styles': [
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
@charset "utf-8";
22

3-
4-
/*Had quite a few css woes on this one
5-
Chrome has some scroll bar issues. This is mostly due to
6-
Iframe problems. There may be a better combination of CSS to get rid of this,
7-
for now just going the best combination found.
8-
9-
*/
103
.mfrViewer {
114
font-family: 'Open Sans';
125
padding:1em;
136
background:#fefefe;
147
height: auto;
158
word-wrap: break-word;
169
}
17-
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
/*
2-
3-
Original highlight.js style (c) Ivan Sagalaev <[email protected]>
4-
5-
*/
1+
/**
2+
* Original highlight.js style (c) Ivan Sagalaev <[email protected]>
3+
*
4+
* https://github.com/isagalaev/highlight.js/blob/9.12.0/src/styles/default.css
5+
*/
66

77
.hljs {
8-
display: block;
9-
overflow-x: auto;
10-
padding: 0.5em;
11-
background: #F0F0F0;
8+
display: block;
9+
overflow-x: auto;
10+
padding: 0.5em;
11+
background: #F0F0F0;
1212
}
1313

1414

1515
/* Base color: saturation 0; */
1616

1717
.hljs,
1818
.hljs-subst {
19-
color: #444;
19+
color: #444;
2020
}
2121

2222
.hljs-comment {
23-
color: #888888;
23+
color: #888888;
2424
}
2525

2626
.hljs-keyword,
@@ -29,7 +29,7 @@ Original highlight.js style (c) Ivan Sagalaev <[email protected]>
2929
.hljs-meta-keyword,
3030
.hljs-doctag,
3131
.hljs-name {
32-
font-weight: bold;
32+
font-weight: bold;
3333
}
3434

3535

@@ -43,13 +43,13 @@ Original highlight.js style (c) Ivan Sagalaev <[email protected]>
4343
.hljs-quote,
4444
.hljs-template-tag,
4545
.hljs-deletion {
46-
color: #880000;
46+
color: #880000;
4747
}
4848

4949
.hljs-title,
5050
.hljs-section {
51-
color: #880000;
52-
font-weight: bold;
51+
color: #880000;
52+
font-weight: bold;
5353
}
5454

5555
.hljs-regexp,
@@ -59,41 +59,41 @@ Original highlight.js style (c) Ivan Sagalaev <[email protected]>
5959
.hljs-link,
6060
.hljs-selector-attr,
6161
.hljs-selector-pseudo {
62-
color: #BC6060;
62+
color: #BC6060;
6363
}
6464

6565

6666
/* Language color: hue: 90; */
6767

6868
.hljs-literal {
69-
color: #78A960;
69+
color: #78A960;
7070
}
7171

7272
.hljs-built_in,
7373
.hljs-bullet,
7474
.hljs-code,
7575
.hljs-addition {
76-
color: #397300;
76+
color: #397300;
7777
}
7878

7979

8080
/* Meta color: hue: 200 */
8181

8282
.hljs-meta {
83-
color: #1f7199;
83+
color: #1f7199;
8484
}
8585

8686
.hljs-meta-string {
87-
color: #4d99bf;
87+
color: #4d99bf;
8888
}
8989

9090

9191
/* Misc effects */
9292

9393
.hljs-emphasis {
94-
font-style: italic;
94+
font-style: italic;
9595
}
9696

9797
.hljs-strong {
98-
font-weight: bold;
98+
font-weight: bold;
9999
}

tests/extensions/md/test_renderer.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_md_file_path():
2020
def invalid_md_file_path():
2121
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files', 'invalid.md')
2222

23+
2324
@pytest.fixture
2425
def url():
2526
return 'http://osf.io/file/test.md'
@@ -49,11 +50,25 @@ def test_render_md_cache_result(self, renderer):
4950
assert renderer.cache_result is True
5051

5152
def test_render_md(self, test_md_file_path, assets_url, export_url):
52-
metadata = ProviderMetadata('test', '.md', 'text/plain',
53-
'1234', 'http://wb.osf.io/file/test.md?token=1234')
54-
renderer = MdRenderer(metadata, test_md_file_path, url, assets_url, export_url)
55-
body = renderer.render()
56-
# Harder to test now that the markdown is parsed javascript side
57-
inbody = 'The rain---not the reign---in\nSpain.'
58-
'\n\n&lt;script&gt;\nalert("Hello world");\n&lt;/script&gt;'
59-
assert inbody in body
53+
54+
metadata = ProviderMetadata(
55+
'test',
56+
'.md',
57+
'text/plain',
58+
'1234',
59+
'http://wb.osf.io/file/test.md?token=1234'
60+
)
61+
62+
md_renderer = MdRenderer(metadata, test_md_file_path, url, assets_url, export_url)
63+
body = md_renderer.render()
64+
in_body = 'The rain---not the reign---in\nSpain.\n\n&lt;script&gt;\nalert("Hello world");\n&lt;/script&gt;'
65+
assert in_body in body
66+
67+
# TODO: it is hard to test since rendering is performed by front-end javascript, if possible test the following:
68+
# TODO: test bleach (python)
69+
# TODO: test invalid/corrupted file (e.g. when bleach fails parsing the file)
70+
# TODO: test markdown-it
71+
# TODO: test toc
72+
# TODO: test highlight
73+
# TODO: test mathjax
74+
# TODO: test sanitizer

0 commit comments

Comments
 (0)