forked from elmasse/hyper-markdown-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.js
54 lines (41 loc) · 1.12 KB
/
markdown.js
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
const Remarkable = require('remarkable');
const hljs = require('highlight.js');
const md = new Remarkable({
html: true,
linkify: true,
typographer: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value;
} catch (err) {}
}
try {
return hljs.highlightAuto(str).value;
} catch (err) {}
return ''; // use external default escaping
}
});
const imageRender = (options, imgFn) => (tokens, idx, opts /*, env */) => {
const src = tokens[idx].src;
const isLocal = !src.match(/^http|file/);
if (isLocal) {
tokens[idx].src = createLocalUri(options.cwd, src);
}
return imgFn(tokens, idx, opts /*, env */);
}
const createLocalUri = (cwd, src) => {
let uri = src;
if (src.match(/^\./)) {
uri = src.substring(1);
}
if (!src.match(/^\//)) {
uri = `/${uri}`;
}
return `file://${cwd}${uri}`;
}
exports.markdown = md;
exports.replaceLocalImagePlugin = (instance, options) => {
const imgFn = instance.renderer.rules.image;
instance.renderer.rules.image = imageRender(options, imgFn)
}