library(glue) # For string interpolation
library(fs) # For file system operations
library(purrr) # For functional programming and iteration
Introduction
In this tutorial, we’ll walk through an R script that automatically generates a Quarto blog post from images stored in a directory. If you have a collection of images and want to quickly create a visually appealing blog post, this guide will help you automate the process.
This code generates Markdown, which is itself code, that can then be built and published to a site. Essentially, it acts as a Markdown generator, producing properly formatted Markdown for your images. Once the Markdown file is created, you can build and publish it using Hugo, Quarto, Jekyll, Gatsby, or any other Static Site Generator (SSG) of your choice.
In a future post, I will show you how to create a Shortcut to convert lots of heic
images to webp
images for much smaller file sizes. I’m assuming you already have a folder full of optimized images.
Loading Required Libraries
Before we begin, we need to load the necessary R libraries that will help us manipulate file paths, generate markdown content, and handle string operations.
These libraries make sure that we can scan directories, manipulate strings, and generate our final Quarto markdown file.
Setting Up Directories and Output File
We define the directory containing the images and specify the output file name.
<- "images/" # Folder where images are stored
image_dir <- "index.qmd" # Output file for the blog post output_file
Checking if the Image Directory Exists
Before proceeding, we must ensure that the specified image directory exists. If it does not, the script will stop execution and display an error message.
if (!dir_exists(image_dir)) {
stop(glue("Directory '{image_dir}' does not exist. Please check the path."))
}
This prevents errors down the line by ensuring that we have a valid directory before processing files.
Listing Image Files
Next, we list all image files in the directory. The dir_ls
function retrieves files matching specific extensions: .jpg
, .jpeg
, .png
, .gif
, and .webp
.
<- dir_ls(image_dir, regexp = "\\.(jpg|jpeg|png|gif|webp)$") image_files
If no images are found, the script stops execution and prompts the user to add images.
if (length(image_files) == 0) {
stop(glue("No image files found in '{image_dir}'. Please add some images."))
}
Selecting a Featured Image
The first image in the list is chosen as the featured image. This is useful for blog platforms that require a cover image.
<- image_files[1]
featured_image <- glue("{featured_image}") # Ensure proper formatting featured_image_path
Generating Markdown Content for Each Image
To display images in the blog post, we create a function that generates markdown content for each image. The function extracts the file name, removes the extension, and formats it into a markdown-friendly structure.
<- function(image_path) {
generate_markdown <- path_file(image_path) # Extract file name
image_name <- path_ext_remove(image_name) # Remove file extension
base_name
# Generate markdown with title and image
glue("\n## {base_name}\n\n\n")
}
We then apply this function to all images using map_chr
.
<- map_chr(image_files, generate_markdown) markdown_content
Creating the Quarto Metadata
Every Quarto post requires metadata, such as the title, author, date, categories, and featured image.
<- c(
metadata "---",
"title: 'My Photo Blog Post'",
"author: 'Your Name'",
glue("date: '{Sys.Date()}'"), # Automatically insert the current date
"categories: ['Photography', 'Hiking']",
glue("image: '{featured_image_path}'"), # Set the featured image
"format: html",
"---\n\n"
)
Combining Metadata and Markdown Content
Finally, we merge the metadata with the generated markdown content to form the complete blog post.
<- paste0(paste(metadata, collapse = "\n"), "\n", paste(markdown_content, collapse = "\n")) final_post
We then write this content to the index.qmd
file.
writeLines(final_post, output_file)
Completion Message
Once the script successfully generates the markdown file, we display a message to notify the user.
message("Markdown post generated successfully at: ", output_file)
Conclusion
This script automates the process of creating a Quarto blog post from images stored in a directory. By leveraging R’s file system handling, string interpolation, and functional programming capabilities, we efficiently generate a structured markdown file ready for publishing.
Try running this script on your own collection of images and see how it works for you!
Full Source
Find the full source doe repositary here.
Credit
I give full credit to Jaydey Ryan for their excellent instructions on setting up continuous deployment with Netlify and GitHub. Thank you!