-
Notifications
You must be signed in to change notification settings - Fork 840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add code transformer to <Demo>
component
#7894
Merged
tkajtoch
merged 5 commits into
elastic:main
from
tkajtoch:feat/interactive-example-transformer
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33aef59
feat(docusaurus-theme): set max height on `<Demo>` code editor
tkajtoch 104604f
feat(docusaurus-theme): switch back to `div` for `<Demo>` component w…
tkajtoch e468808
feat(docusaurus-theme): add code transformer
tkajtoch 3beb4ce
feat(docusaurus-theme): allow extending `<Demo>` scope via the `scope…
tkajtoch af2830c
chore: update button examples to include existing EUI docs code
tkajtoch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/docusaurus-theme/src/components/demo/code_transformer.ts
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,36 @@ | ||
const IMPORT_REGEX = /^import [^'"]* from ['"]([^.'"\n ][^'"\n ]*)['"];?/gm; | ||
const DEFAULT_EXPORT_REGEX = /export default /; | ||
const COMPONENT_ONLY_REGEX = /^\(?</; | ||
|
||
/** | ||
* Transforms input JS/TS source code to a react-live compatible syntax. | ||
* react-live uses the surcase library to transform input source code into | ||
* browser-readable JavaScript. | ||
* | ||
* While surcase does support CommonJS and ESM import/export statements, | ||
* it's not trivial to expose our internal React and EUI exports through it | ||
* and because we already control the execution scope of the interactive demos | ||
* it isn't really necessary to implement a smart `require()` replacement. | ||
* | ||
* Returning an IIFE is necessary when the source code is more than just | ||
* a JSX component definition (e.g. it contains a variable definition | ||
* or `export default` statement). | ||
* | ||
* @see {@link https://github.com/alangpierce/sucrase} | ||
* @see {@link https://github.com/FormidableLabs/react-live/blob/master/packages/react-live/src/utils/transpile/index.ts} | ||
*/ | ||
export const demoCodeTransformer = (code: string) => { | ||
// Remove ESM imports | ||
code = code.replace(IMPORT_REGEX, ''); | ||
|
||
// Handle ESM default exports | ||
code = code.replace(DEFAULT_EXPORT_REGEX, 'return '); | ||
|
||
// If the demo is JSX only return as-is | ||
if (COMPONENT_ONLY_REGEX.test(code)) { | ||
return code; | ||
} | ||
|
||
// If the demo is more than just JSX wrap in an immediately invoked function expression | ||
return `(() => { ${code} })()`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧠 |
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import React from 'react'; | ||
import * as EUI from '@elastic/eui'; | ||
|
||
export const demoScope: Record<string, unknown> = { | ||
export const demoDefaultScope: Record<string, unknown> = { | ||
// React | ||
React, | ||
...React, | ||
|
||
// EUI exports | ||
...EUI, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks for the great description, that's really helpful for understanding! 🙏