Skip to content

Commit

Permalink
🪄 Add more class support to directives (#500)
Browse files Browse the repository at this point in the history
* feat: add more class support

* chore: add changeset

* chore: run prettier
  • Loading branch information
agoose77 authored Nov 19, 2024
1 parent 815265f commit fdccb23
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
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 @@ async function parse(
}

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

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

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

0 comments on commit fdccb23

Please sign in to comment.