@@ -19,6 +19,7 @@ type DocsTree =
19
19
title : string ;
20
20
path : string ;
21
21
} ;
22
+
22
23
const isPathIn = ( a : string , b : string ) => {
23
24
const relative = path . relative ( b , a ) ;
24
25
return ! relative . startsWith ( '..' ) && ! path . isAbsolute ( relative ) ;
@@ -77,13 +78,14 @@ const removeDocsCache = async (target: string) => {
77
78
} ;
78
79
79
80
/**
80
- * 将 markdown 文件中的静态资源引用转换为相对路径
81
+ * 将 markdown 文件中的静态资源引用转换为 web 路径,将链接路径页转换为 web 路径
81
82
* @returns { content: string, staticFiles: [string, string][] } 转换后的 markdown 内容和静态资源文件路径
82
83
* staticFiles 为 [源文件路径, 相对路径] 的数组
83
84
*/
84
85
const transformMarkdown = async ( sourceDir : string ) => {
85
86
const content = await fs . readFile ( sourceDir , 'utf-8' ) ;
86
87
const staticFiles : [ string , string ] [ ] = [ ] ;
88
+ // 将 markdown 文件中的本地静态资源引用转换为 web 路径, 并将本地文件路径读取出来
87
89
const updatedContent = content . replace ( / ! \[ .* ?\] \( ( \/ .* ?) (?: \s " .* ?" ) ? \) / g, ( match , p1 ) => {
88
90
const staticFile = path . isAbsolute ( p1 ) ? path . join ( ROOT_PATH , p1 ) : path . join ( sourceDir , p1 ) ;
89
91
// 如果是网络文件则跳过
@@ -98,9 +100,29 @@ const transformMarkdown = async (sourceDir: string) => {
98
100
) ;
99
101
}
100
102
} ) ;
101
- return { content : updatedContent , staticFiles } ;
103
+ // 将 markdown 文件中的链接路径转换为 web 路径
104
+ const finalContent = updatedContent . replace ( / \[ .* ?\] \( ( .* ?) .m d \) / 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 } ;
102
120
} ;
103
121
122
+ /**
123
+ * 文件夹和文档的命名需要符合“序号.标题”格式
124
+ * 构建到 svelte 中时根据序号排序,标题作为 svelte 文件夹和组件的名称
125
+ */
104
126
export const docs = ( {
105
127
source,
106
128
target,
0 commit comments