fralau / mkdocs-mermaid2-plugin

A Mermaid graphs plugin for mkdocs
https://mkdocs-mermaid2.readthedocs.io
MIT License
203 stars 25 forks source link

Annotations in class diagram not working with SuperFences #60

Closed guansss closed 1 year ago

guansss commented 1 year ago

Source:

classDiagram
  class Foo
  <<abstract>> Foo

Expected:

classDiagram
  class Foo
  <<abstract>> Foo

Actual:

image

Here's the mkdocs.yml, if I disable SuperFences it works as expected.

site_name: Test

docs_dir: docs

plugins:
  - mermaid2

markdown_extensions:
  - pymdownx.superfences:
      custom_fences:
        - name: mermaid
          class: mermaid
          format: !!python/name:mermaid2.fence_mermaid

mkdocs==1.3.1 mkdocs-mermaid2-plugin==0.6.0 pymdown-extensions==9.5

github-actions[bot] commented 1 year ago

Thank you for your contribution! This is very appreciated.

guansss commented 1 year ago

It turns out that the annotations are half escaped, resulting in unexpected HTML tag that breaks Mermaid parser:

1661911981205

I looked into the source of fence_mermaid formatter and the comment says it does not escape the Mermaid source, so I switched to the official formatter superfences.fence_div_format (which does escape) and then everything worked without problem.

  - pymdownx.superfences:
      custom_fences:
        - name: mermaid
          class: mermaid
          format: !!python/name:pymdownx.superfences.fence_div_format

Looks like escaping the source is necessary? Then I'm wondering, what was the original purpose of not to escape? Does escaping have pitfalls?

fralau commented 1 year ago

That's a thought-provoking question 🤔. I will look into it.

fralau commented 1 year ago

The custom mermaid2.fence_mermaid was developed specifically for the loose security mode, which allows tags in text and clicking (it's stated in the code of the fence.py.

When using tags in text, you don't want to escape your HTML .

But if you are operating in the default strict mode (no tags in text and no clickable links), escaping might indeed be better.

🤔 Perhaps I should document this better?

guansss commented 1 year ago

Oh I missed that part. Thanks for the answer!

Perhaps I should document this better?

That'll be good. 👍 Maybe also suggest user to manually escape these symbols (e.g. &lt;&lt;abstract&gt;&gt;) as a workaround in loose mode.