11import marked from 'marked' ;
22
3- // Source: https://github.com/markedjs/marked/issues/339
3+ // Original source: https://github.com/markedjs/marked/issues/339
4+ // our version:
45// 
56export function changeImgCreation ( renderer : marked . Renderer ) {
67 renderer . image = ( src : string | null , title : string | null , alt : string ) => {
7- const getWidthAndHeightStyle = checkForWidthAndHeight ( title ) ;
8- if ( getWidthAndHeightStyle . length ) {
9- return `<deckgo-lazy-img img-src="${ sanitize ( src ) } " img-alt="${ sanitize ( alt ) } " style="${ getWidthAndHeightStyle } "></deckgo-lazy-img>` ;
10- }
11- return `<deckgo-lazy-img img-src="${ sanitize ( src ) } " img-alt="${ sanitize ( alt ) } "></deckgo-lazy-img>` ;
8+ const style : string = checkForWidthAndHeight ( title ) ;
9+ return `<deckgo-lazy-img img-src="${ sanitize ( src ) } " img-alt="${ sanitize ( alt ) } " style="${ style } "></deckgo-lazy-img>` ;
1210 } ;
1311}
1412
@@ -20,15 +18,21 @@ function sanitize(str: string) {
2018 } ) ;
2119}
2220
23- function checkForWidthAndHeight ( title ) {
24- const isWidthSet = / w i d t h : \d + p x / . exec ( title ) ;
25- const isHeightSet = / h e i g h t : \d + p x / . exec ( title ) ;
21+ function checkForWidthAndHeight ( title ) : string {
22+ const isWidthSet : RegExpExecArray | null = / w i d t h : \d + (?: p x | e m | e x | c h | r e m | v w | v h | v m i n | v m a x | % ) / . exec ( title ) ;
23+ const isHeightSet : RegExpExecArray | null = / h e i g h t : \d + (?: p x | e m | e x | c h | r e m | v w | v h | v m i n | v m a x | % ) / . exec ( title ) ;
24+
2625 let style : string = '' ;
27- if ( ! ! ( isWidthSet && isHeightSet ) ) {
28- const width = / \d + p x / . exec ( isWidthSet [ 0 ] ) [ 0 ] ;
29- const height = / \d + p x / . exec ( isHeightSet [ 0 ] ) [ 0 ] ;
26+
27+ if ( isWidthSet ) {
28+ const width = / \d + (?: p x | e m | e x | c h | r e m | v w | v h | v m i n | v m a x | % ) / . exec ( isWidthSet [ 0 ] ) [ 0 ] ;
3029 style += `--deckgo-lazy-img-width: ${ width } ;` ;
30+ }
31+
32+ if ( isHeightSet ) {
33+ const height = / \d + (?: p x | e m | e x | c h | r e m | v w | v h | v m i n | v m a x | % ) / . exec ( isHeightSet [ 0 ] ) [ 0 ] ;
3134 style += `--deckgo-lazy-img-height: ${ height } ;` ;
3235 }
36+
3337 return style ;
34- } ;
38+ }
0 commit comments