William FH
2023-07-26 12:38:58 -07:00
committed by GitHub
parent a612800ef0
commit 01a9b06400
16 changed files with 263 additions and 28 deletions

View File

@@ -21,7 +21,7 @@ function Imports({ imports }) {
</h4>
<ul style={{ paddingBottom: "1rem" }}>
{imports.map(({ imported, source, docs }) => (
<li>
<li key={imported}>
<a href={docs}>
<span>{imported}</span>
</a>{" "}
@@ -34,14 +34,25 @@ function Imports({ imports }) {
}
export default function CodeBlockWrapper({ children, ...props }) {
// Initialize imports as an empty array
let imports = [];
// Check if children is a string
if (typeof children === "string") {
return <CodeBlock {...props}>{children}</CodeBlock>;
// Search for an IMPORTS comment in the code
const match = /<!--IMPORTS:(.*?)-->\n/.exec(children);
if (match) {
imports = JSON.parse(match[1]);
children = children.replace(match[0], "");
}
} else if (children.imports) {
imports = children.imports;
}
return (
<>
<CodeBlock {...props}>{children.content}</CodeBlock>
<Imports imports={children.imports} />
<CodeBlock {...props}>{children}</CodeBlock>
{imports.length > 0 && <Imports imports={imports} />}
</>
);
}
}