-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mdx-code): add sandpack live code editor support (#794)
close #777
- Loading branch information
1 parent
91eb4e2
commit 1f7945e
Showing
11 changed files
with
733 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import Editor from './Editor'; | ||
|
||
describe('Editor', () => { | ||
test('should render sandpack correctly', () => { | ||
render( | ||
<Editor> | ||
<pre> | ||
<code className="tsx">const foo = bar();</code> | ||
</pre> | ||
{/* @ts-expect-error code meta data */} | ||
<pre filename="bar.tsx"> | ||
<code className="tsx">const bar = foo();</code> | ||
</pre> | ||
</Editor> | ||
); | ||
|
||
expect(screen.getByRole('tablist')).toBeInTheDocument(); | ||
expect(screen.getByText('App.tsx')).toBeInTheDocument(); | ||
expect(screen.getByText('bar.tsx')).toBeInTheDocument(); | ||
expect(screen.getByRole('group')).toBeInTheDocument(); | ||
expect(screen.getByText('const foo = bar();')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { SandpackPredefinedTemplate } from '@codesandbox/sandpack-react'; | ||
import { monokaiProTheme, Sandpack } from '@codesandbox/sandpack-react'; | ||
import '@codesandbox/sandpack-react/dist/index.css'; | ||
import type { ReactElement, ReactNode } from 'react'; | ||
import React from 'react'; | ||
import { languageToFilepath, normalizeFilepath } from './utils'; | ||
|
||
interface Props { | ||
template?: SandpackPredefinedTemplate; | ||
children?: ReactNode; | ||
} | ||
|
||
const Editor = ({ template = 'react-ts', children }: Props): JSX.Element => { | ||
const codeSnippets = React.Children.toArray(children); | ||
const files = codeSnippets.reduce( | ||
( | ||
result: { | ||
[key in string]: { | ||
code: any; | ||
}; | ||
}, | ||
codeSnippet | ||
) => { | ||
const preElement = codeSnippet as ReactElement; | ||
const codeElement = preElement.props.children; | ||
|
||
const filename = preElement.props.filename; | ||
const language = codeElement.props.className.replace('language-', ''); | ||
const filePath = | ||
normalizeFilepath(filename) || languageToFilepath(language); | ||
const code = codeElement.props.children; | ||
|
||
result[filePath] = { | ||
code, | ||
}; | ||
|
||
return result; | ||
}, | ||
{} | ||
); | ||
|
||
return ( | ||
<Sandpack | ||
template={template} | ||
theme={monokaiProTheme} | ||
customSetup={{ | ||
files, | ||
dependencies: {}, | ||
}} | ||
options={{ | ||
showLineNumbers: true, | ||
showInlineErrors: false, | ||
showTabs: true, | ||
externalResources: [], | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default Editor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './Editor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { languageToFilepath, normalizeFilepath } from './utils'; | ||
|
||
describe('languageToFilepath', () => { | ||
test('should return /styles.css if language is css', () => { | ||
expect(languageToFilepath('css')).toBe('/styles.css'); | ||
}); | ||
|
||
test('should return /App.js if language is js', () => { | ||
expect(languageToFilepath('js')).toBe('/App.js'); | ||
expect(languageToFilepath('javascript')).toBe('/App.js'); | ||
}); | ||
|
||
test('should return /App.ts if language is ts', () => { | ||
expect(languageToFilepath('ts')).toBe('/App.ts'); | ||
expect(languageToFilepath('typescript')).toBe('/App.ts'); | ||
}); | ||
|
||
test('should return /App.jsx if language is jsx', () => { | ||
expect(languageToFilepath('jsx')).toBe('/App.jsx'); | ||
}); | ||
|
||
test('should return /App.tsx if language is tsx', () => { | ||
expect(languageToFilepath('tsx')).toBe('/App.tsx'); | ||
}); | ||
|
||
test('should return /src/App.vue if language is vue', () => { | ||
expect(languageToFilepath('vue')).toBe('/src/App.vue'); | ||
}); | ||
|
||
test('should return /App.tsx if language is not provided', () => { | ||
expect(languageToFilepath()).toBe('/App.tsx'); | ||
}); | ||
}); | ||
|
||
describe('normalizeFilepath', () => { | ||
test('should return empty string if no filename is provided', () => { | ||
expect(normalizeFilepath()).toBe(''); | ||
}); | ||
|
||
test('should return filename if it starts with /', () => { | ||
expect(normalizeFilepath('/foo.tsx')).toBe('/foo.tsx'); | ||
}); | ||
|
||
test('should return /filename if it does not start with /', () => { | ||
expect(normalizeFilepath('foo.tsx')).toBe('/foo.tsx'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const normalizeFilepath = (filename?: string): string => { | ||
if (!filename) { | ||
return ''; | ||
} | ||
|
||
if (!filename.startsWith('/')) { | ||
return `/${filename}`; | ||
} | ||
|
||
return filename; | ||
}; | ||
|
||
const languageToFilepath = (language?: string): string => { | ||
switch (language) { | ||
case 'css': | ||
return '/styles.css'; | ||
case 'js': | ||
case 'javascript': | ||
return '/App.js'; | ||
case 'ts': | ||
case 'typescript': | ||
return '/App.ts'; | ||
case 'jsx': | ||
return '/App.jsx'; | ||
case 'tsx': | ||
return '/App.tsx'; | ||
case 'vue': | ||
return '/src/App.vue'; | ||
default: | ||
return '/App.tsx'; | ||
} | ||
}; | ||
|
||
export { languageToFilepath, normalizeFilepath }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1f7945e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blog – ./
blog-git-main-sabertaz.vercel.app
blog.tazimi.dev
blog-sabertaz.vercel.app