Skip to content

Commit 262051a

Browse files
Merge pull request #37 from commitd/mc-fix-upward-relative-links
2 parents 115d8d4 + cf7a22b commit 262051a

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

example/docs/tests/a/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
This file is the index.md in the `a` folder. It appears on the path as with a trailing slash.
44

5-
To link relatively to the folder `b` you should use, [b/](b/) or [b/index](b/index) the last has the advantage that is also works in the raw markdown. A full link is also valid [/tests/a/b/](/tests/a/b/) or [/tests/a/b/index](/tests/a/b/index) linking to headings is also supported [b/#link-test](b/#link-test) and [b/index#link-test](b/index#link-test).
5+
To link relatively to the folder `b` you should use, [b/](b/) or [b/index](b/index) the last has the advantage that is also works in the raw markdown. A full link is also valid [/tests/a/b/](/tests/a/b/) or [/tests/a/b/index](/tests/a/b/index) linking to headings is also supported [b/#link-test](b/#link-test) and [b/index#link-test](b/index#link-test).
6+
7+
You can also link 'up' directories, for example: [../a](../a), [../../tests/mdTest](../../tests/mdTest), [../../tests/a/b#link-test](../../tests/a/b#link-test)
68

79
We consider paths as case insensitive, so the following also work [B/](B/) or [B/Index](B/Index).
810

911
The following are invalid.
1012

1113
```markdown
12-
[../](../) // not supported
13-
[../a](../a) // not supported
1414
[a/b/c/d](a/b/c/d) // missing
1515
[b/#link-test](b/#link-tests) // missing
1616
```

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "example",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"main": "index.js",
55
"author": "Committed <[email protected]>",
66
"license": "MIT",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@committed/gatsby-theme-docs-workspace",
33
"private": true,
4-
"version": "2.1.1",
4+
"version": "2.1.2",
55
"main": "index.js",
66
"license": "MIT",
77
"scripts": {

theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@committed/gatsby-theme-docs",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"main": "index.js",
55
"author": "Committed <[email protected]>",
66
"license": "MIT",

theme/plugins/check-links/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@ function getCacheKey(node) {
66
return `check-links-${node.id}-${node.internal.contentDigest}`
77
}
88

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)
21+
}
22+
23+
function convertToBasePath(link, path) {
24+
if (link.startsWith('../')) {
25+
return convertToAbsolutePath(link, path).toLowerCase()
26+
}
27+
return link.toLowerCase().replace(/^\.\//, '') // strip ./
28+
29+
}
30+
931
function getHeadingsMapKey(link, path) {
10-
let basePath = link.toLowerCase().replace(/^\.\//, '') // strip ./
32+
let basePath = convertToBasePath(link, path)
1133
const hashIndex = basePath.indexOf('#')
1234
const hasHash = hashIndex !== -1
1335
const hashId = hasHash ? basePath.slice(hashIndex + 1) : null
@@ -121,7 +143,8 @@ module.exports = async (
121143
return false
122144
}
123145

124-
const headings = headingsMap[key]
146+
// If no heading is found, try again with a trailing /
147+
const headings = headingsMap[key] == null ? headingsMap[`${key}/`] : headingsMap[key]
125148
if (headings) {
126149
if (hasHash) {
127150
return (

0 commit comments

Comments
 (0)