Skip to content
Open
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: 4 additions & 1 deletion packages/mdxe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dist",
"src/app",
"src/config",
"src/payload"
"src/payload",
"src/components"
],
"scripts": {
"build": "tsc && chmod +x bin/mdxe.js",
Expand Down Expand Up @@ -44,6 +45,8 @@
"react-dom": "^19.1.0",
"sqlite3": "^5.1.6",
"tailwindcss": "^3.4.1",
"@monaco-editor/react": "^4.5.1",
"monaco-editor": "^0.38.0",
"undici": "^7.8.0",
"vitest": "^3.1.3"
},
Expand Down
41 changes: 41 additions & 0 deletions packages/mdxe/src/components/NotebookCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client'

import { useState, useEffect } from 'react'
import Editor from '@monaco-editor/react'
import { useDebouncedValue } from './useDebouncedValue'

export interface NotebookCellProps {
initialCode?: string
language?: string
className?: string
debounce?: number
}

export const NotebookCell = ({ initialCode = '', language = 'javascript', className, debounce = 300 }: NotebookCellProps) => {
const [code, setCode] = useState(initialCode)
const [output, setOutput] = useState('')

const debouncedCode = useDebouncedValue(code, debounce)

useEffect(() => {
try {
const result = new Function(debouncedCode)()
if (result instanceof Promise) {
result.then((res) => setOutput(String(res))).catch((err) => setOutput(String(err)))
} else {
setOutput(String(result))
}
} catch (err) {
setOutput(String(err))
}
}, [debouncedCode])

return (
<div className={`notebook-cell border rounded-md my-4 ${className ?? ''}`.trim()}>
<Editor height='200px' defaultLanguage={language} value={code} onChange={(value) => setCode(value || '')} options={{ minimap: { enabled: false } }} />
<div className='preview p-2 bg-gray-50 border-t whitespace-pre-wrap font-mono text-sm'>{output}</div>
</div>
)
}

export default NotebookCell
3 changes: 3 additions & 0 deletions packages/mdxe/src/components/notebookCell.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.notebook-cell .monaco-editor {
border-bottom: 1px solid #e5e7eb; /* tailwind gray-200 */
}
12 changes: 12 additions & 0 deletions packages/mdxe/src/components/useDebouncedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState, useEffect } from 'react'

export function useDebouncedValue<T>(value: T, delay: number): T {
const [debounced, setDebounced] = useState(value)

useEffect(() => {
const timer = setTimeout(() => setDebounced(value), delay)
return () => clearTimeout(timer)
}, [value, delay])

return debounced
}
12 changes: 4 additions & 8 deletions packages/mdxe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ export const version = '0.1.0'

export { useMDXComponents } from './app/mdx-components'

export { NotebookCell } from './components/NotebookCell'
export { useDebouncedValue } from './components/useDebouncedValue'

export { types } from './config/types.js'

// export { createPayloadClient, getPayloadConfig } from './payload'

export {
isDirectory,
isMarkdownFile,
findIndexFile,
resolvePath,
getAllMarkdownFiles,
filePathToRoutePath
} from './utils/file-resolution.js'
export { isDirectory, isMarkdownFile, findIndexFile, resolvePath, getAllMarkdownFiles, filePathToRoutePath } from './utils/file-resolution.js'
Loading