Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mermaid support #637

Closed
3esmit opened this issue Jan 5, 2020 · 6 comments
Closed

Mermaid support #637

3esmit opened this issue Jan 5, 2020 · 6 comments
Assignees
Labels

Comments

@3esmit
Copy link

3esmit commented Jan 5, 2020

How can I use kramdown with mermaid code blocks?

I am trying to implement this for fixing ethereum/EIPs#2450

I tried finding a way of making a custom behavior when ```mermaid blocks are found, which would be simply HTML escaping and wrapping around <div class="mermaid> </mermaid> and leaving the rest for js.

Can this be done through configuration?

@gettalong gettalong self-assigned this Jan 5, 2020
@gettalong
Copy link
Owner

You need to write a syntax highlighter for this, see https://kramdown.gettalong.org/rdoc/Kramdown/Converter/SyntaxHighlighter.html. Your highlighter should probably delegate to another one like rouge for all other highlighting types.

@3esmit
Copy link
Author

3esmit commented Jan 6, 2020

Hi. Thanks for the answer. I was navigating in that link you gave me, but I have a bit of difficulty as I never developed in ruby, but I can learn fast by examples.
Can you please provide me an example of plugin module using SyntaxHighligher?

Perhaps would be interesting to have this module by default in future, as mermaid is getting more popular... LaTeX is very powerful, but its syntax becomes a barrier to most of people, and mermaid came to help with a more "markdownish" way of making common graphs.

@gettalong
Copy link
Owner

@gettalong
Copy link
Owner

If you wanna create something that other people can use too, the second link should be helpful as this is a syntax highlighter for kramdown, delivered in its own Rubygem package.

@3esmit
Copy link
Author

3esmit commented Jan 6, 2020

Thanks, that is very helpful!

For mermaid we would only need to import the js at the html header, and warp it around some divs... https://mermaid-js.github.io/mermaid/#/usage?id=define-a-chart-like-this

I guess for mermaid my plugin would look like this:

require 'kramdown/converter'

module Kramdown #:nodoc:

  module Options #:nodoc:
    define(:enable_mermaid, Boolean, true, <<EOF)
Enables mermaid
EOF
  end

  module Converter #:nodoc:
    module SyntaxHighlighter #:nodoc:
      module Mermaid

        VERSION = '0.0.1'

        def self.call(converter, text, lang, type, call_opts)
          return nil unless converter.options[:enable_mermaid]
          if lang && lang == "mermaid"
            "<div class=\"mermaid\">\n" << text << "\n</div>"
          else
            nil
          end
        rescue StandardError
          converter.warning("There was an error using Mermaid: #{$!.message}")
          nil
        end

        def self.options(converter, type)
          nil
        end

        
    end
    
    add_syntax_highlighter(:mermaid, SyntaxHighlighter::Mermaid)

  end

end

Not sure if it's worth packing such a simple code? Maybe it would be interesting to have a "graphs" plugin that support many of those, such as mermaid and plantuml...

@3esmit
Copy link
Author

3esmit commented Jan 6, 2020

Seems like https://gitlab.com/gitlab-org/gitlab_kramdown would extend kramdown to have the features I am looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants