-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (45 loc) · 1.59 KB
/
index.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
54
55
56
57
'use strict';
const rmEndSlashes = /^(.*?)\/{1,}$/g;
function combine(absolutePath, ...relativePath) {
let firstRelPath = relativePath.length ? relativePath[0] : '';
if (!firstRelPath) {
return absolutePath;
} else if (firstRelPath.startsWith("http") || firstRelPath.startsWith("https") || firstRelPath.startsWith("/")) {
return firstRelPath;
} else {
let tmp = '';
relativePath.forEach(path => {
tmp += '/' + path;
})
return absolutePath.replace(rmEndSlashes, "$1") + tmp.replace(/(\\|\/){2,}/g, '/');
}
}
let imageAbsolutePath = '';
if (hexo.config.tag_replace && hexo.config.tag_replace.image) {
imageAbsolutePath = hexo.config.tag_replace.image.absolute_path;
}
let enable = false;
if (hexo.config.tag_replace && hexo.config.tag_replace.enable) {
enable = hexo.config.tag_replace.enable;
}
if (enable === true) {
const mdImageOrLinkPattern = /\[(.*?)\]\((.*?)\)/g;
const postNamePattern = /.*?\/(.*?)\.md$/g;
hexo.extend.filter.register('before_post_render', function (data) {
if (data.source.endsWith(".md")) {
let postName = data.source.replace(postNamePattern, "$1");
// Process the image/link tag.
data.content = data.content.replace(mdImageOrLinkPattern, (s) => {
let name = s.replace(mdImageOrLinkPattern, "$1");
let path = s.replace(mdImageOrLinkPattern, "$2");
// Don't touch the absolute path.
if (path.startsWith('http')) {
return s;
} else {
return '[' + name + '](' + combine(imageAbsolutePath, postName, path) + ')';
}
});
}
return data;
});
}