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

Center mermaid diagram in a html document with fig-align does not work #4376

Closed
cderv opened this issue Feb 16, 2023 Discussed in #4375 · 2 comments · Fixed by #7878
Closed

Center mermaid diagram in a html document with fig-align does not work #4376

cderv opened this issue Feb 16, 2023 Discussed in #4375 · 2 comments · Fixed by #7878
Assignees
Labels
bug Something isn't working crossref diagrams-mermaid Mermaid diagrams
Milestone

Comments

@cderv
Copy link
Collaborator

cderv commented Feb 16, 2023

Following #3294 and its fix #3433

this is supposed to work

---
title: "test"
format: html 
keep-md: true
---

```{mermaid}
%%| fig-cap: Mermaid diagram
%%| fig-align: center
flowchart TB
a --> b 
b --> c
```

However it is not.

This is because we do in JS now

// forward align attributes to the correct parent dif
// so that the svg figure is aligned correctly
const div = svg.parentElement.parentElement.parentElement;
const align = div.parentElement.parentElement.dataset.layoutAlign;
if (align) {
div.classList.remove("quarto-figure-left");
div.classList.remove("quarto-figure-center");
div.classList.remove("quarto-figure-right");
div.classList.add(`quarto-figure-${align}`);
}

but somehow we have more div than expected. The layout-align is one div above.

This is the intermediate md

---
title: "test"
format: html 
keep-md: true
---

:::{.cell layout-align="center"}

:::{.cell-output-display}

:::{}

:::{}

<pre class="mermaid mermaid-js">flowchart TB
a --&gt; b 
b --&gt; c
</pre>
:::


Mermaid diagram
:::
:::
:::

and the HTML rendered in preview

<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div>
<div>
<svg ...>(SVG CONTENT</svg>
</div>
<p>Mermaid diagram</p>
</div>
</div>
</div>

@cscheid I let you see this because

  • Either we need to add one more parentElement
-const div = svg.parentElement.parentElement.parentElement;
+const div = svg.parentElement.parentElement.parentElement.parentElement;
  • We need to see why we create those empty div in the markdown - maybe there is one more that we expect.

Discussed in #4375

Originally posted by wilcar February 16, 2023
How can I center a mermaid diagram in a html document ?

@cderv cderv added bug Something isn't working diagrams-mermaid Mermaid diagrams labels Feb 16, 2023
@cderv cderv added this to the v1.3 milestone Feb 16, 2023
@cscheid
Copy link
Collaborator

cscheid commented Feb 24, 2023

Hm. The problem here is that we sometimes need that div, and sometimes we don't (that's ultimately a crossref issue, where we assume a certain div structure where things like captions etc will be). It's honestly a mess. The workaround will be to search for either of the divs, but it's really an admission of defeat. We should nuke this code from orbit and start again, and I will on 1.4.

Well, it's worse. Our SCSS assumes a figure node, which we only emit for a crossreferenceable figure with id and caption. So the only way currently to make this work in 1.3 is to do something like:

```{mermaid}
%%| label: fig-1
%%| fig-cap: Mermaid diagram
%%| fig-align: center
flowchart TB
a --> b 
b --> c
```

This is, in the end, just another instance of the "fig-align should work for all "figures" whether or not they're crossreferenceable" bug. I'm reluctant to change this in 1.3 because that code is very finicky and regression prone. We'll fix this in 1.4.

@cscheid
Copy link
Collaborator

cscheid commented Dec 11, 2023

Similarly, the example shared will emit a caption that gets no styling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crossref diagrams-mermaid Mermaid diagrams
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants