Replies: 1 comment 7 replies
-
to clarify your goal is to write something that executes the code AND displays it as a "standard" markdown code block... e.g. ```js [some-id]
const foo = 'bar';
``` should result in something like this (simplified) <script type="module">
const foo = 'bar';
</script>
<code><pre>
const foo = 'bar';
</pre></code> |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Use case
Often I need to show the setup code to the users, because it's important for the demos underneath.
This setup code doesn't contain demos, just some JS or CSS which needs to be executed, without any direct visuals, so I want to avoid rendering it in a story wrapper.
I also reason about this in the following manner:
js preview-story
)js story
)js script
)js
, just standard Markdown)Examples
Show and execute setup JS code, e.g. how to register custom elements in preparation for the demos below with code.
Live: https://backlight.dev/edit/xS11aQmjMLIvr5PU5Mru/flipper/doc/flipper.md?p=doc
I have to duplicate the setup code in
js script
and in thejs
code block.Show and render setup CSS code, e.g. classes which I then use in the demos underneath, so users need to read this CSS code first to understand the follow-up demos code.
Live: https://backlight.dev/edit/4BeMe20hqOWTkdUL2NuJ/box/doc/box.md?branch=feature-request-show-setup-code%4044v7XI9EE5hzBwU7oxi8dbGjKz03&p=doc
I use a workaround here not to duplicate the code, but I had to add a paragraph to invite to read it, although I want to just show the code block immediately without this fancy wrapper around it and not hide it behind a button-click.
Proposals
All proposal are also applicable to
html
code blocks, but I'm usingjs smth
for readability.js preview-script
Given
js preview-story
differs fromjs story
in the fact that it does show the code.We can make
js preview-script
to differ fromjs script
in the same way.Important remark: such block can return smth to render (not really a story though) or can just be some JS to execute.
Pros:
Cons:
html preview-script
reads weird, because it's not really a script 🤷js script
is not designed for rendering, while this feature assumes rendering is needed (not always, but if the store is returned), just without extra UI wrapperExample 1 with this proposal:
js preview-script
Example 2 with this proposal:
html preview-script
js story no-ui
,js story no-ui no-render
Not a big fan of this, but just sharing for completeness.
The idea is to control if the story UI wrapper is shown and if the code needs to be rendered in the DOM.
Cons:
js story
is not designed forjs script
tojs setup
, add newjs preview-setup
for executable JS blocks andjs/html preview-render-setup
for rendered blocksThis combines best of previous two and takes into account their limitations.
js script
can be left for backwards compatibility, but I think it's good to rename it given it's kinda the same thing semantically.But you can also argue it's unrelated, and that makes it just a few new ones.
Example 1 with this proposal:
js preview-setup
Example 2 with this proposal (given version with both js and html for clarity)
js preview-render-setup
html preview-render-setup
Glad to discuss other proposals, certainly there is space for better API ideas.
Beta Was this translation helpful? Give feedback.
All reactions