1+ // TODO: Clean up when https://github.com/DavidAnson/markdownlint/pull/993 is merged
12module . exports = {
23 names : [ "GH003" , "no-empty-alt-text" ] ,
34 description : "Please provide an alternative text for the image." ,
@@ -17,22 +18,38 @@ module.exports = {
1718 } ,
1819 ) ;
1920
20- const htmlAltRegex = new RegExp ( / a l t = [ ' " ] [ ' " ] / , "gid" ) ;
21-
21+ const ImageRegex = new RegExp ( / < i m g ( .* ?) > / , "gid" ) ;
22+ const htmlAltRegex = new RegExp ( / a l t = [ ' " ] / , "gid" ) ;
23+ const htmlEmptyAltRegex = new RegExp ( / a l t = [ ' " ] [ ' " ] / , "gid" ) ;
2224 for ( const token of htmlTagsWithImages ) {
2325 const lineRange = token . map ;
2426 const lineNumber = token . lineNumber ;
2527 const lines = params . lines . slice ( lineRange [ 0 ] , lineRange [ 1 ] ) ;
2628
2729 for ( const [ i , line ] of lines . entries ( ) ) {
28- const matches = line . matchAll ( htmlAltRegex ) ;
29- for ( const match of matches ) {
30- const matchingContent = match [ 0 ] ;
31- const startIndex = match . indices [ 0 ] [ 0 ] ;
32- onError ( {
33- lineNumber : lineNumber + i ,
34- range : [ startIndex + 1 , matchingContent . length ] ,
35- } ) ;
30+ const imageTags = line . matchAll ( ImageRegex ) ;
31+
32+ for ( const imageTag of imageTags ) {
33+ const imageTagIndex = imageTag . indices [ 0 ] [ 0 ] ;
34+
35+ const emptyAltMatches = [
36+ ...imageTag [ 0 ] . matchAll ( htmlEmptyAltRegex ) ,
37+ ] [ 0 ] ;
38+ const noAltMatches = [ ...imageTag [ 0 ] . matchAll ( htmlAltRegex ) ] ;
39+
40+ if ( emptyAltMatches ) {
41+ const matchingContent = emptyAltMatches [ 0 ] ;
42+ const startIndex = emptyAltMatches . indices [ 0 ] [ 0 ] ;
43+ onError ( {
44+ lineNumber : lineNumber + i ,
45+ range : [ imageTagIndex + startIndex + 1 , matchingContent . length ] ,
46+ } ) ;
47+ } else if ( noAltMatches . length === 0 ) {
48+ onError ( {
49+ lineNumber : lineNumber + i ,
50+ range : [ imageTagIndex + 1 , imageTag [ 0 ] . length ] ,
51+ } ) ;
52+ }
3653 }
3754 }
3855 }
0 commit comments