@@ -6,21 +6,23 @@ function getCacheKey(node) {
66 return `check-links-${ node . id } -${ node . internal . contentDigest } `
77}
88
9- function getPosition ( string , subString , index ) {
10- return string . split ( subString , index ) . join ( subString ) . length ;
9+ function getNthPosition ( string , subString , n ) {
10+ return string . split ( subString , n ) . join ( subString ) . length ;
11+ }
12+
13+ function convertToAbsolutePath ( link , path ) {
14+ const moveUpDirectoryCount = ( link . match ( / \. \. \/ / g) || [ ] ) . length
15+ let pathWithoutTrailingSlash = path [ path . length - 1 ] === '/' ? path . slice ( 0 , path . length - 1 ) : path
16+ const pathSlashCount = ( path . match ( / \/ / g) || [ ] ) . length
17+ const indexToSliceTo = getNthPosition ( pathWithoutTrailingSlash , '/' , pathSlashCount - moveUpDirectoryCount ) + 1
18+ const slicedPath = pathWithoutTrailingSlash . slice ( 0 , indexToSliceTo )
19+ const slicedLink = link . slice ( moveUpDirectoryCount * 3 , link . length )
20+ return slicedPath . concat ( slicedLink )
1121}
1222
1323function convertToBasePath ( link , path ) {
1424 if ( link . startsWith ( '../' ) ) {
15- // Convert the relative link to an absolute one
16- const moveUpDirectoryCount = ( link . match ( / \. \. \/ / g) || [ ] ) . length
17- let pathWithoutTrailingSlash = path [ path . length - 1 ] === '/' ? path . slice ( 0 , path . length - 1 ) : path
18- const pathSlashCount = ( path . match ( / \/ / g) || [ ] ) . length
19- const indexToSliceTo = getPosition ( pathWithoutTrailingSlash , '/' , pathSlashCount - moveUpDirectoryCount ) + 1
20- const slicedPath = pathWithoutTrailingSlash . slice ( 0 , indexToSliceTo )
21- const slicedLink = link . slice ( moveUpDirectoryCount * 3 , link . length )
22- const concat = slicedPath . concat ( slicedLink )
23- return concat . toLowerCase ( )
25+ return convertToAbsolutePath ( link , path ) . toLowerCase ( )
2426 }
2527 return link . toLowerCase ( ) . replace ( / ^ \. \/ / , '' ) // strip ./
2628
0 commit comments