-
Notifications
You must be signed in to change notification settings - Fork 186
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(config): Add .processCode config as a hook for complex usecases #262
base: master
Are you sure you want to change the base?
Conversation
38b6285
to
4c29faa
Compare
For a slightly more complex example of what export default function processCode(code: string) {
// Everything after the last blank link is considered "rendered"
const codeLines = code.trim().split('\n\n');
const jsx = codeLines.pop() || '';
// Wrap the entire code block in an IIFE so we have a scope where
// arbitrary JS (including React state) can be executed
return `{
(() => {
${codeLines.join('\n\n')}
${
// When there's already a `return`, leave it as-is
jsx.trim()?.startsWith('return')
? jsx
// Otherwise, wrap it in a return + fragment for safe rendering.
: `return (
<>
${jsx}
</>
)`
}
})()
}`;
} Some examples of the above code working: |
Some investigation reveals that code formatting does not consider any changes returned from If we were to call For our particular usecase, we've disabled formatting by patching our installation of Playroom, but that's not a great solution. I'm not sure if this issue should block the PR or not? |
@seek-oss/playroom-maintainers any interest in this contribution? |
We're definitely keen on exploring this! I do think the code formatting issue is a show-stopper, so I don't think we should open up such a low level feature if it won't integrate cleanly with the rest of Playroom. It might be worth exploring more high-level, built-in support for multiple expressions beyond just JSX. I can see a couple of viable options for this:
|
As added to the
README
:This change provides an escape hatch to fix #66, like so:
Create a new file
processCode.ts
in our repo:Add it to our
playroom.config.js
:Fire up Playroom, and add the following code:
This PR was co-authored by @gwyneplaine