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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
const path = require('path'); const fs = require('fs'); const glob = require('glob');
let allPostsCache = null; let postsCache = [];
function getAllPosts(hexoInstance) { const posts = hexoInstance.locals.get('posts'); if (posts && posts.length > 0) { return posts.toArray().map(post => ({ source: post.source, path: post.path, permalink: post.permalink })); } return null; }
function replaceLinks(content, currentSource, config, posts) { if (!content || !posts || posts.length === 0) return null;
const normalize = p => p.replace(/\\/g, '/');
const linkRegex = /<a\s+(?:[^>]*?\s+)?href="([^"]*\.md)"([^>]*)>([\s\S]*?)<\/a>/gi;
let hasChanged = false; let newContent = content.replace(linkRegex, (match, href, attrs, text) => { if (href.includes('://')) return match;
const currentDir = path.dirname(currentSource); const decodedPath = decodeURIComponent(href); const relPath = path.normalize(path.join(currentDir, decodedPath)); const targetSourcePath = path.join(config.source_dir, relPath);
const targetPost = posts.find(p => { const postSourcePath = path.join(config.source_dir, p.source); return normalize(postSourcePath) === normalize(targetSourcePath); });
if (targetPost) { hasChanged = true; let finalUrl = targetPost.permalink || targetPost.path; if (!finalUrl.startsWith('http://') && !finalUrl.startsWith('https://')) { const siteRoot = config.root || '/'; finalUrl = siteRoot + finalUrl; }
if (!finalUrl.startsWith('http')) { finalUrl = config.url.replace(/\/$/, '') + finalUrl; }
return `<a href="${finalUrl}"${attrs}>${text}</a>`; }
return match; });
return hasChanged ? newContent : null; }
hexo.extend.filter.register('before_generate', function () { const posts = getAllPosts(this); if (posts && posts.length > 0) { allPostsCache = posts; hexo.log.info('fix-relative-links: loaded ' + allPostsCache.length + ' posts in before_generate'); } });
hexo.extend.filter.register('after_post_render', function (data) { const { config } = this;
postsCache.push({ source: data.source, path: data.path, content: data.content, permalink: data.permalink });
const posts = getAllPosts(this); if (posts && posts.length > 0) { allPostsCache = posts; }
const newContent = replaceLinks(data.content, data.source, config, allPostsCache || postsCache); if (newContent) { data.content = newContent; hexo.log.info('Updated links in after_post_render: ' + data.source); }
return data; });
hexo.extend.filter.register('before_exit', function () { const { config } = this;
let posts = getAllPosts(this);
if (!posts || posts.length < 10) { try { const hexoPosts = hexo.locals.get('posts'); if (hexoPosts && hexoPosts.length > 0) { posts = hexoPosts.toArray().map(post => ({ source: post.source, path: post.path, permalink: post.permalink })); hexo.log.info('fix-relative-links: loaded ' + posts.length + ' posts from hexo.locals'); } } catch (e) { } }
if (!posts || posts.length === 0) { if (postsCache.length > 0) { posts = postsCache.map(p => ({ source: p.source, path: p.path, permalink: p.permalink })); hexo.log.info('fix-relative-links: using ' + posts.length + ' cached posts from postsCache'); } else { return; } }
allPostsCache = posts; hexo.log.info('fix-relative-links: processing with ' + posts.length + ' posts, postsCache has ' + postsCache.length + ' items');
const htmlFiles = glob.sync('**/*.html', { cwd: this.public_dir }); let updatedCount = 0;
htmlFiles.forEach(file => { const publicPath = path.join(this.public_dir, file); if (!fs.existsSync(publicPath)) return;
let fileContent = fs.readFileSync(publicPath, 'utf-8'); if (!fileContent.includes('.md')) return;
const post = posts.find(p => p.path === file || file.endsWith(p.path)) || postsCache.find(p => p.path === file || file.endsWith(p.path));
const sourcePath = post ? post.source : ('_posts/' + path.basename(file, '.html') + '.md');
const newContent = replaceLinks(fileContent, sourcePath, config, posts);
if (newContent) { fs.writeFileSync(publicPath, newContent, 'utf-8'); hexo.log.info('Updated links in: ' + file); updatedCount++; } });
if (updatedCount > 0) { hexo.log.info('fix-relative-links: updated ' + updatedCount + ' files'); }
postsCache = []; });
|