|
1 | | -const path = require('path'); |
| 1 | +const { fs, path, hash } = require('@micro-app/shared-utils'); |
2 | 2 |
|
3 | 3 | // 初始化默认值 |
4 | 4 | const initBlogConfig = require('./init'); |
@@ -58,20 +58,36 @@ module.exports = (optins = {}, ctx) => { |
58 | 58 | return extendPageData($page, ctx); |
59 | 59 | }, |
60 | 60 |
|
| 61 | + // 主要处理 excerptKey 的 md 渲染 |
61 | 62 | async clientDynamicModules() { |
62 | 63 | const code = `export default {\n${(await Promise.all(pages |
63 | 64 | .filter(({ excerptKey }) => !!excerptKey) |
64 | 65 | .map(async ({ excerptKey, _filePath, _content }) => { |
65 | 66 | const dir = path.dirname(_filePath); |
66 | 67 | const { parseFrontmatter } = require('@vuepress/shared-utils'); |
67 | 68 | const { excerpt } = parseFrontmatter(_content); |
68 | | - const tempPath = path.join(ctx.tempPath, 'temp-excerpts'); |
| 69 | + const collectMap = {}; |
69 | 70 | const newExcerpt = excerpt.replace(/\[.*\]\(\..+\)/igm, function(word) { // 修复所有相对路径 |
70 | | - const p = word.replace(/\[.*\]\(/, '').replace(/\)$/, ''); |
| 71 | + const p = word.replace(/\[.*\]\(/, '').replace(/\)$/, '') |
| 72 | + .replace(/[\"|\'].+[\"|\']/, '') |
| 73 | + .trim(); |
71 | 74 | 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; |
74 | 82 | }); |
| 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 | + } |
75 | 91 | const excerptTempFilePath = await ctx.writeTemp(`temp-excerpts/${excerptKey}.md`, newExcerpt); |
76 | 92 | return ` ${JSON.stringify(excerptKey)}: () => import(${JSON.stringify(excerptTempFilePath)})`; |
77 | 93 | }))) |
|
0 commit comments