Ruby extensions for Markdown
Sadly, as far as I know CommonMark currently doesn’t include anything about ruby in its spec. On top of that ruby is pretty uncommon, so it is pretty rare for any ruby extensions to exist. As I move through any new frameworks, I will try to document any simple solutions that I figure out.
Examples
Language | Example |
---|---|
Japanese | 日本語の文法は難しい |
Chinese | 北京 北京 |
Korean | 韓國 |
Vietnamese | 河內 |
Other | I love ruby! |
Remark
Recently I moved to Astro, which generally uses JavaScript tools for parsing and manipulating markdown. In particular that’s the Remark and Rehype from Unified.
When looking for a way to extend remark I first looked for an existing plugin which would allow me to automatically convert custom shorthands for ruby inserted into Markdown. I found a plugin called remark-ruby, but honestly after looking at its source code I decided to hand roll my own solution. It just looked overcomplicated for something that should be simple and easy to modify (for me).
I was able to write a really simple and short solution using a pair of Regexes working in conjunction to split strings and replace custom ruby shorthands with HTML, which then passes through to Rehype.
Usage:
{日本語}(にほんご)の{文法}(ぶんぽう)は{難}(むずか)しい
Zola
The following is a snippet for the Tera templating engine which is inspired by Jinja2.
Usage:
{{ ruby(expr="日本語,にほんご;の;文法,ぶんぽう;は;難,むずか;しい") }}
Hugo
The following is a snippet for the Golang templating engine used by Hugo.
Usage:
{{ ruby "日本語,にほんご;の;文法,ぶんぽう;は;難,むずか;しい" }}