News for hackers. Stuff that matters.
Re:Include diagrams in your Markdown files with Mermaid by timwis on Monday February 14, 2022 @01:04PM (#30335516)
I’m super excited for this!

One thought though, on the syntax. Wouldn’t it be a bit odd if suddenly this line in a README.md:

   ```js
   const foo = 1 + 1
   ```
Rendered as:

   `2`
? Isn’t that kind of what we’re doing here with the mermaid source tag? That tag is for showing source code, no? Feels like there should be another tag for rendering it.
Re:Include diagrams in your Markdown files with Mermaid by alaroldai on Monday February 14, 2022 @07:24PM (#30340709)
I’ve actually been using an almost identical filter for GraphViz markup in my university papers for the last year, except I added a leading exclamation point to the language tag to distinguish between included source and included markup:

  ```!dot
  (Markup)
  ```
I wish they’d adopted something like that instead - I have no idea how you’d include highlighted Mermaid source in a GH markdown file.
Re:Include diagrams in your Markdown files with Mermaid by Ajedi32 on Monday February 14, 2022 @02:39PM (#30337131)
Agreed; this is an annoying inconsistency. If you want to embed and render a non-Markdown file in Markdown, there's already another, far better syntax for that which is more in-line with the spirit of Markdown's "just text" philosophy:

![Chart](./charts/chart-file.mermaid)

Re:Include diagrams in your Markdown files with Mermaid by Arnavion on Monday February 14, 2022 @03:03PM (#30337498)
It's nice to have the diagram source in the same file that uses it, instead of a separate file that needs to be kept in sync.
Re:Include diagrams in your Markdown files with Mermaid by dijit on Monday February 14, 2022 @03:22PM (#30337736)
Since it’s sufficiently different to how it’s rendered its really more like an SVG than a table.

So, the question is would you rather have SVGs inline?

Personally I think the reference to another file is a perfectly fine compromise, as the content is sufficiently different to no longer be markdown.

Re:Include diagrams in your Markdown files with Mermaid by brainfish on Monday February 14, 2022 @03:53PM (#30338157)
I think this is a false equivalence. The content of SVGs is non-semantic; to get from SVG code to Meaning one needs to do some sort of rendering and re-interpreting of the resulting image. Even if for some simple images that could be done in-brain, that is not usually the case. Whereas something like Mermaid is intended to be meaningful both as code and as rendered output, just like Markdown itself. Having that additional meaning inline can be very helpful to faster understanding of the content, which is not usually going to be the case for inline'd SVG.
Re:Include diagrams in your Markdown files with Mermaid by dijit on Monday February 14, 2022 @04:48PM (#30338850)
I don't know if it's a false equivalence, because I've read (and certainly written) a fair number of mermaid diagrams which are extremely hard to understand without seeing the rendered content.
Re:Include diagrams in your Markdown files with Mermaid by Arnavion on Monday February 14, 2022 @03:37PM (#30337951)
It's subjective, in my opinion. I might be fine with inline SVGs if they were "smaller", ie didn't have the doctype and root element and general XML verbosity on all nodes. PlantUML / Mermaid have a much more to-the-point syntax.

Also, I personally hate markdown tables since editing even a single cell means that I likely have to then resize either the entire row or the entire column, or both. So when I do need tables I use HTML tables anyway.

At any rate, a middle-ground might be to have the diagram source as references at the end of the document, and reference those from the use site, like `![Chart][#chart]` or something. So you don't have to see the diagram source inline if you don't like it, but it's still there in the same file.

Re:Include diagrams in your Markdown files with Mermaid by asiachick on Monday February 14, 2022 @04:50PM (#30338884)
Yes, it sucks. This is why we can't have nice things. Because a million different programmers always choose some random incompatible interpretation and then we're all stuck with it.

Another similar example, all of VSCode's configuration files are called filename.json but they aren't actually json (any spec compliant json parser would barf on them).

They could just have easily named them filename.jsonc (or whatever they actually are) but they didn't so we're stuck with all these .json files that json parsers won't actually parse and special hacks to validate them differently based on like if they're in a .vscode folder or if their name is some specific filename.

Unfortunately there's no way to wrangle millions of programmers so we're stuck with whatever bad decisions become popular.

IF that's not clear above, for example, trailing commas and comments are not allowed in JSON but they are in JSONC. You'd like your editor to highlight errors. Typically this is done by filename extension. .json = comments show as errors, .jsonc = comments OK. But VSCode named it's .jsonc files as .json so every editor that wants to be able to show if there are errors in your file now needs some random heuristic to decide how to interpret the file..... all because of a bad decision by 1 or 2 programmers that ended up being popular and now their too entrenched to fix it.

The same thing is true here. ``` is supposed to mark a codeblock as in show the text as is, whitespace as is, line breaks as is. If you want to write a tutorial on how to use mermaid in markdown you'd want ``` to show the mermaid source. Use some other tag for rendering. But now where stuck with an exception and more will be added.

And before you go say "there's no official spec", so what!? It's called being consistent and interpreting the contents of a ``` block is inconsistent.

Re:Include diagrams in your Markdown files with Mermaid by anchpop on Tuesday February 15, 2022 @12:58AM (#30343297)
If I could have one wish, I'd replace file extensions with a hash of the spec and have someone maintain a lookup table from spec hash -> extension for rendering
Re:Include diagrams in your Markdown files with Mermaid by account42 on Tuesday February 15, 2022 @04:20AM (#30344344)
That prevents any possibility for progressive enhancement though as your old renderer won't know the hashes for new but still mostly compatible spec versions.
Re:Include diagrams in your Markdown files with Mermaid by mminer237 on Monday February 14, 2022 @02:13PM (#30336774)
I agree. R Markdown has R code execution as part of it's Markdown flavor and it distinguishes code block vs code execution as ```R vs ```{R}
Re:Include diagrams in your Markdown files with Mermaid by sitkack on Monday February 14, 2022 @06:40PM (#30340194)
It feels like Markdown and Jupyter notebooks in on the path to merging, and markdown is like the dumbed (need a different word for this, simplicated (though that that word is cumbersome)) down yaml which is a simplicated xml.

Why don't we just go back to xml and provide decent structured editors?

Re:Include diagrams in your Markdown files with Mermaid by rch on Monday February 14, 2022 @07:01PM (#30340471)
Lack of high-quality free editors was a massive problem with xml, particularly when xsd was involved.
Re:Include diagrams in your Markdown files with Mermaid by sitkack on Wednesday February 16, 2022 @07:59PM (#30368235)
Yaml is now worse than the thing it was designed to replace. Rather than solve the real problem (ungood editors), we prop up and extend a janky solution.
Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Monday February 14, 2022 @01:09PM (#30335593)
Only the mermaid identifier is supported for triple backticks blocks, you can't put js and have it spit out script that executes in the browser.
Re:Include diagrams in your Markdown files with Mermaid by e12e on Monday February 14, 2022 @01:22PM (#30335838)
I think op meant that we can:

   ```ruby
   # syntax highlighted as ruby
   a = 1 + 1
   ```
And:

   ```js
   // syntax highlighted as js
   a = 1 + 1
   ```
So maybe we should have something like a "bang" prefix to evaluate and inline/embed like:

   ```!ruby
   # evaluated 
 as ruby
   a = 1 + 1
   # renders 2 because of implicit return? 
   ```
Re:Include diagrams in your Markdown files with Mermaid by martinwoodward on Monday February 14, 2022 @02:15PM (#30336812)
The idea of the bang syntax came up. https://talk.commonmark.org/t/mermaid-generation-of-diagrams...

But in the end went with simple. There will be a very limited subset that will render this way - executing raw code without the users permission in the browser just opens up too much attack surface area that we’d need a very compelling use case to do the security work necessary to make it protected (even if that was possible)

Re:Include diagrams in your Markdown files with Mermaid by mintplant on Monday February 14, 2022 @04:21PM (#30338529)
Alright, but how do I embed highlighted Mermaid source code in Markdown now?
Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Tuesday February 15, 2022 @01:32AM (#30343450)
embed the HTML:

<pre><code> your mermaid code <span class="foo">with highlighted</span> sections </code></pre>

Use a tool like pygments to generate the highlighted HTML (assuming there even is highlighting for mermaid syntax).

What you're asking for is an uncommon edgecase that's not what people want in 99% of circumstances (instead they want the rendered chart). Markdown still gives you the HTML escape hatch so use it. Sure it's not pretty but it's no worse than if github didn't support highlighting mermaid source anyways.

If you don't want highlighting just put a fenced code block without the mermaid info string. It will render as unhighlighted code. IMHO that's what you'd want as a workaround to demo source like in a mermaid readme.

Re:Include diagrams in your Markdown files with Mermaid by mintplant on Tuesday February 15, 2022 @04:13AM (#30344284)
> What you're asking for is an uncommon edgecase that's not what people want in 99% of circumstances

It's how it worked before this change, and how it works for every other language in Markdown. This is taking away functionality with no reasonable replacement and creating a special-case gotcha in the Markdown syntax (the same syntax could produce highlighted code or a rendered image depending on the language and what features any particular Markdown implementation supports), and it's not clear why this is necessary when something like ```!mermaid would work just as well to trigger rendering. It's unforced bad language design.

> Use a tool like pygments to generate the highlighted HTML

This requires hardcoding colors into the generated HTML, so the highlighting won't match the color theme of the context that the document is embedded in, breaking e.g. dark mode.

Re:Include diagrams in your Markdown files with Mermaid by e12e on Monday February 14, 2022 @03:42PM (#30338025)
I don't think github should (generally) render stuff in the browser (although I see that both "benign"[1] graphs and source code highlights, not to mention the somewhat simple translation from Markdown to html - probably could be done in the browser as js/wasm.

But for "markdown the language" having a standard syntax for embed/evaluate along with prettify/highlight would've been nice - effectively a standard markup for multi-language "notebooks"/literate programs (maybe we need a triplet: format, render/embed, transclude/literate programming).

Github could then opt-in to highlighting the N languages they do, and initially only try to render mermaid graphs. Then perhaps also graphviz graphs. Etc.

[1] I'm sure one could do some minor DOS at least with a 500 mb graph..

Re:Include diagrams in your Markdown files with Mermaid by mukundesh on Wednesday February 16, 2022 @05:52AM (#30358446)
Would be good to copy from MkDocs Material

https://squidfunk.github.io/mkdocs-material/reference/code-b...

``` py hl_lines="2 3"

def bubble_sort(items):

    for i in range(len(items)):
        for j in range(len(items) - 1 - i):
            if items[j] > items[j + 1]:
                items[j], items[j + 1] = items[j + 1], items[j]
```
Re:Include diagrams in your Markdown files with Mermaid by tehbeard on Monday February 14, 2022 @01:20PM (#30335796)
You missed the point, that being; isn't triple backticks meant for whitespace preserving, highlighted in certain cases source code? And not embedding other file types.
Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Monday February 14, 2022 @01:25PM (#30335898)
No there's not a formal requirement or spec for how markdown blocks are rendered. Even things like syntax highlighting are optional choices different renderers make (and even the whole idea of highlighting is not specified or defined, how do you define the grammer, etc?).

Some tools in the computational notebook space use markdown with fenced code blocks as blocks of executable code, see for example jupytext: https://github.com/mwouts/jupytext/blob/main/docs/formats.md or myst markdown: https://myst-parser.readthedocs.io/en/latest/syntax/syntax.h... or nbconvert: https://nbconvert.readthedocs.io/en/latest/

Re:Include diagrams in your Markdown files with Mermaid by dijit on Monday February 14, 2022 @03:27PM (#30337822)
There is a formal spec (from github themselves) for how it’s rendered.

What’s debatable is what the spec does with the “info” section.

https://github.github.com/gfm/#fenced-code-blocks

Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Monday February 14, 2022 @03:41PM (#30337996)
Sure but there's no markdown spec. Markdown is purely a series of blog posts by John Gruber and a collection of different implementations. CommonMark is as close to as it's gotten to a formal spec, and even it says there is no requirement the info string (i.e. text like mermaid after backticks) be interpreted for specific rendering (or not rendering) of the content: https://spec.commonmark.org/0.28/#fenced-code-blocks
Re:Include diagrams in your Markdown files with Mermaid by acchow on Monday February 14, 2022 @02:35PM (#30337071)
It's rendering style, not execution.
Re:Include diagrams in your Markdown files with Mermaid by reificator on Monday February 14, 2022 @01:22PM (#30335843)
Which is the exact inconsistency under scrutiny here.
Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Monday February 14, 2022 @01:39PM (#30336168)
Markdown supports arbitrary HTML, you can do this:

    # Heading

    <script>
    const foo = 1 + 1;
    window.alert(`1 + 1 = ${foo}`);
    </script>

    Paragraphs of exposition.
In practice most markdown renderers will strip HTML or at least script tags as it is obviously a cross site security risk to allow github.com to serve user uploaded scripts without sanitization.
Re:Include diagrams in your Markdown files with Mermaid by reificator on Monday February 14, 2022 @02:07PM (#30336678)
I think you're caught up on exactly the opposite of the complaint here. People aren't requesting JS execution, they have the various "notebook" solutions for that.

People are saying that triple backtick fenced code blocks are used to preserve whitespace and fixed width, usually with syntax highlighting as a bonus. That's the expected behavior.

However with the described mermaid support, it's taking a fenced code block with a mermaid tag and transforming it into a graph.

When inspecting the text content of a markdown file, there's no difference between a `javascript` tagged code block, a `haskell` tagged one, and a `mermaid` tagged one. But now when rendering, the mermaid one is special cased.

This is undesirable for multiple reasons, the most obvious being that it's now harder to write a README that displays mermaid syntax.

A good starting point would be either different syntax or a different tagging convention. For instance I think I saw `!mermaid` suggested, where the `!` would mean that the code should be interpreted instead of rendered as code.

I don't have a horse in the race of what this looks like, only that there should be a distinction between rendered and interpreted code blocks.

Re:Include diagrams in your Markdown files with Mermaid by qbasic_forever on Monday February 14, 2022 @02:31PM (#30337034)
You're pushing a rock up hill--many markdown renderers natively support mermaid, plantUML, etc. using fenced code blocks.

If you explicitly want source code rendering don't add the mermaid tag to the block. There could be an argument that now you don't get syntax highlighting, but remember syntax highlighting itself is not part of any markdown spec and it differs wildly between implementations--there is no common grammar or spec for it even.

In all cases these are still valid markdown files and will pass through markdown processing, rendering, etc. systems without breaking them.

Re:Include diagrams in your Markdown files with Mermaid by reificator on Monday February 14, 2022 @03:35PM (#30337921)
> You're pushing a rock up hill--many markdown renderers natively support mermaid, plantUML, etc. using fenced code blocks.

And yet Github's markdown support carries much more weight. Raising concerns at this point is a perfectly reasonable thing to do.

Re:Include diagrams in your Markdown files with Mermaid by dan-robertson on Monday February 14, 2022 @07:21PM (#30340672)
FWIW, you also sometimes see

  ```math
  -b \pm \sqrt{b^2 - 4ac} \over {2a}
  ```
So to some extent the rendering is advanced syntax highlighting. I wonder where you would draw the monospace text vs rendered line on this spectrum?

  JavaScript ; poem ; ’90s email ; OP ; latex math ; ditaa 
I guess you’d put it at the end with nothing being rendered?