Skip to content

Instantly share code, notes, and snippets.

@flyx
Created February 18, 2023 12:41
Show Gist options
  • Save flyx/9fff080cf4edc95d495bc661a002232c to your computer and use it in GitHub Desktop.
Save flyx/9fff080cf4edc95d495bc661a002232c to your computer and use it in GitHub Desktop.
Simple plugin to use mermaid.js in a jekyll-asciidoc + just-the-docs setup

Requires a working mermaid setup for just-the-docs.

Place the file below in the _plugins folder of your site. Now in your AsciiDoc files, you can do

[mermaid]
....
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
....

Stolen and minimally edited from GitLab, under MIT

Conflicts with AsciiDoctor Diagram, which renders during site generation.

begin
require 'asciidoctor'
class MermaidBlockProcessor < ::Asciidoctor::Extensions::BlockProcessor
use_dsl
named :mermaid
on_context :literal, :listing
parse_content_as :simple
def process(parent, reader, attrs)
create_mermaid_source_block(parent, reader.read, attrs)
end
private
def create_mermaid_source_block(parent, content, attrs)
# If "subs" attribute is specified, substitute accordingly.
# Be careful not to specify "specialcharacters" or your diagram code won't be valid anymore!
subs = attrs['subs']
content = parent.apply_subs(content, parent.resolve_subs(subs)) if subs
html = %(<div><pre class="language-mermaid">#{CGI.escape_html(content)}</pre></div>)
::Asciidoctor::Block.new(parent, :pass, {
content_model: :raw,
source: html,
subs: :default
}.merge(attrs))
end
end
::Asciidoctor::Extensions.register do
block MermaidBlockProcessor
end
rescue LoadError
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment