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

Rendering Mermaid Diagram with errors on Webpage #5385

Closed
mnidhi172 opened this issue Mar 14, 2024 Discussed in #5384 · 2 comments
Closed

Rendering Mermaid Diagram with errors on Webpage #5385

mnidhi172 opened this issue Mar 14, 2024 Discussed in #5384 · 2 comments
Labels
Status: Triage Needs to be verified, categorized, etc

Comments

@mnidhi172
Copy link

Discussed in https://github.com/orgs/mermaid-js/discussions/5384

Originally posted by mnidhi172 March 14, 2024
I am trying to render the mermaid diagrams along with the errors in the mermaid code if any on the webpage using react js. In my app the diagram is reflecting properly however the errors in the code are not going from the page even if the mermaid code is correct.

Here is how I am doing it:

import React from 'react';
import mermaid from 'mermaid';
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch';

class Mermaid extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      mermaidError: null,
    };
  }

  componentDidMount() {
    mermaid.initialize({
      startOnLoad: true
    });
    mermaid.contentLoaded();

    mermaid.parseError = (error, hash) => {
      console.error('Mermaid errors:', error);
      this.setState({ mermaidError: error.str });
    };
  }

  componentDidUpdate(prevProps) {
    if (prevProps.chart !== this.props.chart) {
      const mermaidChart = document.getElementById('mermaid-chart');
      if (mermaidChart) {
        mermaidChart.removeAttribute('data-processed');
        mermaid.contentLoaded();
      }
    }
  }

  render() {
    return (
      <TransformWrapper>
        <TransformComponent>
          <div
            id="mermaid-chart"
            className="mermaid"
            style={{
              width: '800px',
              height: '600px',
              boxSizing: 'border-box',
              overflow: 'auto',
            }}
          >
            {this.props.chart}
          </div>
          {this.state.mermaidError && (
            <div style={{ color: 'red' }}>
              Mermaid Error: {this.state.mermaidError}
            </div>
          )}
        </TransformComponent>
      </TransformWrapper>
    );
  }
}

export default Mermaid;
<div className="col-md-6 diagram">
  <div >
    <Mermaid chart={diagramCode} onParseError={handleMermaidError} mermaidError={mermaidError} />
  </div>

Anyone knows how can we properly show the mermaid diagram as well as error in the mermaid code on the webpage using react?

@github-actions github-actions bot added the Status: Triage Needs to be verified, categorized, etc label Mar 14, 2024
@sidharthv96
Copy link
Member

@mnidhi172
Copy link
Author

I have sorted the issue, thanks for the reference. This is how I am doing it:

import React from 'react';
import mermaid from 'mermaid';

class Mermaid extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      mermaidError: null,
    };
  }

  componentDidMount() {
    mermaid.initialize({
      startOnLoad: true
    });
    mermaid.contentLoaded();

    mermaid.parseError = (error, hash) => {
      console.error('Mermaid errors:', error);
      this.setState({ mermaidError: error.str });
    };
  }

  componentDidUpdate(prevProps) {
    mermaid.contentLoaded();
    if (prevProps.chart !== this.props.chart) {
      const mermaidgraph = document.getElementById('mermaid-chart');
      if (mermaidgraph) {
        mermaidgraph.removeAttribute('data-processed');
      }
      this.handleDiagramCodeChange(this.props.chart);
    }
  }
  
  

  handleDiagramCodeChange = async (newDiagramCode) => {
    try {
      const isValidSyntax = await mermaid.parse(newDiagramCode);
      if (isValidSyntax) {
        this.setState({ mermaidError: null });
      } else {
        this.setState({ mermaidError: mermaid.parseError });
      }
    } catch (error) {
      console.error('Error in handleDiagramCodeChange:', error);
      this.setState({mermaidError: error.message});
    }
  };

  render() {
  if ( !this.props.chart){
    return null;
  }      
  {
    return (
      <div>
        <div
          id="mermaid-chart"
          className="mermaid"
        >
          {this.state.mermaidError ? (
            <div style={{ color: 'red' }}>Error: {this.state.mermaidError}</div>
          ) : (
            this.props.chart
          )}
        </div>
      </div>
    );
  }
}}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage Needs to be verified, categorized, etc
Projects
None yet
Development

No branches or pull requests

2 participants