0

I am trying to convert relative image in markdown to image base64 and then insert in markdown.

I tried with remark and below custom plugin

import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { promisify } from 'util';
import * as mimes from 'mime-types';
import {visit} from 'unist-util-visit';
import {is} from 'unist-util-is';
export default function remarkTest() {
    return (tree:any, file:any, done: any) => {
        const mdParentFolder = path.dirname(vscode.window.activeTextEditor?.document.uri.fsPath || "");   
        visit(tree, 'image', function(node, index, parent) {
            let imagePath = path.resolve(mdParentFolder, node.url);
            const mime = mimes.lookup(path.extname(node.url));
            const imageBase64 = fs.readFileSync(imagePath, { encoding: 'base64' });
            node.url = `data:${mime};base64,${imageBase64}`;
        }, true);  
    };
  }

While debugging, I found the imageBase64 will not return the correct base64 for my image.

Edward
  • 28,296
  • 11
  • 76
  • 121

0 Answers0