Skip to content

Custom language handlers

Dawa Ometto edited this page Mar 23, 2023 · 3 revisions

The Gollum API allows defining custom behavior for rendering codeblocks for specific languages. The rendering of Mermaid diagrams is implemented in this way and may serve as an example:

Gollum::Filter::Code.language_handlers[/mermaid/] = Proc.new { |lang, code| "<div class=\"mermaid\">\n#{code}\n</div>" }

This snippet ensures that code blocks that define their language as mermaid...

```mermaid

...will be rendered as the div returned by the Proc.

The language definition (the string after the triple backticks) must match the regular expression in Gollum::Filter::Code.language_handlers[regex] for the Proc to be called.

The Proc is passed two arguments: the String language of the codeblock, and its contents.

You can add a custom language handler to your config.rb, so there's no need to alter Gollum's source code.