Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
CodeGroupAutoload (#115)
Pavel 'Strajk' Dolecek6518663d0a53
diff --git a/apps/docs/src/app/playgrounds/overview/page.mdx b/apps/docs/src/app/playgrounds/overview/page.mdxindex dc6b4f1ae..c5d14f93f 100644--- a/apps/docs/src/app/playgrounds/overview/page.mdx+++ b/apps/docs/src/app/playgrounds/overview/page.mdx@@ -22,11 +22,4 @@ The AI playgorunds run on E2B's cloud infrastructure. Use our [SDK](/getting-staHere's a quick code example showing how to create an AI Playground.-<CodeGroup>-```python {{ language: 'python', snippet: 'python/basics/init.py' }}-```-```js {{ language: 'js', snippet: 'js/basics/init.js' }}-```-</CodeGroup>--Head over to [SDK Basics](/getting-started/basics) to see more quick start examples.+<CodeGroupAutoload path="basics/init" />diff --git a/apps/docs/src/components/Code.tsx b/apps/docs/src/components/Code.tsxindex 0181539e8..55aeea05a 100644--- a/apps/docs/src/components/Code.tsx+++ b/apps/docs/src/components/Code.tsx@@ -78,7 +78,6 @@ function CodePanel({code?: string}) {let child = Children.only(children)-if (isValidElement(child)) {tag = child.props.tag ?? taglabel = child.props.label ?? label@@ -250,14 +249,13 @@ export function CodeGroup({children,title,...props-}: React.ComponentPropsWithoutRef<typeof CodeGroupPanels> & { title: string }) {+}: React.ComponentPropsWithoutRef<typeof CodeGroupPanels> & { title?: string }) {let languages =Children.map(children, (child) =>getPanelTitle(isValidElement(child) ? child.props : {})) ?? []let tabGroupProps = useTabGroupProps(languages)let hasTabs = Children.count(children) > 1-let containerClassName ='not-prose my-6 overflow-hidden rounded-2xl bg-zinc-900 shadow-md dark:ring-1 dark:ring-white/10'let header = (@@ -319,3 +317,14 @@ export function Pre({return <CodeGroup {...props}>{children}</CodeGroup>}++/**+ * Special Component just for MDX files, processed by Remark+ */+export function CodeGroupAutoload({children}) {+ if (!children) {+ console.warn(`CodeGroupAutoload: No children provided – something is wrong with your MDX file`)+ return null+ }+ return <CodeGroup>{children}</CodeGroup>+}diff --git a/apps/docs/src/components/mdx.tsx b/apps/docs/src/components/mdx.tsxindex f38a366f4..95e4d52d2 100644--- a/apps/docs/src/components/mdx.tsx+++ b/apps/docs/src/components/mdx.tsx@@ -7,7 +7,7 @@ import { Prose } from '@/components/Prose'export const a = Linkexport { Button } from '@/components/Button'-export { CodeGroup, Code as code, Pre as pre } from '@/components/Code'+export { CodeGroupAutoload, CodeGroup, Code as code, Pre as pre } from '@/components/Code'export function wrapper({ children }: { children: React.ReactNode }) {return (diff --git a/apps/docs/src/mdx/remark.mjs b/apps/docs/src/mdx/remark.mjsindex 327d4bed4..188b29217 100644--- a/apps/docs/src/mdx/remark.mjs+++ b/apps/docs/src/mdx/remark.mjs@@ -4,6 +4,12 @@ import { visit } from 'unist-util-visit'import { readFile } from 'fs/promises'import path from 'path'import JSON5 from 'json5'+import fs from 'fs';++const lang2ext = {+ js: 'js',+ python: 'py'+}function loadFileSnippet() {return async (tree) => {@@ -39,4 +45,44 @@ function loadFileSnippet() {}}-export const remarkPlugins = [mdxAnnotations.remark, remarkGfm, loadFileSnippet]+/**+ * Process <CodeGroupAutoload /> to add code snippets for each language+ * Depends on loadFileSnippet() to load the actual code+ */+function processCodeGroupAutoload() {+ return (tree) => {+ visit(tree, 'mdxJsxFlowElement', (node) => {+ if (node.name !== 'CodeGroupAutoload') return // only process <CodeGroupAutoload />+ const { attributes, children } = node+ const attrPath = attributes.find(({ name }) => name === 'path')?.value+ if (children.length) throw new Error(`CodeGroupAutoload should not have children`)+ if (!attrPath) throw new Error(`CodeGroupAutoload should have "path" attribute`)+ node.name = `CodeGroup` // rename to <CodeGroup />+ for (const lang of [`js`, `python`]) {+ const snippetPath = `${lang}/${attrPath}.${lang2ext[lang]}`+ const fileExists = fs.existsSync(path.resolve(`./src/code/${snippetPath}`))+ if (!fileExists) {+ console.warn(`CodeGroupAutoload: snippet "${attrPath}" does not exist for language "${lang}"`)+ continue+ }+ node.children.push({+ type: 'code',+ value: '', // loadFileSnippet() is checking for this+ lang,+ data: {+ hProperties: {+ annotation: `{ language: '${lang}', snippet: '${snippetPath}' }`+ }+ }+ })+ }+ })+ }+}++export const remarkPlugins = [+ processCodeGroupAutoload,+ mdxAnnotations.remark,+ remarkGfm,+ loadFileSnippet+]