TinyLetter, smaller images

Every now and again I write something for a newsletter hosted by TinyLetter. It’s mainly an excuse for me to practice my Stata, R and Python skills while focusing on something that isn’t work-related. Plus it’s fun.

I’ve loved TinyLetter so far with the exception of two annoyances. First, TinyLetter abuses any images greater than 600px wide, squashing and distorting them to fit into pre-defined bounds. It’s irritating to see my painstakingly made graphs look like they’re a funhouse mirror reflection.

Second: as best as I can tell, TinyLetter doesn’t directly support Markdown. Since I prefer to do most of my writing in Markdown, this is something of a bummer.

So I decided to channel my inner Drang and kill two birds with one script. My normal writing workflow is to put a .markdown file and all of the images in a folder for each project/newsletter issue. This script does the following:

  1. Uses Pandoc to convert the .markdown to an .html file
  2. Resizes the images using ImageMagick and puts them in a new folder
  3. Optimizes the images for the web with ImageOptim

Before we get to the code, let me warn you that my shell scripting skills are, uh, in development. With that caveat, here it is:

    #!/bin/sh
    
    # Convert the .markdown file to .html
    for draft in *.markdown
        do pandoc "$draft" -o "$draft".html
        done
    
    # make a directory for the resized images. Ideally I'd check to see if there are images to be converted first, but not today.
    mkdir resizedImages
    
    
    jpgs=(*.jpg) # if there are .jpg files, will create an array consisting of names of the files. Otherwise, will create an array with a single element *.jpg.
    if [ "$jpgs" != "*.jpg" ] # if the array doesn't equal literally "*.jpg", then the directory must have had jpgs.
    then
        for a in *.jpg
            do convert "$a" -resize 600x resizedImages/"$a"
            done
    fi
    
    pngs=(*.png)
    if [ "$pngs" != "*.png" ]
    then
        for a in *.png
            do convert "$a" -resize 600x resizedImages/"$a"
            done
    fi
    
    # Change to the resizedImages directory and use ImageOptim to save them for the web. Note that ImageOptim gives a "Missing setter or instance" error. This is a known issue with ImageOptim that's harmless.
    cd ./resizedImages
    
    if [ "$jpgs" != "*.jpg" ]
    then
        /Applications/ImageOptim.app/Contents/MacOS/ImageOptim *.jpg
    fi
    
    if [ "$pngs" != "*.png" ]
    then
        /Applications/ImageOptim.app/Contents/MacOS/ImageOptim *.png
    fi
  

This saves me a bit of time when I post a newsletter. Eventually, I’ll adapt it to my academic workflows, too.