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
+
5
+ const redirects = {
6
+ '/guides/migrating/#json-loader-is-not-required-anymore' : '/migrate/3#json-loader-is-not-required-anymore'
7
+ } ;
8
+
3
9
module . exports = function processREADME ( body , options = { } ) {
4
10
return body
5
11
. replace ( / [ ^ ] * ?< d i v a l i g n = " c e n t e r " > ( [ ^ ] * ?) < \/ d i v > / , ( match , content ) => {
@@ -14,11 +20,30 @@ module.exports = function processREADME(body, options = {}) {
14
20
// EXAMPLE: [Contributing](./.github/CONTRIBUTING.md)
15
21
// EXAMPLE: [Contributing](CONTRIBUTING.md)
16
22
. replace ( / \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) / g, ( markdownLink , content , href ) => {
23
+ const oldHref = href ;
24
+
17
25
if ( href . includes ( '//npmjs.com' ) ) {
18
26
href = href . replace ( '//www.npmjs.com' ) ;
19
27
}
20
28
21
- return `[${ content } ](${ url . resolve ( options . source , href ) } )` ;
29
+ if ( ! href . startsWith ( '#' ) ) {
30
+ href = url . resolve ( options . source , href ) ;
31
+ }
32
+
33
+ // Modify absolute documenation links to be root relative
34
+ if ( beginsWithDocsDomainRegex . test ( href ) ) {
35
+ href = href . replace ( beginsWithDocsDomainRegex , '' ) ;
36
+ }
37
+
38
+ if ( href in redirects ) {
39
+ href = redirects [ href ] ;
40
+ }
41
+
42
+ if ( oldHref !== href ) {
43
+ console . log ( 'REWRITE URL:' , oldHref , '-->' , href ) ;
44
+ }
45
+
46
+ return `[${ content } ](${ href } )` ;
22
47
} )
23
48
// Modify links to keep them within the site
24
49
. 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