@@ -45,22 +45,39 @@ module.exports = ({ content, name }, _, arr) => {
4545 anchor = params . get ( 'id' )
4646 }
4747 }
48- const resolvedPath = path . resolve ( path . dirname ( name ) , linkPath )
48+
4949 const docsPath = arr . find ( ( { name } ) => name . endsWith ( 'docs/README.md' ) )
5050 const docsDir = docsPath ? path . dirname ( docsPath . name ) : ''
5151
52- return {
53- file : arr . find ( ( { name : fileName } ) => {
54- if ( fileName === resolvedPath ) return true
55- if ( fileName === `${ resolvedPath } .md` ) return true
56-
57- if ( docsDir ) {
58- const resolvedFromDocs = path . resolve ( docsDir , linkPath )
59- if ( fileName === resolvedFromDocs ) return true
60- if ( fileName === `${ resolvedFromDocs } .md` ) return true
61- }
52+ let file
53+
54+ const findFile = ( basePath ) => {
55+ return arr . find ( ( { name : fileName } ) => {
56+ const absoluteFileName = path . resolve ( process . cwd ( ) , fileName )
57+ if ( absoluteFileName === basePath ) return true
58+ if ( absoluteFileName === `${ basePath } .md` ) return true
59+ if ( absoluteFileName === path . join ( basePath , 'README.md' ) ) return true
6260 return false
63- } ) ,
61+ } )
62+ }
63+
64+ if ( linkPath . startsWith ( '/' ) ) {
65+ const absolutePath = path . join ( docsDir , linkPath )
66+ file = findFile ( absolutePath )
67+ } else {
68+ // First, try to resolve the path relative to the current file's directory.
69+ const resolvedPath = path . resolve ( path . dirname ( name ) , linkPath )
70+ file = findFile ( resolvedPath )
71+
72+ // If the file is not found, try to resolve it relative to the docs root directory as a fallback.
73+ if ( ! file && docsDir ) {
74+ const resolvedFromDocs = path . resolve ( docsDir , linkPath )
75+ file = findFile ( resolvedFromDocs )
76+ }
77+ }
78+
79+ return {
80+ file,
6481 link : href ,
6582 anchor
6683 }
@@ -91,7 +108,7 @@ module.exports = ({ content, name }, _, arr) => {
91108 }
92109
93110 if ( ! headingNode ) {
94- console . error ( ' no heading (non blocking error)' , link )
111+ console . warn ( ` no heading found for ${ link } . The link will point to the top of the page.` )
95112 return { link, unsafeTag : '' }
96113 }
97114
0 commit comments