From f6f0fa48159cacd6b24e58e00d07896bd9a6d34c Mon Sep 17 00:00:00 2001 From: ishikawa999 <14245262+ishikawa999@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:28:26 +0900 Subject: [PATCH] Render Mermaid diagrams in collapsed blocks once they are shown (#44425). Mermaid.js lays out a diagram by measuring its text, which has no size inside a collapsed {{collapse}} macro, so the diagram was rendered squashed. Wait with a ResizeObserver until the block is displayed. --- .../controllers/mermaid_controller.js | 28 ++++++++++++++++++- test/system/mermaid_rendering_test.rb | 28 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/app/javascript/controllers/mermaid_controller.js b/app/javascript/controllers/mermaid_controller.js index 8d51d246ad..794d2c3d6b 100644 --- a/app/javascript/controllers/mermaid_controller.js +++ b/app/javascript/controllers/mermaid_controller.js @@ -71,7 +71,7 @@ export default class extends Controller { loadMermaid().then((mermaid) => { // The block may have left the DOM while Mermaid.js was loading. - if (this.element.isConnected) this.render(mermaid); + if (this.element.isConnected) this.renderWhenShown(mermaid); }).catch((error) => { console.error('Failed to load Mermaid.js:', error); // Show the source that the mermaid-available CSS rule hides. @@ -80,6 +80,8 @@ export default class extends Controller { } disconnect() { + this.visibilityObserver?.disconnect(); + this.visibilityObserver = null; // Undo everything render() added so that a reconnect starts clean. this.container?.remove(); this.errorElement?.remove(); @@ -92,6 +94,30 @@ export default class extends Controller { } } + // Mermaid.js lays out a diagram by measuring its text, which has no size + // while the block is not displayed, e.g. inside a collapsed {{collapse}} + // macro. Such a block is rendered once it is shown. + renderWhenShown(mermaid) { + const pre = this.element.closest('pre'); + if (!pre) return; + + const isShown = () => pre.getClientRects().length > 0; + if (isShown()) { + this.render(mermaid); + return; + } + + // The size of the block changes from zero when it is shown. + const observer = new ResizeObserver(() => { + if (!isShown()) return; + observer.disconnect(); + if (this.visibilityObserver === observer) this.visibilityObserver = null; + this.render(mermaid); + }); + observer.observe(pre); + this.visibilityObserver = observer; + } + render(mermaid) { const pre = this.element.closest('pre'); if (!pre) return; diff --git a/test/system/mermaid_rendering_test.rb b/test/system/mermaid_rendering_test.rb index 8bb7733a9b..8b7a4f90a6 100644 --- a/test/system/mermaid_rendering_test.rb +++ b/test/system/mermaid_rendering_test.rb @@ -93,6 +93,34 @@ class MermaidRenderingTest < ApplicationSystemTestCase end end + def test_should_render_mermaid_code_block_in_collapse_macro + skip unless mermaid_installed? + + issue = Issue.find(1) + issue.update_column(:description, "{{collapse\n```mermaid\ngraph TD;\nA-->B;\n```\n}}") + + log_user('jsmith', 'jsmith') + visit "/issues/#{issue.id}" + + within('div.description') do + # Wait until Mermaid.js is loaded while the block is still collapsed. + assert page.evaluate_async_script(<<~JS) + const done = arguments[0]; + const check = () => typeof mermaid !== 'undefined' ? setTimeout(() => done(true), 500) : setTimeout(check, 50); + check(); + JS + find('a.collapsible', text: 'Show').click + within('div.collapsed-text') do + assert_selector 'div.mermaid svg g.node', count: 2 + assert_no_selector 'svg .error-icon' + # The diagram must be laid out while visible, so that its labels have a size. + assert page.evaluate_script( + "document.querySelector('div.collapsed-text div.mermaid svg g.node .label').getBoundingClientRect().width > 0" + ) + end + end + end + def test_should_show_flash_message_for_invalid_mermaid_diagram skip unless mermaid_installed? -- 2.56.1