Adding a media preview and gallery helper to blog poster

I've had gastro the last WEEK. While on holidays. So I needed a win. Decided to scratch an itch.


When I add images into my simple Post Uploader (that commits the markdown file to Bitbucket and triggers all the generator logic), I don't have any helpers for making my markdown gallery. I just have to run a preview function and the page submits and puts all the image names into a markdown gallery template. But I have to guess the files to write captions for them.


A quick addition of JavaScript on the client side and now it gives me a preview of each image and an input text field to write the caption on. It then gives me the markdown combining images to captions. Since it needs JavaScript to function, I've written it to create the elements when invoked rather than having empty elements that serve no function without JavaScript.

// Media preview
var mediaElement = document.querySelector("#media"),
    uprev =document.createElement('DIV') ,
    mprev =document.createElement('DIV') ;
uprev.setAttribute('id', 'upload-preview');

// This function is called when text changes in the inputs
updateMediaMarkdown = function(e) {
    // This is the start of a gallery
    mprev.innerText = `<section class="gallery-2020-4">` + "\n";
    var now = new Date();
    // For each `li` of the template outputs:
    document.querySelectorAll('#upload-preview ul li').forEach((e) => {
        var name = e.querySelector("img").getAttribute('data-name'),
            thumb = name.replace(/(.jpg|.gif|.png|.webm|.jpeg)$/ig, "-thumb.jpg"), 
            cap = e.querySelector("input").value;
            // This is a template to add the date to the media file as well as the autothumbnail
        mprev.innerText += `[![${cap}](/blog/media/${now.toISOString().substring(0,10).replaceAll('-','/')}/${thumb} "${cap}")](/blog/media/${now.toISOString().substring(0,10).replaceAll('-','/')}/${name})` + "\n";
    mprev.innerText += `</section>`;

// When we alter the media files upload whatsit..
mediaElement.onchange = function(e) {
    // Clear areas
    document.querySelector("#upload-preview").innerHTML = "<ul></ul>";
    document.querySelector("#markdown-preview").innerHTML = "";
    // Process individual uploaded files
    Array.from(mediaElement.files).forEach((file) => {
        const fileReader = new FileReader();
        const fileName =;
        // Once we've got the image content in the js...
        fileReader.addEventListener("load", function() {
            var img = document.createElement("IMG"),
                caption = document.createElement("INPUT")
                li = document.createElement("LI");
           // Create a new LI with the image+input template
            img.setAttribute("src", this.result);
            img.setAttribute('data-name', fileName);
            // Here's where we attach the change-text function
            caption.onchange = updateMediaMarkdown;
            document.querySelector("#upload-preview ul").append(li);