GraphContent: fix recursive transclusion

This commit is contained in:
Liam Fitzgerald 2021-05-06 12:23:47 +10:00
parent 61e9deb787
commit 1db67feec9
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB

View File

@ -56,28 +56,26 @@ const codeToMdAst = (content: CodeContent) => {
children: [ children: [
{ {
type: 'code', type: 'code',
value: content.code.expression value: content.code.expression,
}, },
{ {
type: 'code', type: 'code',
value: (content.code.output || []).join('\n') value: (content.code.output || []).join('\n'),
} },
] ],
}; };
};
}
const contentToMdAst = (tall: boolean) => ( const contentToMdAst = (tall: boolean) => (
content: Content content: Content
): [StitchMode, any] => { ): [StitchMode, any] => {
if ('text' in content) { if ('text' in content) {
return ['merge', tall ? parseTall(content.text) : parseWide(content.text)] as [StitchMode, any];
} else if ('code' in content) {
return [ return [
'block', 'merge',
codeToMdAst(content) tall ? parseTall(content.text) : parseWide(content.text),
]; ] as [StitchMode, any];
} else if ('code' in content) {
return ['block', codeToMdAst(content)];
} else if ('reference' in content) { } else if ('reference' in content) {
return [ return [
'block', 'block',
@ -337,10 +335,15 @@ const renderers = {
<RemoteContent key={url} url={url} /> <RemoteContent key={url} url={url} />
</Box> </Box>
), ),
'graph-reference': ({ api, reference }) => { 'graph-reference': ({ api, reference, transcluded }) => {
const { link } = referenceToPermalink({ reference }); const { link } = referenceToPermalink({ reference });
return ( return (
<PermalinkEmbed api={api} link={link} transcluded={0} showOurContact /> <PermalinkEmbed
api={api}
link={link}
transcluded={transcluded}
showOurContact
/>
); );
}, },
root: ({ children }) => <Col gapY="2">{children}</Col>, root: ({ children }) => <Col gapY="2">{children}</Col>,
@ -350,18 +353,24 @@ const renderers = {
export function Graphdown<T extends {} = {}>( export function Graphdown<T extends {} = {}>(
props: { props: {
ast: GraphAstNode; ast: GraphAstNode;
transcluded: number;
tall?: boolean; tall?: boolean;
depth?: number; depth?: number;
} & T } & T
) { ) {
const { ast, depth = 0, ...rest } = props; const { ast, transcluded, depth = 0, ...rest } = props;
const { type, children = [], ...nodeRest } = ast; const { type, children = [], ...nodeRest } = ast;
const Renderer = renderers[ast.type] ?? (() => `unknown element: ${type}`); const Renderer = renderers[ast.type] ?? (() => `unknown element: ${type}`);
return ( return (
<Renderer depth={depth} {...rest} {...nodeRest}> <Renderer transcluded={transcluded} depth={depth} {...rest} {...nodeRest}>
{children.map((c) => ( {children.map((c) => (
<Graphdown depth={depth+1} {...rest} ast={c} /> <Graphdown
transcluded={transcluded}
depth={depth + 1}
{...rest}
ast={c}
/>
))} ))}
</Renderer> </Renderer>
); );
@ -385,11 +394,10 @@ export const GraphContent = React.memo(function GraphContent(
api, api,
...rest ...rest
} = props; } = props;
const [,ast] = stitchAsts(contents.map(contentToMdAst(tall))); const [, ast] = stitchAsts(contents.map(contentToMdAst(tall)));
return ( return (
<Box {...rest}> <Box {...rest}>
<Graphdown api={api} ast={ast} /> <Graphdown transcluded={transcluded} api={api} ast={ast} />
</Box> </Box>
); );
}); });