Markdown on caffeine.
Have you ever wanted a bit more from your markdown files?
Introducing Markdown.
Mmarkdown takes a plain markdown file and generates a copy of it.
It starts to be less boring when you add fenced code blocks with the language identifier set to mmd
.
For example:
will output:
Hello Jessie
How it works
Everything that is returned (as a string) from the code in a block will be interpreted and replaced to the block in the output file.
It's full async, which is cool, lots of awaits
are waiting for you there but soon enough you will face a problem: too much code to write in a markdown file! Terrible experience!
The solution in mmarkdown is in the scripts
option.
You can pass the path of a javascript module in your app to the mmarkdown command.
The module that the scripts file returns will be passed to the context of the fenced block, eg:
This block:
//scripts is passed
const array = [1, 3, 5]
const something = await scripts.processMyArray(array)
const myFinalString = something.map(item => '#### ' + item.name)
.join('\n\n')
return myFinalString
and this script file:
module.exports = {
processMyArray: async array =>
new Promise(resolve => {
setTimeout(() => {
resolve(
array.map(item => ({
name: item + ' async'
}))
)
}, 1000)
})
}
will output:
1 async
3 async
5 async
(The setTimeout is there just for demo purposes)
Backup
The backup
option, false by default, will make a copy of the current output file, postfix it with a timestamp and move it into backupPath
.
Can you imagine an use for this?
Share it in the comments!
Thanks for reading!
Don't forget to add me on Twitter ok? :)
Top comments (2)
I can't imagine many uses for this, because one of the main draws of markdown is that it's human-readable and can be displayed by dumb services.
You could conceivably use it as your page content in a CMS, but I don't really like the idea of mixing logic with editable content.
Hello Ben,
I do not image many uses of this as well, although this is a good one I think: github.com/MicheleBertoli/css-in-js
To mitigate the feeling of working with content and logic I introduced the 'scripts' option in the mmarkdown config. The logic would happen there, the mmd fenced block in the markdown would just output something, or do some operations on the final output, such as a loop generating the relevant markdown.