Skip to content

@streamdown/mermaid: a hook to transform the source before render #719

Open

Description

@aryakapoor

LLM-written Mermaid sometimes has a small syntax error we could fix without losing content, like an unquoted ( in a flowchart label or a quoted state name used as a transition endpoint. We want to fix the source only when Mermaid fails to parse it, then render again.

There's no seam for that today. createMermaidPlugin only takes { config } and hands the fence body straight to render, so the only way in is to wrap the plugin's render. That depends on the shape of MermaidInstance and changes every caller of render, export included. errorComponent only fires on failure, so a host also can't tell when a render succeeded.

Proposal:

interface MermaidPluginOptions {
  config?: MermaidConfig;
  /** Runs on the fence body before every render, including export. Throwing falls back to the original source. */
  transform?: (source: string, ctx: { isIncomplete: boolean }) => string | Promise<string>;
}

interface MermaidOptions {
  config?: MermaidConfig;
  errorComponent?: React.ComponentType<MermaidErrorComponentProps>;
  /** Fires once per closed fence when its final render settles. */
  onRenderSettled?: (result: { chart: string; renderedSource: string; ok: boolean; error?: Error }) => void;
}

errorComponent would keep getting the original chart, so the error UI sees what the model wrote. With #668, onRenderSettled only fires after the fence closes, so it never reports a partial chart.

Related: #490 (host callbacks on MermaidOptions). I can put up a PR if this shape works.

Activity

  1. sleitor commented on Nov 13, 2026

    @sleitor
    Contributor

    Looked into this as a possible external contribution and I'm going to leave it alone. Findings:

    Suggestion: a maintainer should first agree on the API shape, ideally with transform and onRenderSettled split into separate changes. After that, @aryakapoor's PR offer seems like the best route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions