Skip to content

Commit a231172

Browse files
authored
fix: don't replace comments inside codeblocks while processing README (#7093)
1 parent e52b618 commit a231172

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`processReadme should preserve comments inside code blocks 1`] = `
4+
"
5+
6+
### Disable url resolving using the \`<!-- webpackIgnore: true -->\` comment
7+
8+
\`\`\`html
9+
<!-- Disabled url handling for the src attribute -->
10+
<!-- webpackIgnore: true -->
11+
<img src="image.png" />
12+
13+
<!-- Disabled url handling for the src and srcset attributes -->
14+
<!-- webpackIgnore: true -->
15+
<img
16+
srcset="image.png 480w, image.png 768w"
17+
src="image.png"
18+
alt="Elva dressed as a fairy"
19+
/>
20+
21+
<!-- Disabled url handling for the content attribute -->
22+
<!-- webpackIgnore: true -->
23+
<meta itemprop="image" content="./image.png" />
24+
25+
<!-- Disabled url handling for the href attribute -->
26+
<!-- webpackIgnore: true -->
27+
<link rel="icon" type="image/png" sizes="192x192" href="./image.png" />
28+
\`\`\`
29+
"
30+
`;

src/utilities/process-readme.mjs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,36 @@ export default function processREADME(body, options = {}) {
109109
.replace(inlineLinkRegex, linkFixerFactory(options.source))
110110
// Replace any <h2> with `##`
111111
.replace(/<h2[^>]*>/g, '## ')
112-
.replace(/<\/h2>/g, '')
113-
// Drop any comments
114-
.replace(/<!--[\s\S]*?-->/g, '');
112+
.replace(/<\/h2>/g, '');
113+
114+
// Drop any comments that are not in code blocks
115+
// EXAMPLE: <!-- some comment --> should be dropped
116+
// EXAMPLE: `<!-- webpackIgnore: true -->` should not be dropped
117+
// EXAMPLE: ```html <!-- webpackIgnore: true --> <!-- some comment -->``` should not be dropped
118+
processingString = processingString.replace(/<!--[\s\S]*?-->/g, (match) => {
119+
const codeBlockPattern = /```([\s\S]*?)```|`([\s\S]*?)`/g;
120+
const contents = [];
121+
let matches;
122+
while ((matches = codeBlockPattern.exec(processingString)) !== null) {
123+
// Content inside triple backticks
124+
if (matches[1] !== undefined) {
125+
contents.push(matches[1]);
126+
}
127+
// Content inside single backticks
128+
else if (matches[2] !== undefined) {
129+
contents.push(matches[2]);
130+
}
131+
}
132+
133+
// If the comment is inside a code block, return the match
134+
if (contents.join('').includes(match)) {
135+
return match;
136+
}
137+
138+
return '';
139+
});
115140

116-
// find the laoders links
141+
// find the loaders links
117142
const loaderMatches = getMatches(
118143
processingString,
119144
/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g

src/utilities/process-readme.test.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,38 @@ describe('processReadme', () => {
3939
'See the file [`https://github.com/webpack-contrib/postcss-loader/blob/master/src/config.d.ts`](https://github.com/webpack-contrib/postcss-loader/blob/master/src/config.d.ts).'
4040
);
4141
});
42+
43+
it('should preserve comments inside code blocks', () => {
44+
const options = {
45+
source:
46+
'https://raw.githubusercontent.com/webpack-contrib/postcss-loader/master/README.md',
47+
};
48+
const loaderMDData = `
49+
<!-- some comment that should be dropped -->
50+
### Disable url resolving using the \`<!-- webpackIgnore: true -->\` comment
51+
52+
\`\`\`html
53+
<!-- Disabled url handling for the src attribute -->
54+
<!-- webpackIgnore: true -->
55+
<img src="image.png" />
56+
57+
<!-- Disabled url handling for the src and srcset attributes -->
58+
<!-- webpackIgnore: true -->
59+
<img
60+
srcset="image.png 480w, image.png 768w"
61+
src="image.png"
62+
alt="Elva dressed as a fairy"
63+
/>
64+
65+
<!-- Disabled url handling for the content attribute -->
66+
<!-- webpackIgnore: true -->
67+
<meta itemprop="image" content="./image.png" />
68+
69+
<!-- Disabled url handling for the href attribute -->
70+
<!-- webpackIgnore: true -->
71+
<link rel="icon" type="image/png" sizes="192x192" href="./image.png" />
72+
\`\`\`
73+
`;
74+
expect(processReadme(loaderMDData, options)).toMatchSnapshot();
75+
});
4276
});

0 commit comments

Comments
 (0)