Skip to content

Dedent ui.mermaid content like ui.markdown does - #6012

Merged
falkoschindler merged 2 commits into
mainfrom
fix/mermaid-dedent
Apr 30, 2026
Merged

Dedent ui.mermaid content like ui.markdown does#6012
falkoschindler merged 2 commits into
mainfrom
fix/mermaid-dedent

Conversation

@falkoschindler

Copy link
Copy Markdown
Contributor

Motivation

ui.markdown already auto-dedents its content via remove_indentation, but ui.mermaid only stripped outer whitespace.
Identical triple-quoted strings therefore behaved differently between the two elements.

The discrepancy becomes a hard error with Mermaid's YAML front-matter (e.g. displayMode: compact for compact-mode Gantt charts), which requires --- markers at column 0.
Any leading indentation makes Mermaid bail out with "Diagrams beginning with --- are not valid. If you were trying to use a YAML front-matter, please use un-indented --- blocks".

Refs discussion #6011.

Implementation

  • Promote remove_indentation from nicegui/elements/markdown.py to nicegui/helpers/strings.py and re-export it via nicegui.helpers.
    It is no longer markdown-specific now that ui.mermaid uses it as well, and lifting it removes the cross-element dependency from mermaidmarkdown.
  • Update all existing call sites (code, markdown, restructured_text, tests/test_vbuild.py, three website files) to import from the new location.
  • Use remove_indentation inside Mermaid._handle_content_change, replacing the previous content.strip() calls.
    Since remove_indentation already drops leading whitespace-only lines and produces no leading whitespace on the first line, no extra .strip() is needed; Mermaid is fine with any trailing whitespace.
  • Add a regression test test_mermaid_with_yaml_frontmatter that places a YAML front-matter block inside an indented triple-quoted string and verifies the chart renders without a syntax error.

This is not a breaking change in practice: Mermaid is whitespace-insensitive at the start of lines for every diagram type, so removing a uniform leading indent is a no-op for existing diagrams.
The four demos in mermaid_documentation.py continue to render identically.

Progress

  • The PR title is a short phrase starting with a verb like "Add ...", "Fix ...", "Update ...", "Remove ...", etc.
  • The implementation is complete.
  • This PR does not address a security issue.
  • Pytest has been added.
  • Documentation is not necessary.
  • No breaking changes to the public API.

falkoschindler and others added 2 commits April 29, 2026 12:23
ui.markdown auto-dedents triple-quoted content via remove_indentation,
but ui.mermaid only stripped outer whitespace. The discrepancy is most
visible with Mermaid's YAML front-matter syntax (e.g. displayMode: compact),
which requires --- markers at column 0 and breaks under any leading indent.

Refs discussion #6011.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The helper is no longer markdown-specific now that ui.mermaid uses it
too, so promoting it to nicegui.helpers keeps imports clean and avoids
cross-element dependencies.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@falkoschindler falkoschindler added feature Type/scope: New or intentionally changed behavior review Status: PR is open and needs review labels Apr 29, 2026
@falkoschindler falkoschindler added this to the 3.12 milestone Apr 29, 2026
@falkoschindler
falkoschindler requested a review from evnchn April 29, 2026 10:33

@evnchn evnchn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor looks sound (trusting your AI agent). On my end I watch for the empirical results:

Image

Please merge only after "Claude, is it safe to dedent all Mermaid diagrams automatically" yields a Yes.

@falkoschindler

Copy link
Copy Markdown
Contributor Author

@evnchn Yes, Claude couldn't come up with a counter example where existing Mermaid code would break after this change. 👍🏻

@falkoschindler
falkoschindler added this pull request to the merge queue Apr 30, 2026
Merged via the queue into main with commit a7587d6 Apr 30, 2026
12 of 14 checks passed
@falkoschindler
falkoschindler deleted the fix/mermaid-dedent branch April 30, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Type/scope: New or intentionally changed behavior review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants