How to get the markdown file path when write a markdown-it plugin

242 views Asked by At

I am writing a markdown-it plugin, which can convert image file to a Base64 type. e.g.

![foo](./foo.png)

And the render result should be

<p><img src="data:image/jpeg;base64,/9j/2dkaAAA=="/></p>

So I write a little plugin like this

let defaultImageRender=md.renderer.rules.image;
md.renderer.rules.image=function(tokens,idx,options,env,self){
    let token=tokens[idx];
    let relativeImgSrc=token.attrs.filter(x=>x[0]==='src').map(x=>x[1])[0];
    let imageBase64=convertToBase64(relativeImgSrc);
    token.attrSet('src',imageBase64);
   return defaultImageRender(tokens,idx,options,env,self);
}

Because I can only get the path which is relative to the markdown file path not my code path, the code throw an error. Is there anyway I can get the markdown file path in plugin or is there any other way which can implement the feature?

0

There are 0 answers