11import fs from 'fs/promises' ;
22import path from 'path' ;
3+ import crypto from 'crypto' ;
34import matter from 'gray-matter' ;
45import { marked } from 'marked' ;
56import { glob } from 'glob' ;
@@ -13,11 +14,21 @@ export interface MarkdownPluginOptions {
1314
1415interface MarkdownFile {
1516 path : string ;
17+ relativePath : string ;
1618 frontMatter : Record < string , unknown > ;
1719 markdown : string ;
1820 html : string ;
1921}
2022
23+ function normalizePath ( input : string ) : string {
24+ return input . replace ( / \\ / g, '/' ) ;
25+ }
26+
27+ function generateGuideId ( relativePath : string ) : string {
28+ const content = `guide|${ normalizePath ( relativePath ) } ` ;
29+ return crypto . createHash ( 'md5' ) . update ( content ) . digest ( 'hex' ) . slice ( 0 , 8 ) ;
30+ }
31+
2132export default function markdownPlugin ( options : MarkdownPluginOptions ) : Plugin {
2233 const files : MarkdownFile [ ] = [ ] ;
2334
@@ -37,6 +48,7 @@ export default function markdownPlugin(options: MarkdownPluginOptions): Plugin {
3748
3849 for ( const file of foundFiles ) {
3950 const content = await fs . readFile ( file , 'utf-8' ) ;
51+ const relativePath = normalizePath ( path . relative ( options . sourceDir , file ) ) ;
4052 let frontMatter : Record < string , unknown > = { } ;
4153 let markdown = content ;
4254
@@ -51,6 +63,7 @@ export default function markdownPlugin(options: MarkdownPluginOptions): Plugin {
5163
5264 files . push ( {
5365 path : file ,
66+ relativePath,
5467 frontMatter,
5568 markdown,
5669 html,
@@ -64,17 +77,19 @@ export default function markdownPlugin(options: MarkdownPluginOptions): Plugin {
6477 afterExtract ( docs : DocEntry [ ] ) {
6578 const guideDocs : DocEntry [ ] = files . map ( ( file ) => {
6679 const fileName = path . basename ( file . path , path . extname ( file . path ) ) ;
80+ const modulePath = file . relativePath . replace ( / \. [ ^ / . ] + $ / , '' ) ;
6781 const title =
6882 typeof file . frontMatter . title === 'string' ? file . frontMatter . title : fileName ;
6983 const description =
7084 typeof file . frontMatter . description === 'string' ? file . frontMatter . description : '' ;
7185
7286 return {
73- id : `guide: ${ file . path } ` ,
87+ id : generateGuideId ( file . relativePath ) ,
7488 name : title ,
7589 kind : 'guide' ,
76- fileName : file . path ,
77- source : { file : file . path , line : 1 , column : 0 } ,
90+ fileName : file . relativePath ,
91+ module : modulePath ,
92+ source : { file : file . relativePath , line : 1 , column : 0 } ,
7893 position : { line : 1 , column : 0 } ,
7994 signature : '' ,
8095 documentation : {
0 commit comments