Hey everyone! I've done the manual conversion of images in markdown files to base64 before, but I was wondering if there's a script out there that can automate this process. Just to give some context, I want to convert images to base64 and embed them directly into my markdown documents. This way, I can have a single file that contains everything I need for a tutorial without depending on external hosted images, which can disappear or be inaccessible later. If anyone knows of a script that can scan through a markdown file and replace all image links (whether they are stored locally or hosted online) with base64 encoded images, I would really appreciate your help! Thanks!
6 Answers
You could use coreutils with a base64 encoder and combine it with some `sed` or `awk` tricks to find and replace the images in your markdown. Here's a handy command to get you started: [base64 encoding](https://ss64.com/bash/base64.html). It’s worth checking out!
I never realized you could use base64 in markdown, but that’s super interesting! When you render markdown with embedded images, the HTML will convert the image syntax to standard HTML `` tags, and it should display correctly as long as the renderer supports it.
For repositories, I suggest hosting the images within the same repo. That way, you won't have to deal with broken links if the original hosting gets deleted.
If you're using Vim, you can also convert images manually: open the file, go to where you want to insert the base64 image, and use the command `:r!base64 -w0 file.png`. Just remember to save your changes after!
Not the solution you’re looking for, but I found a Python markdown parser that seems like it might work for you. You could customize how images are rendered by overriding the methods. Check out the GitHub repo [here](https://github.com/lepture/mistune).
If you want to give `awk` a shot (though I haven't tested it), you can start with this command: Use `wget` to download the images, then convert them to base64. I put together a script that does this for you. I hope it helps!
Exactly! A good markdown renderer will read that base64 URI and handle it just like any other image source. You won't run into issues unless the renderer itself has limitations.