Skip to content

Commit

Permalink
💅 🖼️ Add topic directive (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Apr 19, 2024
1 parent 65a2bab commit 9810bf7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-hornets-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-react': patch
---

Support various types of margin content
55 changes: 55 additions & 0 deletions packages/myst-to-react/src/aside.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { NodeRenderer } from '@myst-theme/providers';
import { MyST } from './MyST.js';
import type { GenericNode } from 'myst-common';

type Aside = {
type: 'aside';
};

function getAsideClass(kind?: string) {
switch (kind) {
case 'topic':
return {
container: 'my-5 shadow dark:bg-stone-800 overflow-hidden dark:border-l-4 border-slate-400',
title:
'm-0 font-medium py-2 px-4 flex min-w-0 text-md border-y dark:border-y-0 bg-gray-50/80 dark:bg-slate-900',
body: 'px-4',
};
case 'margin':
case 'sidebar':
default:
return {
container: 'text-sm lg:h-0 col-margin-right',
title: 'text-base font-semibold',
body: '',
};
}
}

export const AsideRenderer: NodeRenderer<Aside> = ({ node }) => {
const [title, ...rest] = node.children as GenericNode[];
const className = getAsideClass(node.kind);
if (title.type !== 'admonitionTitle') {
return (
<aside className={className.container}>
<MyST ast={node.children} />
</aside>
);
}
return (
<aside className={className.container}>
<div className={className.title}>
<MyST ast={title} />
</div>
<div className={className.body}>
<MyST ast={rest} />
</div>
</aside>
);
};

const ASIDE_RENDERERS = {
aside: AsideRenderer,
};

export default ASIDE_RENDERERS;
12 changes: 0 additions & 12 deletions packages/myst-to-react/src/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ type CaptionNumber = {
identifier: string;
};

type Aside = {
type: 'aside';
};

type Keyboard = {
type: 'keyboard';
};
Expand Down Expand Up @@ -93,7 +89,6 @@ type BasicNodeRenderers = {
definitionList: NodeRenderer<DefinitionList>;
definitionTerm: NodeRenderer<DefinitionTerm>;
definitionDescription: NodeRenderer<DefinitionDescription>;
aside: NodeRenderer<Aside>;
include: NodeRenderer<Include>;
};

Expand Down Expand Up @@ -378,13 +373,6 @@ const BASIC_RENDERERS: BasicNodeRenderers = {
</dd>
);
},
aside({ node }) {
return (
<aside className="text-sm lg:h-0 col-margin-right">
<MyST ast={node.children} />
</aside>
);
},
keyboard({ node }) {
return (
<kbd>
Expand Down
2 changes: 2 additions & 0 deletions packages/myst-to-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import EXT_RENDERERS from './extensions/index.js';
import INLINE_EXPRESSION_RENDERERS from './inlineExpression.js';
import PROOF_RENDERERS from './proof.js';
import EXERCISE_RENDERERS from './exercise.js';
import ASIDE_RENDERERS from './aside.js';
import UNKNOWN_MYST_RENDERERS from './unknown.js';

export { CopyIcon, HoverPopover, Tooltip, LinkCard } from './components/index.js';
Expand Down Expand Up @@ -51,6 +52,7 @@ export const DEFAULT_RENDERERS: Record<string, NodeRenderer> = {
...EXT_RENDERERS,
...PROOF_RENDERERS,
...EXERCISE_RENDERERS,
...ASIDE_RENDERERS,
};

export { MyST } from './MyST.js';

0 comments on commit 9810bf7

Please sign in to comment.