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

🪄 Add more class support to directives #500

Merged
merged 3 commits into from
Nov 19, 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
7 changes: 7 additions & 0 deletions .changeset/tidy-ducks-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@myst-theme/diagrams': patch
'myst-demo': patch
'myst-to-react': patch
---

Add more support for class and identifier
20 changes: 17 additions & 3 deletions packages/diagrams/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ async function parse(id: string, text: string): Promise<string> {
});
}

export function MermaidRenderer({ id, value }: { value: string; id: string }) {
export function MermaidRenderer({
id,
value,
className,
}: {
value: string;
id: string;
className?: string;
}) {
const key = useId();
const [graph, setGraph] = useState<string>();
const [error, setError] = useState<Error>();
Expand All @@ -26,7 +34,7 @@ export function MermaidRenderer({ id, value }: { value: string; id: string }) {
});
}, []);
return (
<figure id={id}>
<figure id={id} className={className}>
{graph && <div dangerouslySetInnerHTML={{ __html: graph }}></div>}
{error && (
<pre>
Expand All @@ -42,5 +50,11 @@ export function MermaidRenderer({ id, value }: { value: string; id: string }) {
}

export const MermaidNodeRenderer: NodeRenderer = ({ node }) => {
return <MermaidRenderer id={node.html_id || node.identifier} value={node.value} />;
return (
<MermaidRenderer
id={node.html_id || node.identifier}
value={node.value}
className={node.class}
/>
);
};
12 changes: 11 additions & 1 deletion packages/myst-demo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
}

export function MySTRenderer({
id,
value,
column,
fullscreen,
Expand All @@ -239,6 +240,7 @@
captureTab,
className,
}: {
id?: string;
value: string;
column?: boolean;
fullscreen?: boolean;
Expand Down Expand Up @@ -386,10 +388,11 @@
);

const mdastStage = astStage === 'pre' ? mdastPre : mdastPost;
const { downloads, exports, parts, ...reducedFrontmatter } = frontmatter;

Check warning on line 391 in packages/myst-demo/src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

'downloads' is assigned a value but never used

Check warning on line 391 in packages/myst-demo/src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

'exports' is assigned a value but never used

Check warning on line 391 in packages/myst-demo/src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

'parts' is assigned a value but never used

return (
<figure
id={id}
className={classnames(
'relative',
{
Expand Down Expand Up @@ -503,5 +506,12 @@
}

export const MystDemoRenderer: NodeRenderer = ({ node }) => {
return <MySTRenderer value={node.value} numbering={node.numbering} />;
return (
<MySTRenderer
id={node.html_id || node.identifier}
value={node.value}
numbering={node.numbering}
className={node.class}
/>
);
};
17 changes: 15 additions & 2 deletions packages/myst-to-react/src/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type Include = {
type: 'include';
};

type Glossary = {
type: 'glossary';
};

type BasicNodeRenderers = {
text: NodeRenderer<spec.Text>;
span: NodeRenderer<GenericNode>;
Expand Down Expand Up @@ -90,6 +94,7 @@ type BasicNodeRenderers = {
definitionTerm: NodeRenderer<DefinitionTerm>;
definitionDescription: NodeRenderer<DefinitionDescription>;
include: NodeRenderer<Include>;
glossary: NodeRenderer<Glossary>;
};

const BASIC_RENDERERS: BasicNodeRenderers = {
Expand Down Expand Up @@ -344,7 +349,7 @@ const BASIC_RENDERERS: BasicNodeRenderers = {
},
definitionList({ node }) {
return (
<dl className="my-5" id={node.html_id}>
<dl className="my-5" id={node.html_id || node.identifier || node.key}>
<MyST ast={node.children} />
</dl>
);
Expand All @@ -355,7 +360,7 @@ const BASIC_RENDERERS: BasicNodeRenderers = {
node.children?.reduce((allowed, n) => allowed && allowedStrongTypes.has(n.type), true) ??
false;
return (
<dt id={node.html_id}>
<dt id={node.html_id || node.identifier || node.key}>
{makeStrong ? (
<strong>
<MyST ast={node.children} />
Expand Down Expand Up @@ -384,6 +389,14 @@ const BASIC_RENDERERS: BasicNodeRenderers = {
// TODO, provider could give context about the filename
return <MyST ast={node.children} />;
},
glossary({ node }) {
// TODO, provider could give context about the filename
return (
<div id={node.html_id || node.identifier || node.key} className={node.class}>
<MyST ast={node.children} />
</div>
);
},
};

export default BASIC_RENDERERS;
5 changes: 4 additions & 1 deletion packages/myst-to-react/src/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ export function Details({
title,
children,
open,
className,
}: {
title: React.ReactNode;
children: React.ReactNode;
open?: boolean;
className?: string;
}) {
return (
<details
className={classNames(
'rounded-md my-5 shadow dark:shadow-2xl dark:shadow-neutral-900 overflow-hidden',
'bg-gray-50 dark:bg-stone-800',
className,
)}
open={open}
>
Expand Down Expand Up @@ -61,7 +64,7 @@ export function Details({
export const DetailsRenderer: NodeRenderer<DropdownSpec> = ({ node }) => {
const [title, ...rest] = node.children as any[];
return (
<Details title={<MyST ast={[title]} />} open={node.open}>
<Details title={<MyST ast={[title]} />} open={node.open} className={node.class}>
<MyST ast={rest} />
</Details>
);
Expand Down
Loading