You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to do some additional transformations to src attribute of img tags and I am using typescript, so I have done a bit of refactoring to rehype-img-size plugin:
importpathfrom'path';import{visit}from'unist-util-visit';importsizeOffrom'image-size';importtype{Plugin}from'unified';importtype{Node}from'unist';// eslint-disable-next-line @typescript-eslint/explicit-function-return-typeconstgetImageSize=(src: string,dir: string)=>{// Handles "//" such as "http://" "https://" "ftp://"if(src.match(/^(?:[a-z]+:)?\/\//)){returnundefined;}// Treat `/` as a relative path, according to the serverconstshouldJoin=!path.isAbsolute(src)||src.startsWith('/');letsrcPath=src;if(dir&&shouldJoin){srcPath=path.join(dir,src);}returnsizeOf(srcPath);};interfaceTagNodeextendsNode{tagName: '';}interfaceImgNodeextendsNode{tagName: 'img';properties: {src: string;width: number;height: number;};}typeElementNode=TagNode|ImgNode;typePluginParameters=[{dir: string;},];constrehypeImgSize: Plugin<PluginParameters>=({ dir })=>{return(tree,_file)=>{visit(tree,'element',(node: ElementNode)=>{if(node.tagName==='img'){const{ src }=node.properties;constdimensions=getImageSize(src,dir);node.properties.width=dimensions?.width??0;node.properties.height=dimensions?.height??0;}});};};exportdefaultrehypeImgSize;
I wonder if there is an interest in updating this project to typescript either as the code above or in jsdoc syntax flavour? In either syntax typescript can automatically generate typings and replace some unit testes by compile-time checks.
The text was updated successfully, but these errors were encountered:
Thank you for sharing
rehype-img-size
!I needed to do some additional transformations to
src
attribute ofimg
tags and I am using typescript, so I have done a bit of refactoring torehype-img-size
plugin:I wonder if there is an interest in updating this project to typescript either as the code above or in jsdoc syntax flavour? In either syntax typescript can automatically generate typings and replace some unit testes by compile-time checks.
The text was updated successfully, but these errors were encountered: