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

Mermaid does not render on page load from SPA route changes #4

Open
manastalukdar opened this issue Sep 4, 2019 · 2 comments
Open
Assignees
Labels
bug Something isn't working
Milestone

Comments

@manastalukdar
Copy link
Owner

manastalukdar commented Sep 4, 2019

Overview

Mermaid has a direct dependency on window.load event to start rendering. This is triggered when a page initially loads, or when the page is refreshed. In applications based on many javascript frameworks, entering a page using a router usually does not invoke the window.load event. As a result mermaid does not render the text definitions.

A workaround is to initialize mermaid using the (deprecated) mermaid.init() method and point it to a specific container. There are few disadvantages of this approach:

  1. mermaid.init() is deprecated and kept around only for backwards compatibility. Their documentation clearly recommends not to use it.
  2. If the goal is to render all containers in a page decorated with the class name mermaid, this approach does not work. It works only when the page contains a single container where the text definitions needs to be rendered.

There is a related issue in the mermaid repo.

This issue is to explore a solution to this behavior.

Potential Solutions

  1. https://github.com/yuzutech/kroki
  2. https://mermaid.ink
@manastalukdar manastalukdar added the bug Something isn't working label Sep 4, 2019
@manastalukdar manastalukdar self-assigned this Sep 4, 2019
@manastalukdar manastalukdar changed the title Mermaid does not initialize on page load from SPA route changes Mermaid does not render on page load from SPA route changes Sep 4, 2019
@manastalukdar manastalukdar added this to the v0.2.0 milestone Dec 17, 2019
@xifenghuyang
Copy link

it should be note in the document of mermaidAPI. for i saw not a api called init and the render doesn,t work.
when can fix the render function?

@Sun-ZhenXing
Copy link

I solved this problem by using the code below the type: activate re-rendering using the run().

<script setup lang="ts">
import mermaid from 'mermaid'

const value = defineModel<string>('value', { required: true })
const throttled = refThrottled(value, 500)
mermaid.initialize({ startOnLoad: false })

const html = computed(() => {
  return renderMarkdown(throttled.value)
})

watch(throttled, () => {
  setTimeout(() => {
    mermaid.run()
  })
}, { immediate: true })
</script>

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

No branches or pull requests

3 participants