Generate Markdown files
Ever wondered how to DRY (Don’t Repeat Yourself) in a README.md file? Or generate contents in any Markdown file? As it turns out, it’s quite simple.
Table of contents
Markdown & JavaScript file
Create a Markdown file as usual. Then in the places where you want to have generated contents, put:
Where foo is the name of your function. You can have many functions, and you put them all in a complementary JavaScript file:
Because it’s a regular JavaScript file, you can include all files from your projects and generate contents with e.g. settings, default values, API functions, CLI parameters, etc.
By the way, I’ve put together a small collection of tools I use regularly — things like time tracking, Git helpers, database utilities, and a few others that make development a bit smoother.
It is also possible to include the JavaScript code directly in the mmd blocks and get rid of the supporting JavaScript file.
Generator
First, install the mmarkdown npm package as a devDependency.
As part of your build process, run mmarkdown. In the simplest case, it can be a separate npm script. Also include the configuration in package.json: