Skip to content

Commit

Permalink
🐛 Fix hooks in citation (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed May 30, 2023
1 parent bab1c8c commit 194936b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-onions-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-react': patch
---

Update citation to avoid bad hook pattern
31 changes: 25 additions & 6 deletions packages/myst-to-react/src/cite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,26 @@ export const CiteGroup: NodeRenderer = (node, children) => {
);
};

export const Cite: NodeRenderer = (node, children) => {
export const Cite = ({
label,
error,
children,
}: {
label?: string;
error?: boolean;
children: React.ReactNode;
}) => {
const references = useReferences();
const { html, doi: doiString } = references?.cite?.data[node.label] ?? {};
if (node.error) {
return <InlineError key={node.key} value={node.label} message={'Citation Not Found'} />;
if (!label) {
return <InlineError value="cite (no label)" message={'Citation Has No Label'} />;
}
const { html, doi: doiString } = references?.cite?.data[label] ?? {};
if (error) {
return <InlineError value={label} message={'Citation Not Found'} />;
}
const doiUrl = doiString ? doi.buildUrl(doiString as string) : null;
return (
<HoverPopover key={node.key} openDelay={300} card={<CiteChild html={html} />}>
<HoverPopover openDelay={300} card={<CiteChild html={html} />}>
<cite className="hover-link">
{doiUrl && (
<a href={doiUrl} target="_blank" rel="noreferrer" className="hover-link">
Expand All @@ -49,9 +60,17 @@ export const Cite: NodeRenderer = (node, children) => {
);
};

export const CiteRenderer: NodeRenderer = (node, children) => {
return (
<Cite key={node.key} label={node.label} error={node.error}>
{children}
</Cite>
);
};

const CITE_RENDERERS: Record<string, NodeRenderer> = {
citeGroup: CiteGroup,
cite: Cite,
cite: CiteRenderer,
};

export default CITE_RENDERERS;

0 comments on commit 194936b

Please sign in to comment.