Have you ever stumbled upon a beautiful image in a Markdown file and wondered what it depicts?
Or perhaps you’re aiming to make your content more accessible by providing descriptive alt text for all the visuals? 🖼️✨
In this sample, we’ll dive into a GenAIScript tool that automates the generation of alt text for images in Markdown files,
making your content more inclusive and SEO-friendly!
👩💻 Understanding the Script Code
This script is composed of TypeScript code, designed to run with the GenAIScript CLI. Let’s break it down:
Here we declare the script with a title and description, specifying it uses OpenAI’s GPT-4 model. We also set parameters for the file paths, choice to regenerate all descriptions, and the assets path.
Next, we extract environmental variables:
Configuration is crucial, as it allows customization of the script’s behavior.
Now, let’s create a helper function:
This function resolves the URL for the image, determining its absolute path based on different types of relative URLs.
Following that, we define a regular expression to find images in Markdown:
This regex captures different scenarios based on whether we want to force regenerate all image descriptions or just the ones missing alt text.
We then use this regex to find relevant files:
The script uses workspace.grep to find all occurrences of the regex pattern in the specified documents.
For each image URL found, we generate alt text:
In a loop, we process each file, generate the alt text using an AI model, and store it, making sure to strip out any brackets that could interfere with Markdown syntax.
Finally, we update the Markdown content with the generated alt text:
We replace the placeholder in the original content with the alt text and save the updated file.
💻 How to Run the Script
To run this script, you’ll need the GenAIScript CLI. If you haven’t installed it yet, check out the installation guide.
Once you have the CLI, you can run the script with the following command:
This command will execute the script using the filename without the .genai.mts extension. The script will search for Markdown files, generate alt text for images without descriptions or update existing ones if the force option is used.
🧑💼 Make sure to navigate to the directory where your Markdown files are located before running the script.
That’s it! You now have the power to enhance your Markdown files with descriptive alt text, making your content more accessible and engaging for all your readers.