mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.52k stars 240 forks source link

No charts found when space between opening code fence and language name #623

Open PizzapunkDev opened 12 months ago

PizzapunkDev commented 12 months ago

Describe the bug When there's a space between the code fence ticks ``` and the mermaid language definition, the CLI fails to find mermaid definitions and generate images. Including the space in a client-side integration, such as GFM, accepts the space and still renders the diagram as expected.

To Reproduce

Create a markdown file with whatever Mermaid chart you wish, but instead of defining it as this:

```mermaid [chart definition goes here] ```

define it like this (noting the space between the last backtick and the mermaid language definition):

``` mermaid [chart definition goes here] ```

and run your favourite method of generating diagrams with the CLI. It should report the error No mermaid charts found in Markdown input

For demonstration, here is GFM rendering it with the space included:

sequenceDiagram
    Alice->>John: just a single space today john?

Or indeed, with an arbitrary number of spaces, as implied by the GFM spec:

sequenceDiagram
    Alice->>John: wow its wild how many spaces i can use

Expected behavior CLI works like the client-side rendering (Github, Gitlab tested), and accepts spaces between the code block and the language name, so that it can be detected by the CLI and rendered appropriately alongside the other options.

Tested on both latest version of Docker image and also running via npx as stated in the official documentation.

PizzapunkDev commented 12 months ago

I suppose this line about not rolling your own markdown parser proved to be prophetic 😉

aloisklink commented 12 months ago

I suppose this line about not rolling your own markdown parser proved to be prophetic 😉

:laughing:

The main reason we haven't switched yet is that:

However, we're planning on making a v11 release once Mermaid makes a v11 release, and we're planning on finally dropping support for Node.JS v14 & Node.JS v16. So dropping support for some more unusual Markdown implementations might be okay too.


As a temporary measure, here are two remark plugins that actually use a real markdown parser that you can use:

PizzapunkDev commented 12 months ago

Sounds like a plan! My toolchain involves pandoc for manipulating and combining markdown files before running mermaid-cli, so for future Googlers in the same situation for now, my solution was to write a very crude Lua filter to 'process' the mermaid code blocks explicitly:

function CodeBlock(elem)
    local language = pandoc.utils.stringify(elem.classes)
    if FORMAT:match 'gfm' and language == 'mermaid' then -- don't forget to change the markdown format here or make this more generic if needed
        return {
            pandoc.RawInline('markdown', '```mermaid'), -- basically just hardcode the block without the space
            pandoc.RawBlock('markdown', elem.text),
            pandoc.RawInline('markdown', '```')
        }
    end
    return elem
end