Skip to content

Commit 72f8e01

Browse files
author
Matt Copas
committed
Tidy up code
1 parent 01b72d6 commit 72f8e01

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

theme/plugins/check-links/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1323
function 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

Comments
 (0)