Skip to content

Commit f95cf0c

Browse files
committed
fix: 修复中文路径问题
1 parent 49558aa commit f95cf0c

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

theme/plugins/blog/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require('path');
1+
const { fs, path, hash } = require('@micro-app/shared-utils');
22

33
// 初始化默认值
44
const initBlogConfig = require('./init');
@@ -58,20 +58,36 @@ module.exports = (optins = {}, ctx) => {
5858
return extendPageData($page, ctx);
5959
},
6060

61+
// 主要处理 excerptKey 的 md 渲染
6162
async clientDynamicModules() {
6263
const code = `export default {\n${(await Promise.all(pages
6364
.filter(({ excerptKey }) => !!excerptKey)
6465
.map(async ({ excerptKey, _filePath, _content }) => {
6566
const dir = path.dirname(_filePath);
6667
const { parseFrontmatter } = require('@vuepress/shared-utils');
6768
const { excerpt } = parseFrontmatter(_content);
68-
const tempPath = path.join(ctx.tempPath, 'temp-excerpts');
69+
const collectMap = {};
6970
const newExcerpt = excerpt.replace(/\[.*\]\(\..+\)/igm, function(word) { // 修复所有相对路径
70-
const p = word.replace(/\[.*\]\(/, '').replace(/\)$/, '');
71+
const p = word.replace(/\[.*\]\(/, '').replace(/\)$/, '')
72+
.replace(/[\"|\'].+[\"|\']/, '')
73+
.trim();
7174
const op = path.resolve(dir, p);
72-
const a = word.replace(p, path.relative(tempPath, op));
73-
return a;
75+
if (fs.existsSync(op)) { // 存在收集
76+
const name = hash(op);
77+
const ext = path.extname(op);
78+
collectMap[`${name}${ext}`] = op;
79+
return word.replace(p, `./${excerptKey}/${name}${ext}`); // 替换
80+
}
81+
return word;
7482
});
83+
const arrs = Object.keys(collectMap);
84+
const tempPath = path.join(ctx.tempPath, 'temp-excerpts', excerptKey);
85+
if (arrs.length) { // 迁移
86+
fs.ensureDirSync(tempPath);
87+
arrs.forEach(key => {
88+
fs.copySync(collectMap[key], path.join(tempPath, key));
89+
});
90+
}
7591
const excerptTempFilePath = await ctx.writeTemp(`temp-excerpts/${excerptKey}.md`, newExcerpt);
7692
return ` ${JSON.stringify(excerptKey)}: () => import(${JSON.stringify(excerptTempFilePath)})`;
7793
})))

0 commit comments

Comments
 (0)