106
106
* Prefix of headings (example: `'#'`, `'#markdown-header-'`).
107
107
* @property {string | null | undefined } [hostname]
108
108
* 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.
109
111
* @property {boolean | null | undefined } [lines]
110
112
* Whether lines in files can be linked.
111
113
* @property {string | null | undefined } [prefix]
@@ -227,6 +229,10 @@ export default function remarkValidateLinks(options, fileSet) {
227
229
config . lines = lineLinks [ info . type ]
228
230
}
229
231
232
+ if ( info . type === 'github' ) {
233
+ config . resolveAbsolutePathsInRepo = true
234
+ }
235
+
230
236
if ( info . type in topAnchors ) {
231
237
config . topAnchor = topAnchors [ info . type ]
232
238
}
@@ -416,9 +422,9 @@ export default function remarkValidateLinks(options, fileSet) {
416
422
}
417
423
}
418
424
419
- // To do: test for no readme in directory .
425
+ // To do: test for no readme in folder .
420
426
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 .
422
428
if ( file ) {
423
429
filePath = path . join ( filePath , file )
424
430
statted . add ( filePath )
@@ -632,14 +638,19 @@ function warn(landmarks, reference) {
632
638
*/
633
639
// eslint-disable-next-line complexity
634
640
function urlToPath ( value , state , type ) {
635
- // Absolute paths: `/wooorm/test/blob/main/directory /example.md`.
641
+ // Absolute paths: `/folder /example.md`.
636
642
if ( value . charAt ( 0 ) === slash ) {
637
643
if ( ! state . urlConfig . hostname ) {
638
644
return
639
645
}
640
646
647
+ const pathname =
648
+ state . urlConfig . resolveAbsolutePathsInRepo && state . urlConfig . prefix
649
+ ? state . urlConfig . prefix + 'main' + value
650
+ : value
651
+
641
652
// Create a URL.
642
- value = https + slashes + state . urlConfig . hostname + value
653
+ value = https + slashes + state . urlConfig . hostname + pathname
643
654
}
644
655
645
656
/** @type {URL | undefined } */
@@ -649,7 +660,7 @@ function urlToPath(value, state, type) {
649
660
url = new URL ( value )
650
661
} catch { }
651
662
652
- // URLs: `https://github.com/wooorm/test/blob/main/directory /example.md`.
663
+ // URLs: `https://github.com/wooorm/test/blob/main/folder /example.md`.
653
664
if ( url && state . root ) {
654
665
// Exit if we don’t have hosted Git info or this is not a URL to the repo.
655
666
if (
@@ -666,7 +677,7 @@ function urlToPath(value, state, type) {
666
677
value = url . pathname . slice ( state . urlConfig . prefix . length )
667
678
668
679
// 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`
670
681
// branch.
671
682
// Currently, we’re ignoring this and just not supporting branches.
672
683
value = value . split ( slash ) . slice ( 1 ) . join ( slash )
0 commit comments