forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveRelativeSrc.js
More file actions
executable file
·56 lines (40 loc) · 1.29 KB
/
Copy pathresolveRelativeSrc.js
File metadata and controls
executable file
·56 lines (40 loc) · 1.29 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
'use strict';
/**
* Replaces relative img src with resourceRoot/src
*/
const tokenUtils = require('../utils/token');
module.exports = function(md) {
md.core.ruler.push('resolve_relative_src', function(state) {
let methods = {
link_open,
image
};
for (let idx = 0; idx < state.tokens.length; idx++) {
let token = state.tokens[idx];
if (token.type !== 'inline') continue;
for (let i = 0; i < token.children.length; i++) {
let inlineToken = token.children[i];
if (methods[inlineToken.type]) {
methods[inlineToken.type](inlineToken);
}
}
}
function image(imgToken) {
let src = tokenUtils.attrGet(imgToken, 'src');
if (src.indexOf('://') == -1 && src[0] != '/') {
src = state.md.options.resourceWebRoot + '/' + src;
tokenUtils.attrReplace(imgToken, 'src', src);
}
}
function link_open(token) {
let href = tokenUtils.attrGet(token, 'href');
// don't touch info:tutorial link protocol
// don't touch absolute links
// don't touch in-page #hash
if (!href.match(/^\w+:/) && href[0] != '/' && href[0] != '#') {
href = state.md.options.resourceWebRoot + '/' + href;
tokenUtils.attrReplace(token, 'href', href);
}
}
});
};