Skip to content

Commit

Permalink
Fix support for absolute paths on GitHub
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed Oct 31, 2024
1 parent ac9a800 commit 4791865
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
23 changes: 17 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
* Prefix of headings (example: `'#'`, `'#markdown-header-'`).
* @property {string | null | undefined} [hostname]
* Domain of URLs (example: `'github.com'`, `'bitbucket.org'`).
* @property {boolean | null | undefined} [resolveAbsolutePathsInRepo]
* Whether absolute paths (`/x/y/z.md`) resolve relative to a repo.
* @property {boolean | null | undefined} [lines]
* Whether lines in files can be linked.
* @property {string | null | undefined} [prefix]
Expand Down Expand Up @@ -227,6 +229,10 @@ export default function remarkValidateLinks(options, fileSet) {
config.lines = lineLinks[info.type]
}

if (info.type === 'github') {
config.resolveAbsolutePathsInRepo = true
}

if (info.type in topAnchors) {
config.topAnchor = topAnchors[info.type]
}
Expand Down Expand Up @@ -416,9 +422,9 @@ export default function remarkValidateLinks(options, fileSet) {
}
}

// To do: test for no readme in directory.
// To do: test for no readme in folder.

// Else, there’s no readme that we can parse, so add the directory.
// Else, there’s no readme that we can parse, so add the folder.
if (file) {
filePath = path.join(filePath, file)
statted.add(filePath)
Expand Down Expand Up @@ -632,14 +638,19 @@ function warn(landmarks, reference) {
*/
// eslint-disable-next-line complexity
function urlToPath(value, state, type) {
// Absolute paths: `/wooorm/test/blob/main/directory/example.md`.
// Absolute paths: `/folder/example.md`.
if (value.charAt(0) === slash) {
if (!state.urlConfig.hostname) {
return
}

const pathname =
state.urlConfig.resolveAbsolutePathsInRepo && state.urlConfig.prefix
? state.urlConfig.prefix + 'main' + value
: value

// Create a URL.
value = https + slashes + state.urlConfig.hostname + value
value = https + slashes + state.urlConfig.hostname + pathname
}

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

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

// Things get interesting here: branches: `foo/bar/baz` could be `baz` on
// the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
// the `foo/bar` branch, or, `baz` in the `bar` folder on the `foo`
// branch.
// Currently, we’re ignoring this and just not supporting branches.
value = value.split(slash).slice(1).join(slash)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ References w/o definitions are not checked: [delta]
…and a module `example.js`:

```js
import {remark} from 'remark'
import remarkValidateLinks from 'remark-validate-links'
import {remark} from 'remark'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is an invalid relative heading [link](#world).

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

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

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

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ So is this [link](https://gitlab.com/wooorm/test/blob/foo-bar/examples/gitlab.md

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

How about this [link](/examples/gitlab.md).

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

This is a valid external [file](../index.js).
Expand Down
Loading

0 comments on commit 4791865

Please sign in to comment.