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

🧜‍♀️ Allow hover-references of mermaid diagrams #380

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-emus-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/diagrams': patch
---

Allow for hover references of mermaid diagrams
11 changes: 7 additions & 4 deletions packages/diagrams/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NodeRenderer } from '@myst-theme/providers';
import { useEffect, useState } from 'react';
import { useEffect, useId, useState } from 'react';

async function parse(id: string, text: string): Promise<string> {
const { default: mermaid } = await import('mermaid');
Expand All @@ -11,10 +11,11 @@ async function parse(id: string, text: string): Promise<string> {
}

export function MermaidRenderer({ id, value }: { value: string; id: string }) {
const key = useId();
const [graph, setGraph] = useState<string>();
const [error, setError] = useState<Error>();
useEffect(() => {
parse(id, value)
parse(`mermaid-${key.replace(/:/g, '')}`, value)
Copy link
Member Author

Choose a reason for hiding this comment

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

The ID was shared before and stole the diagram.

.then((svg) => {
setGraph(svg);
setError(undefined);
Expand All @@ -25,12 +26,14 @@ export function MermaidRenderer({ id, value }: { value: string; id: string }) {
});
}, []);
return (
<figure className="">
<figure id={id}>
{graph && <div dangerouslySetInnerHTML={{ __html: graph }}></div>}
{error && (
<pre>
Error parsing mermaid graph.
{'\n\n'}
{error.message}
{'\n\n'}
{value}
</pre>
)}
Expand All @@ -39,5 +42,5 @@ export function MermaidRenderer({ id, value }: { value: string; id: string }) {
}

export const MermaidNodeRenderer: NodeRenderer = ({ node }) => {
return <MermaidRenderer id={node.key} value={node.value} />;
return <MermaidRenderer id={node.html_id || node.identifier} value={node.value} />;
};
Loading