@@ -49,22 +49,12 @@ const checkImageDimensions = async (imageTag, imagePath, cssWidth, cssHeight, dp
49
49
50
50
const { width, height } = imageMetaData ;
51
51
52
- if ( cssWidth != null ) {
53
- const factor = dpi / 96 ;
54
- const expectedWidth = cssWidth * factor ;
55
- // NOTE: We need to account for the integer division used to calculate the value of the `width` attribute.
56
- if ( width < expectedWidth || expectedWidth + factor <= width ) {
57
- errors . push ( [ 'warn' , `Horizontal image resolution doesn't correspond to attribute (width="...") in ${ imageTag } . The image should be ${ expectedWidth } pixels wide, considering the image DPI (${ dpi } ).` ] ) ;
58
- }
52
+ if ( cssWidth != null && cssWidth !== Math . trunc ( width * 96 / dpi ) ) {
53
+ errors . push ( [ 'warn' , `The relation \`CSS width = image width in pixels * 96 / DPI\` is not satisfied. CSS width specified via attribute (width="...") = ${ cssWidth } , image width in pixels = ${ width } , DPI = ${ dpi } .` ] ) ;
59
54
}
60
55
61
- if ( cssHeight != null ) {
62
- const factor = dpi / 96 ;
63
- const expectedHeight = cssHeight * factor ;
64
- // NOTE: We need to account for the integer division used to calculate the value of the `height` attribute.
65
- if ( height < expectedHeight || expectedHeight + factor <= height ) {
66
- errors . push ( [ 'warn' , `Vertical image resolution doesn't correspond to attribute (height="...") in ${ imageTag } . The image should be ${ expectedHeight } pixels tall, considering the image DPI (${ dpi } ).` ] ) ;
67
- }
56
+ if ( cssHeight != null && cssHeight !== Math . trunc ( height * 96 / dpi ) ) {
57
+ errors . push ( [ 'warn' , `The relation \`CSS height = image height in pixels * 96 / DPI\` is not satisfied. CSS height specified via attribute (height="...") = ${ cssHeight } , image height in pixels = ${ height } , DPI = ${ dpi } .` ] ) ;
68
58
}
69
59
}
70
60
@@ -114,23 +104,24 @@ const checkImages = async (content) => {
114
104
const errors = [ ] ;
115
105
116
106
let i = - 1 ;
117
- for ( const tag of extractImageData ( content ) ) {
107
+ for ( const [ tag , mdImageSrc ] of extractImageData ( content ) ) {
118
108
i ++ ;
119
109
120
110
const isMarkdown = tag . startsWith ( '![' ) ;
121
- if ( isMarkdown ) {
122
- // Markdown image detected
123
- errors . push ( [ 'warn' , `Markdown image syntax found (${ tag } ). Use <img src="..." alt="..." width="..." height="..." /> instead for images.` ] ) ;
111
+ if ( isMarkdown ) { // Markdown image detected
112
+ if ( shouldCheckImage ( mdImageSrc ) ) {
113
+ errors . push ( [ 'warn' , `Markdown image syntax found (${ tag } ). Use <img src="..." alt="..." width="..." height="..." /> instead for images.` ] ) ;
114
+ }
124
115
continue ;
125
116
}
126
117
127
118
let [ imageSrc , cssWidth , cssHeight ] = checkForImageAttributes ( tag , errors ) ;
128
119
129
120
const pathPrefix = "/docs/"
130
- if ( imageSrc . startsWith ( pathPrefix ) ) {
131
- imageSrc = imageSrc . substring ( pathPrefix . length - 1 ) ;
132
- } else if ( / ^ h t t p s ? : / i. test ( imageSrc ) ) {
121
+ if ( ! shouldCheckImage ( imageSrc ) ) {
133
122
continue ;
123
+ } else if ( imageSrc . startsWith ( pathPrefix ) ) {
124
+ imageSrc = imageSrc . substring ( pathPrefix . length - 1 ) ;
134
125
} else {
135
126
errors . push ( `Invalid image src found in ${ tag } ` ) ;
136
127
continue ;
@@ -222,13 +213,19 @@ function attributeRegex(attribute) {
222
213
function * extractImageData ( content ) {
223
214
let imageTagsRegexMatch ;
224
215
while ( ( imageTagsRegexMatch = imageTagsRegex . exec ( content ) ) !== null ) {
225
- const [ tag ] = imageTagsRegexMatch ;
226
- yield tag ;
216
+ const [ tag , , mdImageSrc ] = imageTagsRegexMatch ;
217
+ yield [ tag , mdImageSrc ?. trim ( ) ] ;
227
218
}
228
219
}
229
220
230
221
function getImageFullPath ( imagePath ) {
231
222
return path . join ( __dirname , 'static' , imagePath ) ;
232
223
}
233
224
225
+ function shouldCheckImage ( imageSrc ) {
226
+ if ( / ^ h t t p s ? : / i. test ( imageSrc ) ) return false ;
227
+ if ( path . extname ( imageSrc ) . toLowerCase ( ) === ".svg" ) return false ;
228
+ return true ;
229
+ }
230
+
234
231
checkFiles ( ) ;
0 commit comments