1
1
const url = require ( 'url' ) ;
2
2
3
+ const beginsWithDocsDomainRegex = / ^ h t t p s ? : \/ \/ w e b p a c k \. j s \. o r g / ;
4
+
3
5
module . exports = function processREADME ( body , options = { } ) {
4
6
return body
5
7
. replace ( / [ ^ ] * ?< d i v a l i g n = " c e n t e r " > ( [ ^ ] * ?) < \/ d i v > / , ( match , content ) => {
@@ -14,11 +16,27 @@ module.exports = function processREADME(body, options = {}) {
14
16
// EXAMPLE: [Contributing](./.github/CONTRIBUTING.md)
15
17
// EXAMPLE: [Contributing](CONTRIBUTING.md)
16
18
. replace ( / \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) / g, ( markdownLink , content , href ) => {
19
+ const oldHref = href ;
20
+
17
21
if ( href . includes ( '//npmjs.com' ) ) {
18
22
href = href . replace ( '//www.npmjs.com' ) ;
19
23
}
20
24
21
- return `[${ content } ](${ url . resolve ( options . source , href ) } )` ;
25
+ // Only resolve non-absolute urls from their source if they are not a document fragment link
26
+ if ( ! href . startsWith ( '#' ) ) {
27
+ href = url . resolve ( options . source , href ) ;
28
+ }
29
+
30
+ // Modify absolute documenation links to be root relative
31
+ if ( beginsWithDocsDomainRegex . test ( href ) ) {
32
+ href = href . replace ( beginsWithDocsDomainRegex , '' ) ;
33
+ }
34
+
35
+ if ( oldHref !== href ) {
36
+ console . log ( 'REWRITE URL:' , oldHref , '-->' , href ) ;
37
+ }
38
+
39
+ return `[${ content } ](${ href } )` ;
22
40
} )
23
41
// Modify links to keep them within the site
24
42
. replace ( / h t t p s ? : \/ \/ g i t h u b .c o m \/ ( w e b p a c k | w e b p a c k - c o n t r i b ) \/ ( [ - A - z a - z 0 - 9 ] + - l o a d e r \/ ? ) ( [ ) " ] ) / g, '/loaders/$2/$3' )
0 commit comments