This repository was archived by the owner on Feb 28, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 2.17 KB
/
Copy pathindex.js
File metadata and controls
66 lines (53 loc) · 2.17 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
'use strict';
const path = require('path');
// cache permalink for each post
const cachedPost = {};
hexo.extend.filter.register('before_post_render', function(data){
const filePath = path.normalize(data.source.replace('_posts/', ''));
cachedPost[filePath] = data.permalink;
return data;
}, 20);
// update links in post
hexo.extend.filter.register('before_post_render', function(data){
let currentPostPath = data.source.replace('_posts/', '');
data.content = updateLinksInPost(data.content, currentPostPath);
return data;
}, 21);
function updateLinksInPost(postContent, currentPostPath) {
// regex matching "[name](link)"
const regex = /\[([^\]]+)\]\(([^)]+)\)/g;
const replacements = [];
let match;
while ((match = regex.exec(postContent)) !== null) {
const [fullMatch, name, link] = match;
// Check if link ends with ".md" or contains ".md#", and does not contain "://"
// These links should be processed, as they points to another post
if ((/\.md(#|$)/.test(link)) && !/:\//.test(link)) {
// split the link (like "../github/introduction.md#History")
const [relativePath, anchor = ''] = link.split("#");
// resolve the relative link path against current directory
const currentDir = path.dirname(currentPostPath);
const targetPostPath = path.normalize(path.join(currentDir, relativePath));
// Is this "targetPostPath" actually points to an existing post?
const targetPermalink = cachedPost[targetPostPath]
if(!targetPermalink) {
continue;
}
// reconstruct the new link
const newLink = `${targetPermalink}${anchor ? "#" + anchor : ""}`;
const newMarkdownLink = `[${name}](${newLink})`;
replacements.push({
start: match.index,
end: match.index + fullMatch.length,
replacement: newMarkdownLink,
});
hexo.log.i(`Replace link ${fullMatch} --> ${newMarkdownLink} in post [${currentPostPath}]`)
}
}
// apply replacements in reverse order to avoid index shifting
replacements.reverse().forEach(({ start, end, replacement }) => {
postContent =
postContent.slice(0, start) + replacement + postContent.slice(end);
});
return postContent;
}