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 root renderer #556

Merged
merged 3 commits into from
Mar 6, 2025
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/large-needles-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-react': patch
---

Add `root` renderer
5 changes: 5 additions & 0 deletions .changeset/rotten-mangos-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-react': patch
---

Ensure that a node with `{length: 0}` as a property can be rendered.
6 changes: 3 additions & 3 deletions packages/myst-demo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {
fileError,
RuleId,
type GenericNode,

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

View workflow job for this annotation

GitHub Actions / lint

'GenericNode' is defined but never used
type GenericParent,
type References,
} from 'myst-common';
Expand Down Expand Up @@ -392,7 +392,7 @@
);

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

Check warning on line 395 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 395 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 395 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
Expand All @@ -408,7 +408,7 @@
)}
>
{column && (
<div className="flex flex-row col-span-2 items-stretch px-2 h-full border dark:border-slate-600">
<div className="flex flex-row items-stretch h-full col-span-2 px-2 border dark:border-slate-600">
<div className="flex-grow"></div>
{demoMenu}
</div>
Expand Down Expand Up @@ -452,7 +452,7 @@
>
<GridSystemProvider gridSystem="demo-grid">
{TitleBlock && <TitleBlock frontmatter={frontmatter}></TitleBlock>}
<MyST ast={references.article?.children as GenericNode[]} />
<MyST ast={references.article} />
</GridSystemProvider>
</ArticleProvider>
</>
Expand All @@ -474,7 +474,7 @@
{previewType === 'DOCX' && (
<div>
<button
className="p-3 rounded border"
className="p-3 border rounded"
onClick={() => saveDocxFile('demo.docx', references.article)}
title={`Download Micorsoft Word`}
aria-label={`Download Micorsoft Word`}
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-to-react/src/MyST.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export function MyST({
className?: string;
}) {
const renderers = useNodeRenderers();
if (!ast || ast.length === 0) return null;
if (!ast) return null;
if (!Array.isArray(ast)) {
const Component = selectRenderer(renderers, ast);
return <Component key={ast.key} node={ast} className={className} />;
}
if (ast.length === 0) return null;
Comment on lines -31 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor updates to this logic, technically a node with {length: 0} as a property would never render. This ensures first that we are dealing with an array.

return (
<>
{ast?.map((node) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/myst-to-react/src/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Glossary = {
type: 'glossary';
};

type Root = {
type: 'root';
};

type BasicNodeRenderers = {
text: NodeRenderer<spec.Text>;
span: NodeRenderer<GenericNode>;
Expand Down Expand Up @@ -95,6 +99,7 @@ type BasicNodeRenderers = {
definitionDescription: NodeRenderer<DefinitionDescription>;
include: NodeRenderer<Include>;
glossary: NodeRenderer<Glossary>;
root: NodeRenderer<Root>;
};

const BASIC_RENDERERS: BasicNodeRenderers = {
Expand Down Expand Up @@ -412,6 +417,9 @@ const BASIC_RENDERERS: BasicNodeRenderers = {
</div>
);
},
root({ node, className }) {
return <MyST ast={node.children} className={className} />;
},
};

export default BASIC_RENDERERS;
2 changes: 1 addition & 1 deletion themes/article/app/components/Article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function Article({
<ErrorTray pageSlug={article.slug} />
<div id="skip-to-article" />
<FrontmatterParts parts={parts} keywords={keywords} hideKeywords={hideKeywords} />
<MyST ast={tree.children as GenericParent[]} />
<MyST ast={tree} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that this now renderers without a wrapping div.

<BackmatterParts parts={parts} />
<Footnotes />
<Bibliography />
Expand Down
2 changes: 1 addition & 1 deletion themes/book/app/components/ArticlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const ArticlePage = React.memo(function ({
)}
<div id="skip-to-article" />
<FrontmatterParts parts={parts} keywords={keywords} hideKeywords={hideKeywords} />
<MyST ast={tree.children as GenericParent[]} />
<MyST ast={tree} />
<BackmatterParts parts={parts} />
<Footnotes />
<Bibliography />
Expand Down
Loading