forked from CenterForOpenScience/markdown-it-atrules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
101 lines (85 loc) · 5.36 KB
/
test.js
File metadata and controls
101 lines (85 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint func-names: ["error", "never"] */
var assert = require('assert');
var md;
var renderedHtml;
var id;
function getMfrId(html) {
return html.split('"')[1];
}
function getMfrUrl(assetID) {
return 'https://mfr.osf.io/render?url=https://osf.io/' + assetID + '/?action=download%26mode=render';
}
// Because the mfr iframe requires a random id these tests cannot be part of
// the markdown-it-testgen fixture
describe('markdown-it-atrules', function () {
md = require('markdown-it')({
html: true,
linkify: true,
typography: true,
}).use(require('..'), {
type: 'osf',
pattern: /^http(?:s?):\/\/(?:www\.)?[a-zA-Z0-9 .:]{1,}\/render\?url=http(?:s?):\/\/[a-zA-Z0-9 .:]{1,}\/([a-zA-Z0-9]{5})\/\?action=download|(^[a-zA-Z0-9]{5}$)/,
format(assetID) {
var cssId = '__markdown-it-atrules-' + (new Date()).getTime();
return '<div id="' + cssId + '" class="mfr mfr-file"></div>'
+ '<script>$(document).ready(function () {new mfr.Render("' + cssId + '", "' + getMfrUrl(assetID) + '"); }); </script>';
},
});
it('fails with bad guid', function () {
renderedHtml = md.render('@[osf](2manychars)');
assert.equal(renderedHtml, '<p>@<a href="2manychars">osf</a></p>\n');
});
it('fails with bad non osf link', function () {
renderedHtml = md.render('@[osf](https://google.com )');
assert.equal(renderedHtml, '<p>@<a href="https://google.com">osf</a></p>\n');
});
it('fails when empty', function () {
renderedHtml = md.render('@[osf]()');
assert.equal(renderedHtml, '<p>@<a href="">osf</a></p>\n');
});
it('generates iframe properly with guid', function () {
renderedHtml = md.render('@[osf](xxxxx)');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with guid and line break', function () {
renderedHtml = md.render('@[osf](xxxxx\n)');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with guid and extra space', function () {
renderedHtml = md.render('@[osf](xxxxx )');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with guid and two extra spaces', function () {
renderedHtml = md.render('@[osf]( xxxxx )');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with link', function () {
renderedHtml = md.render('@[osf](https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render)');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with link and extra space', function () {
renderedHtml = md.render('@[osf](https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render )');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with link and two extra spaces', function () {
renderedHtml = md.render('@[osf](https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render )');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates iframe properly with local ip', function () {
renderedHtml = md.render('@[osf](http://localhost:7778/render?url=http://192.168.168.167:5000/xxxxx/?action=download%26mode=render)');
id = getMfrId(renderedHtml);
assert.equal(renderedHtml, '<p><div id="' + id + '" class="mfr mfr-file"></div><script>$(document).ready(function () {new mfr.Render("' + id + '", "https://mfr.osf.io/render?url=https://osf.io/xxxxx/?action=download%26mode=render"); }); </script></p>\n');
});
it('generates embed followed by formatted markdown', function () {
renderedHtml = md.render('@[osf](abc12)\n*foo*');
id = getMfrId(renderedHtml);
assert.ok(renderedHtml.indexOf(id) >= 0);
});
});