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

🐛 Fix hooks in citation #109

Merged
merged 1 commit into from
May 30, 2023
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/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;
Loading