forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-readme.js
140 lines (123 loc) · 4.62 KB
/
process-readme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const url = require('url');
const { excludedLoaders, excludedPlugins } = require('./constants');
const beginsWithDocsDomainRegex = /^(?:https?:)\/\/webpack\.js\.org/;
const inlineLinkRegex = /\[[^\]]*\]\(([^)]+)\)/g;
const fragmentLinkMap = {
'/api/module-variables/#__webpack_public_path__-webpack-specific-':
'/api/module-variables/#__webpack_public_path__-webpack-specific',
'/configuration/module/#rule-exclude': '/configuration/module/#ruleexclude',
'/configuration/module/#rule-include': '/configuration/module/#ruleinclude',
'/configuration/module/#rule-options-rule-query':
'/configuration/module/#ruleoptions--rulequery',
'/configuration/module/#rule-use': '/configuration/module/#ruleuse',
'/configuration/optimization/#optimization-concatenatemodules':
'/configuration/optimization/#optimizationconcatenatemodules',
'/configuration/output/#output-chunkfilename':
'/configuration/output/#outputchunkfilename',
'/configuration/output/#output-publicpath':
'/configuration/output/#outputpublicpath',
'/configuration/resolve/#resolve-modules':
'/configuration/resolve/#resolvemodules',
'/guides/shimming/#exports-loader': '/loaders/exports-loader',
'/guides/shimming/#imports-loader': '/loaders/imports-loader',
'/guides/shimming/#provideplugin': '/plugins/provide-plugin/',
};
function linkFixerFactory(sourceUrl) {
return function linkFixer(markdownLink, href) {
const oldHref = href;
if (href.includes('//npmjs.com')) {
href = href.replace('//www.npmjs.com');
}
// Only resolve non-absolute urls from their source if they are not a document fragment link
if (!href.startsWith('#')) {
// Convert Github raw links to rendered links
let rendered_url = sourceUrl
.replace(/raw.githubusercontent.com/, 'github.com')
.replace(/master/, 'blob/master');
href = url.resolve(rendered_url, href);
}
// Modify absolute documentation links to be root relative
if (beginsWithDocsDomainRegex.test(href)) {
href = href.replace(beginsWithDocsDomainRegex, '');
}
const fragmentLinkMapMatch = Object.keys(fragmentLinkMap).find((mapFrom) =>
href.includes(mapFrom)
);
if (fragmentLinkMapMatch) {
href = href.replace(
fragmentLinkMapMatch,
fragmentLinkMap[fragmentLinkMapMatch]
);
console.error(`DEPRECATED EXTERNAL README LINK:
URL: ${sourceUrl}
ACTUAL: ${oldHref}
EXPECTED: ${oldHref.replace(
fragmentLinkMapMatch,
fragmentLinkMap[fragmentLinkMapMatch]
)}`);
}
// Lowercase all fragment links, since markdown generators do the same
if (href.includes('#')) {
const [urlPath, urlFragment] = href.split('#');
href = `${urlPath}#${urlFragment.toLowerCase()}`;
}
if (oldHref !== href) {
console.log('REWRITE URL:', oldHref, '-->', href);
}
return markdownLink.replace(oldHref, href);
};
}
function getMatches(string, regex) {
const matches = [];
let match;
// eslint-disable-next-line
while ((match = regex.exec(string))) {
matches.push(match);
}
return matches;
}
module.exports = function processREADME(body, options = {}) {
let processingString = body
// Replace lone h1 formats
.replace(/<h1.*?>.+?<\/h1>/, '')
.replace(/^# .+/m, '')
.replace(/.*\n=+/, '')
// Replace local github links with absolute links to the github location
// EXAMPLE: [Contributing](./.github/CONTRIBUTING.md)
// EXAMPLE: [Contributing](CONTRIBUTING.md)
// EXAMPLE: [line-identifier]: https://webpack.js.org/loaders/
.replace(inlineLinkRegex, linkFixerFactory(options.source))
// Replace any <h2> with `##`
.replace(/<h2[^>]*>/g, '## ')
.replace(/<\/h2>/g, '')
// Drop any comments
.replace(/<!--[\s\S]*?-->/g, '');
// find the laoders links
const loaderMatches = getMatches(
processingString,
/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g
);
// dont make relative links for excluded loaders
loaderMatches.forEach((match) => {
if (!excludedLoaders.includes(`${match[1]}/${match[2]}`)) {
processingString = processingString.replace(
match[0],
`/loaders/${match[2]}/)`
);
}
});
const pluginMatches = getMatches(
processingString,
/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-plugin\/?)([)"])/g
);
// dont make relative links for excluded loaders
pluginMatches.forEach((match) => {
if (!excludedPlugins.includes(`${match[1]}/${match[2]}`)) {
processingString = processingString.replace(
match[0],
`/plugins/${match[2]}/)`
);
}
});
return processingString;
};