@@ -2,6 +2,7 @@ import { exists as _exists } from 'fs';
2
2
import _imageSize from 'image-size' ;
3
3
import { HTML , Image } from 'mdast' ;
4
4
import { basename , resolve } from 'path' ;
5
+ import { expect } from 'ts-std' ;
5
6
import { promisify } from 'util' ;
6
7
import BaseWalker from '../../walker' ;
7
8
import Options from './options' ;
@@ -38,9 +39,14 @@ function attr(v: unknown): string {
38
39
39
40
40
41
async function toImgTag ( node : Image , options : Options ) : Promise < HTML > {
41
- let size = await imageSize ( pathFor ( node . url , options ) ) ;
42
- let width = Math . floor ( size ! . width ! / 2 ) ;
43
- let height = Math . floor ( size ! . height ! / 2 ) ;
42
+ let sizeOfImage = await imageSize ( pathFor ( node . url , options ) ) ;
43
+
44
+ let size = expect ( sizeOfImage , 'size should be present' ) ;
45
+ let width = expect ( size . width , 'width should be present' ) ;
46
+ let height = expect ( size . height , 'height should be present' ) ;
47
+
48
+ let widthAttr = Math . floor ( width / 2 ) ;
49
+ let heightAttr = Math . floor ( height / 2 ) ;
44
50
45
51
let attrs = [ ] ;
46
52
@@ -54,8 +60,8 @@ async function toImgTag(node: Image, options: Options): Promise<HTML> {
54
60
attrs . push ( `title=${ attr ( node . title ) } ` ) ;
55
61
}
56
62
57
- attrs . push ( `width=${ attr ( width ) } ` ) ;
58
- attrs . push ( `height=${ attr ( height ) } ` ) ;
63
+ attrs . push ( `width=${ attr ( widthAttr ) } ` ) ;
64
+ attrs . push ( `height=${ attr ( heightAttr ) } ` ) ;
59
65
60
66
return {
61
67
type : 'html' ,
0 commit comments