Skip to content

Commit da23a06

Browse files
committed
fix(utils): 修复路径处理以支持自定义 base 路径
- 在 about 页面加载 Markdown 内容时,修正路径拼接逻辑,支持 import.meta.env.BASE_URL - folderScanner 工具中统一加入 base 路径处理,确保 fetch 请求地址正确 - markdownIndex 模块调整所有路径拼接,避免路径错误导致加载失败 - 保持向后兼容基础上增强了路径动态适配能力,支持部署在非根目录的场景 - 所有 fetch 请求均基于动态 base 路径生成,提高灵活性和可维护性
1 parent 99bc48b commit da23a06

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

src/pages/About.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export function About() {
2121
async function loadSiteDescription() {
2222
setLoading(true);
2323
try {
24+
// 获取 base 路径并构建正确的文件路径
25+
const base = import.meta.env.BASE_URL || '/';
26+
const relativePath = '/content/pages/about-site';
27+
const basePath = base === '/' ? relativePath : base + relativePath.replace(/^\//, '');
2428
// 尝试从 Markdown 文件加载
25-
const markdown = await loadI18nMarkdown('/content/pages/about-site', language);
29+
const markdown = await loadI18nMarkdown(basePath, language);
2630
setSiteMarkdown(markdown);
2731
} catch (error) {
2832
console.error('加载关于本站内容失败:', error);

src/utils/folderScanner.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* 并为每个文件夹生成相应的文档中心配置
55
*/
66

7+
// 获取 base 路径(用于 fetch 请求)
8+
const base = import.meta.env.BASE_URL || '/';
9+
710
/**
811
* 获取所有 Markdown 文件的 glob 模块
912
* 排除 files 文件夹
@@ -67,8 +70,9 @@ export function getFolderFiles(folderName) {
6770
for (const path in allMarkdownModules) {
6871
const folder = extractFolderName(path);
6972
if (folder === folderName) {
70-
// 转换为绝对路径
71-
const absolutePath = path.replace('../..', '');
73+
// 转换为绝对路径(考虑 base 路径)
74+
const relativePath = path.replace('../..', '');
75+
const absolutePath = base === '/' ? relativePath : base + relativePath.replace(/^\//, '');
7276
files.push(absolutePath);
7377
}
7478
}
@@ -92,7 +96,9 @@ export function hasIndexFile(folderName) {
9296
* @returns {Promise<Object|null>} index.md 的配置信息
9397
*/
9498
export async function loadFolderIndex(folderName) {
95-
const indexPath = `/content/${folderName}/index.md`;
99+
// 构建 index.md 路径(考虑 base 路径)
100+
const relativePath = `/content/${folderName}/index.md`;
101+
const indexPath = base === '/' ? relativePath : base + relativePath.replace(/^\//, '');
96102

97103
try {
98104
const response = await fetch(indexPath);

src/utils/markdownIndex.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { getAllMarkdownModules } from './folderScanner';
88
// 使用文件夹扫描器获取所有 Markdown 模块
99
const allModules = getAllMarkdownModules();
1010

11+
// 获取 base 路径(用于 fetch 请求)
12+
const base = import.meta.env.BASE_URL || '/';
13+
1114
// 为了向后兼容,保留原有的 posts 和 pages 模块导出
1215
const postsModules = {};
1316
const pagesModules = {};
@@ -59,8 +62,9 @@ export async function loadAllMarkdownFiles() {
5962
// 加载所有模块(包括 posts, pages, tutorials 等)
6063
for (const path in allModules) {
6164
try {
62-
// 将相对路径转换为绝对路径
63-
const absolutePath = path.replace('../..', '');
65+
// 将相对路径转换为绝对路径(考虑 base 路径)
66+
const relativePath = path.replace('../..', '');
67+
const absolutePath = base === '/' ? relativePath : base + relativePath.replace(/^\//, '');
6468
const filename = path.split('/').pop();
6569

6670
// 提取文件夹名称
@@ -147,7 +151,9 @@ export async function loadFolderMarkdownFiles(folderName) {
147151
if (folder !== folderName) continue;
148152

149153
try {
150-
const absolutePath = path.replace('../..', '');
154+
// 将相对路径转换为绝对路径(考虑 base 路径)
155+
const relativePath = path.replace('../..', '');
156+
const absolutePath = base === '/' ? relativePath : base + relativePath.replace(/^\//, '');
151157
const filename = path.split('/').pop();
152158

153159
const response = await fetch(absolutePath);

0 commit comments

Comments
 (0)