Skip to content

Commit 4791865

Browse files
committed
Fix support for absolute paths on GitHub
At some point in time, GitHub changed absolute paths in markdown, to be related to the repo. Instead of `github.com`. This patch reflects that. Closes GH-75.
1 parent ac9a800 commit 4791865

File tree

5 files changed

+75
-54
lines changed

5 files changed

+75
-54
lines changed

lib/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
* Prefix of headings (example: `'#'`, `'#markdown-header-'`).
107107
* @property {string | null | undefined} [hostname]
108108
* Domain of URLs (example: `'github.com'`, `'bitbucket.org'`).
109+
* @property {boolean | null | undefined} [resolveAbsolutePathsInRepo]
110+
* Whether absolute paths (`/x/y/z.md`) resolve relative to a repo.
109111
* @property {boolean | null | undefined} [lines]
110112
* Whether lines in files can be linked.
111113
* @property {string | null | undefined} [prefix]
@@ -227,6 +229,10 @@ export default function remarkValidateLinks(options, fileSet) {
227229
config.lines = lineLinks[info.type]
228230
}
229231

232+
if (info.type === 'github') {
233+
config.resolveAbsolutePathsInRepo = true
234+
}
235+
230236
if (info.type in topAnchors) {
231237
config.topAnchor = topAnchors[info.type]
232238
}
@@ -416,9 +422,9 @@ export default function remarkValidateLinks(options, fileSet) {
416422
}
417423
}
418424

419-
// To do: test for no readme in directory.
425+
// To do: test for no readme in folder.
420426

421-
// Else, there’s no readme that we can parse, so add the directory.
427+
// Else, there’s no readme that we can parse, so add the folder.
422428
if (file) {
423429
filePath = path.join(filePath, file)
424430
statted.add(filePath)
@@ -632,14 +638,19 @@ function warn(landmarks, reference) {
632638
*/
633639
// eslint-disable-next-line complexity
634640
function urlToPath(value, state, type) {
635-
// Absolute paths: `/wooorm/test/blob/main/directory/example.md`.
641+
// Absolute paths: `/folder/example.md`.
636642
if (value.charAt(0) === slash) {
637643
if (!state.urlConfig.hostname) {
638644
return
639645
}
640646

647+
const pathname =
648+
state.urlConfig.resolveAbsolutePathsInRepo && state.urlConfig.prefix
649+
? state.urlConfig.prefix + 'main' + value
650+
: value
651+
641652
// Create a URL.
642-
value = https + slashes + state.urlConfig.hostname + value
653+
value = https + slashes + state.urlConfig.hostname + pathname
643654
}
644655

645656
/** @type {URL | undefined} */
@@ -649,7 +660,7 @@ function urlToPath(value, state, type) {
649660
url = new URL(value)
650661
} catch {}
651662

652-
// URLs: `https://github.com/wooorm/test/blob/main/directory/example.md`.
663+
// URLs: `https://github.com/wooorm/test/blob/main/folder/example.md`.
653664
if (url && state.root) {
654665
// Exit if we don’t have hosted Git info or this is not a URL to the repo.
655666
if (
@@ -666,7 +677,7 @@ function urlToPath(value, state, type) {
666677
value = url.pathname.slice(state.urlConfig.prefix.length)
667678

668679
// Things get interesting here: branches: `foo/bar/baz` could be `baz` on
669-
// the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
680+
// the `foo/bar` branch, or, `baz` in the `bar` folder on the `foo`
670681
// branch.
671682
// Currently, we’re ignoring this and just not supporting branches.
672683
value = value.split(slash).slice(1).join(slash)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ References w/o definitions are not checked: [delta]
109109
…and a module `example.js`:
110110

111111
```js
112-
import {remark} from 'remark'
113112
import remarkValidateLinks from 'remark-validate-links'
113+
import {remark} from 'remark'
114114
import {read} from 'to-vfile'
115115
import {reporter} from 'vfile-reporter'
116116

test/fixtures/github.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is an invalid relative heading [link](#world).
88

99
This is a valid relative file [link](https://github.com/wooorm/test/blob/main/examples/github.md).
1010

11-
This is a valid absolute file [link](/wooorm/test/blob/main/examples/github.md).
11+
This is a valid absolute file [link](/examples/github.md).
1212

1313
So is this [link](https://github.com/wooorm/test/blob/foo-bar/examples/github.md).
1414

@@ -20,7 +20,7 @@ This is a valid external [file](../index.js).
2020

2121
This is an invalid relative file [link](https://github.com/wooorm/test/blob/main/examples/world.md).
2222

23-
This is an invalid absolute file [link](/wooorm/test/blob/main/examples/world.md).
23+
This is an invalid absolute file [link](/examples/world.md).
2424

2525
So is this [link](https://github.com/wooorm/test/blob/foo-bar/examples/world.md).
2626

test/fixtures/gitlab.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ So is this [link](https://gitlab.com/wooorm/test/blob/foo-bar/examples/gitlab.md
1212

1313
And this [link](./examples/gitlab.md).
1414

15+
How about this [link](/examples/gitlab.md).
16+
1517
And this [link](examples/gitlab.md).
1618

1719
This is a valid external [file](../index.js).

0 commit comments

Comments
 (0)