Skip to content

Commit 86d782f

Browse files
committed
better commenting
1 parent 5d3b549 commit 86d782f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

scripts/check-links.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const CHECK_EXTERNAL = process.argv.includes('--external');
1515
const MARKDOWN_LINK_REGEX = /\[([^\]]+)\]\(([^)]+)\)/g;
1616
const JSX_LINK_REGEX = /href=["']([^"']+)["']/g;
1717
const ANCHOR_REGEX = /#[^)\s"']*/;
18+
const CODE_BLOCK_REGEX = /```[\s\S]*?```/g;
19+
20+
function removeCodeBlocks(content) {
21+
return content.replace(CODE_BLOCK_REGEX, (match) => {
22+
const newlineCount = (match.match(/\n/g) || []).length;
23+
return '\n'.repeat(newlineCount);
24+
});
25+
}
1826

1927
function findMDXFiles(dir, fileList = []) {
2028
const files = fs.readdirSync(dir);
@@ -36,23 +44,26 @@ function findMDXFiles(dir, fileList = []) {
3644
function extractLinks(content, filePath) {
3745
const links = [];
3846

47+
// Remove code blocks to avoid flagging example links in documentation
48+
const contentWithoutCodeBlocks = removeCodeBlocks(content);
49+
3950
// Extract markdown links: [text](url)
4051
let match;
41-
while ((match = MARKDOWN_LINK_REGEX.exec(content)) !== null) {
52+
while ((match = MARKDOWN_LINK_REGEX.exec(contentWithoutCodeBlocks)) !== null) {
4253
links.push({
4354
text: match[1],
4455
url: match[2],
4556
type: 'markdown',
46-
line: content.substring(0, match.index).split('\n').length
57+
line: contentWithoutCodeBlocks.substring(0, match.index).split('\n').length
4758
});
4859
}
4960

50-
// Extract JSX links: href="..." or src="..."
51-
while ((match = JSX_LINK_REGEX.exec(content)) !== null) {
61+
// Extract JSX links: href="..."
62+
while ((match = JSX_LINK_REGEX.exec(contentWithoutCodeBlocks)) !== null) {
5263
links.push({
5364
url: match[1],
5465
type: 'jsx',
55-
line: content.substring(0, match.index).split('\n').length
66+
line: contentWithoutCodeBlocks.substring(0, match.index).split('\n').length
5667
});
5768
}
5869

0 commit comments

Comments
 (0)