From: Alexander Wilms Date: Sat, 14 Jun 2025 16:28:54 +0000 (+0200) Subject: feat: Add interactive Mermaid diagram viewer X-Git-Url: http://source.bookstackapp.com/hacks/commitdiff_plain/2cdfe9f6943527674dda83730d8ad5bf011f2c5f feat: Add interactive Mermaid diagram viewer --- diff --git a/content/mermaid-viewer/head.html b/content/mermaid-viewer/head.html new file mode 100644 index 0000000..2470231 --- /dev/null +++ b/content/mermaid-viewer/head.html @@ -0,0 +1,555 @@ + + + + \ No newline at end of file diff --git a/content/mermaid-viewer/index.md b/content/mermaid-viewer/index.md new file mode 100644 index 0000000..754fdb4 --- /dev/null +++ b/content/mermaid-viewer/index.md @@ -0,0 +1,40 @@ ++++ +title = "Mermaid viewer" +author = "@Alexander-Wilms" +date = 2024-07-16T00:00:00Z +updated = 2024-07-16T00:00:00Z +tested = "v25.05" ++++ + +This hack enables interactive Mermaid diagrams to be rendered within a page on BookStack. The Mermaid code itself can be written and edited using either BookStack's WYSIWYG editor by creating a code block and assigning it the language "mermaid" or the Markdown editor (using standard ` ```mermaid ... ``` ` code fences). +It automatically detects `pre code` blocks with the class `language-mermaid` and replaces them with a feature-rich viewer: + +````markdown +```mermaid +flowchart TD + A[Christmas] -->|Get money| B(Go shopping) + B --> C{Let me think} + C -->|One| D[Laptop] + C -->|Two| E[iPhone] + C -->|Three| F[fa:fa-car Car] +``` +```` + +The viewer provides the following functionalities: +- **Pan and Zoom:** Pan the diagram by clicking and dragging, zoom using the mouse wheel or use dedicated buttons for both. +- **Interaction Toggle:** Toggle manual interaction (pan/zoom) on or off using a lock/unlock button so it doesn't interfere with scrolling the page. +- **Reset Zoom:** Reset the diagram to its default zoom level and centered position. +- **Copy Code:** Copy the underlying Mermaid code to the clipboard. +- **Font Awesome Icons:** Use Font Awesome icons directly within the Mermaid diagram syntax. +- **Text selection:** When interaction is disabled, text can be selected and copied. + +#### Considerations + +- This relies on JavaScript to parse and render content on page load. +- This loads the Mermaid.js library from `cdn.jsdelivr.net` and Font Awesome icons from `cdnjs.cloudflare.com`. +- There is no live preview of the *interactive viewer features* while editing the page content. +- The viewer injects its own CSS for styling. While designed to be self-contained, there's a minor possibility of style interactions with highly customized BookStack themes. + +#### Code + +{{}}