@@ -15,6 +15,14 @@ const CHECK_EXTERNAL = process.argv.includes('--external');
1515const MARKDOWN_LINK_REGEX = / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) / g;
1616const JSX_LINK_REGEX = / h r e f = [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g;
1717const 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
1927function findMDXFiles ( dir , fileList = [ ] ) {
2028 const files = fs . readdirSync ( dir ) ;
@@ -36,23 +44,26 @@ function findMDXFiles(dir, fileList = []) {
3644function 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