-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
32 lines (25 loc) · 825 Bytes
/
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
'use strict';
const cachedPost = {};
let lastPost;
hexo.extend.filter.register('post_permalink', function(data){
lastPost = data;
return data;
}, 1);
hexo.extend.filter.register('post_permalink', function(permalink) {
if (lastPost) {
const fileName = lastPost.source.replace('_posts/', '');
cachedPost[fileName] = permalink;
}
return permalink;
}, 25);
hexo.extend.filter.register("after_render:html", (str) => {
const re = /<a[^>]*href[=\"\'\s]+([^\"\']*)[\"\']?[^>]*>/g;
return str.replace(re, function(p1, p2) {
const fileName = p2.replace(/..\/|.\//g, '').replace(/.md#[\w]+/, '.md');
if (cachedPost[fileName]) {
const toBeReplacedContent = p2.replace(/.md#[\w]+/, '.md');
return p1.replace(toBeReplacedContent, `/${cachedPost[fileName]}`);
}
return p1;
});
});