Skip to content

Commit 6a1d312

Browse files
committed
文档转换器添加跳转链接转换的逻辑
1 parent 93d86da commit 6a1d312

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

darkit/core/web/vite-plugin-docs.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type DocsTree =
1919
title: string;
2020
path: string;
2121
};
22+
2223
const isPathIn = (a: string, b: string) => {
2324
const relative = path.relative(b, a);
2425
return !relative.startsWith('..') && !path.isAbsolute(relative);
@@ -77,13 +78,14 @@ const removeDocsCache = async (target: string) => {
7778
};
7879

7980
/**
80-
* 将 markdown 文件中的静态资源引用转换为相对路径
81+
* 将 markdown 文件中的静态资源引用转换为 web 路径,将链接路径页转换为 web 路径
8182
* @returns { content: string, staticFiles: [string, string][] } 转换后的 markdown 内容和静态资源文件路径
8283
* staticFiles 为 [源文件路径, 相对路径] 的数组
8384
*/
8485
const transformMarkdown = async (sourceDir: string) => {
8586
const content = await fs.readFile(sourceDir, 'utf-8');
8687
const staticFiles: [string, string][] = [];
88+
// 将 markdown 文件中的本地静态资源引用转换为 web 路径, 并将本地文件路径读取出来
8789
const updatedContent = content.replace(/!\[.*?\]\((\/.*?)(?:\s".*?")?\)/g, (match, p1) => {
8890
const staticFile = path.isAbsolute(p1) ? path.join(ROOT_PATH, p1) : path.join(sourceDir, p1);
8991
// 如果是网络文件则跳过
@@ -98,9 +100,29 @@ const transformMarkdown = async (sourceDir: string) => {
98100
);
99101
}
100102
});
101-
return { content: updatedContent, staticFiles };
103+
// 将 markdown 文件中的链接路径转换为 web 路径
104+
const finalContent = updatedContent.replace(/\[.*?\]\((.*?).md\)/g, (match, p1) => {
105+
// 如果是网络文件则跳过
106+
if (p1.startsWith('http')) return match;
107+
const newUrl = (p1 as string)
108+
.split('/')
109+
.map((segment) => {
110+
if (segment !== '.' && segment !== '..' && segment.includes('.')) {
111+
return segment.split('.')[1];
112+
}
113+
return segment;
114+
})
115+
.join('/');
116+
117+
return match.replace(`${p1}.md`, newUrl);
118+
});
119+
return { content: finalContent, staticFiles };
102120
};
103121

122+
/**
123+
* 文件夹和文档的命名需要符合“序号.标题”格式
124+
* 构建到 svelte 中时根据序号排序,标题作为 svelte 文件夹和组件的名称
125+
*/
104126
export const docs = ({
105127
source,
106128
target,

docs/1.Introduction/1.About.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# About
2-
本仓库是一个基于 PyTorch 的自然语言处理工具包,提供了一些常用的 NLP 模型和工具。
2+
DarwinKit is a PyTorch-based NLP toolkit that provides some commonly used NLP models and tools.
33

44
## Getting Started For Users
55
You can read the following documents to start using this repository:
6-
- [Installation](/docs/User-guide/Installation-guide)
7-
- [Tutorials](/docs/User-guide/How-use-web)
6+
- [Installation](../2.User-guide/1.Installation-guide.md)
7+
- [Tutorials](../2.User-guide/2.How-use-web.md)

docs/zh/1.Introduction/1.About.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
## 用户入门指南
55
您可以阅读以下文档来开始使用该仓库:
6-
- [安装指南](/docs/User-guide/Installation-guide)
7-
- [教程](/docs/User-guide/How-use-web)
6+
- [安装指南](../2.User-guide/1.Installation-guide.md)
7+
- [教程](../2.User-guide/2.How-use-web.md)

0 commit comments

Comments
 (0)