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

feat(cli): support mermaid diagrams within GFM (github flavored markdown) #16

Merged
merged 3 commits into from
Apr 16, 2024

Conversation

aloisklink
Copy link
Contributor

Draft PR: This PR is stacked on top of:

Please change this PR to target main once #15 has been merged.

📖 Description

Support mermaid diagrams that are embedded within GFM (GitHub flavoured markdown) .md or .markdown markdown files.

Example

For example, given a markdown file that looks like:

# Here is my Mermaid diagram in a quote in a list

* This is my list
  * This is my list
    1. This is my entry with the diagram:
       > Here is my quote with the diagram:
       >
       > ```mermaid
       > ---
       > title: This is my diagram title
       > ---
       > flowchart
       >     A[Hello World]
       > ```

After running mermaid-chart link ./path/to/my/markdown/file.md, we'd expect the output file to look like:

# Here is my Mermaid diagram in a quote in a list

* This is my list
  * This is my list
    1. This is my entry with the diagram:
       > Here is my quote with the diagram:
       >
       > ```mermaid
       > ---
       > title: This is my diagram title
       > id: 'diagram-id-on-mermaidchart.com'
       > ---
       > flowchart
       >     A[Hello World]
       > ```

Implementation details

We're using remark to parse the markdown into a markdown AST. remark by default only supports CommonMark, however, with remark-gfm and remark-frontmatter we can support GFM (GitHub flavoured markdown, which is used by GitHub.

From this, we process every code block that has lang=mermaid, then turn the mdAST back to a string.

This means that we also automatically format/prettify the user's markdown input, which is potentially unideal, as it might break somebody's markdown file if they're using an unusual dialect of markdown, but I can't think of a better way to do this.

@aloisklink aloisklink marked this pull request as draft December 1, 2023 14:51
Copy link

vercel bot commented Dec 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
office-plugin-site ⬜️ Ignored (Inspect) Visit Preview Mar 6, 2024 4:15pm

Support mermaid diagrams that are embedded within CommonMark
`.md` markdown files.

We're using [remark](https://github.com/remarkjs/remark) to parse the
markdown into a [markdown AST](https://github.com/syntax-tree/mdast).
From this, we process every code block that has `lang=mermaid`, then
turn the mdAST back to a string.

This means that we also automatically format/prettify the user's
markdown input, which is potentially unideal.
Currently, running
`mermaid-chart link test/fixtures/partially-linked-markdown-file.md`
fails if the markdown file has some diagrams that are already linked.

Now, already linked diagrams are ignored if they're located within a
markdown file.
[remark][1] by default only supports [CommonMark][2], however, with
[remark-gfm][3] and [remark-frontmatter][4] we can support
[GFM (GitHub flavoured markdown][5], which is used by GitHub.

[1]: https://github.com/remarkjs/remark
[2]: https://commonmark.org/
[3]: https://github.com/remarkjs/remark-gfm
[4]: https://github.com/remarkjs/remark-frontmatter
[5]: https://github.github.com/gfm/
Base automatically changed from feat/support-multiple-files-at-once-in-cli to main April 16, 2024 09:00
@aloisklink aloisklink marked this pull request as ready for review April 16, 2024 09:00
@aloisklink aloisklink merged commit 3f3daf3 into main Apr 16, 2024
6 checks passed
@aloisklink aloisklink deleted the feat/support-mermaid-in-markdown-files branch April 16, 2024 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants