-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
inject script tag in mdx content #6327
Comments
Sorry for the frustration! I'm curious if |
@natemoo-re after testing, the I made a rewrite in Astro with a little enhancement. It feels a bit more like Here's for posterity: ---
import { readFileSync } from "fs"
import { dirname, join } from "path"
const { src, url } = Astro.props
export type Props = {
src: string
url?: string
}
const dir = url ? dirname(new URL(url).pathname) : process.cwd()
const code = readFileSync(join(dir, src), 'utf-8')
---
<script set:html={code} /> to be used as: import Script from '~/components/Script.astro'
<Script src='./client-script.js' url={import.meta.url} /> it's ok for small scripts especially if you're loading client-side umd libraries from unpkg, but i really do think there's something to do here to enable bundling, minification, etc... EDIT: I've used |
Cross posting from discord in case people come here from the web: Create an astro component called Scriptify that just contains a slot: // components/Scriptify.astro ---
---
<slot /> Then in your MDX: ---
title: MyPost
---
import Scriptify from "@components/Scriptify"
<Scriptify>
<script>
const a = 3;
const b = 2;
console.log(a * b);
</script>
</Scriptify> You should see "6" in your browser console. |
@feraleyebrows sadly it seems not to work: https://stackblitz.com/edit/github-4sr41n Have i missed something from your solution? |
@y-nk pushed an example repo here because stackblitz was being a pain. needed to add some curly braces to get it to work across multiple lines e.g:
https://github.com/feraleyebrows/script-in-mdx-astro-example |
I've made long comments on the Discord thread to explain what happens when we write this ↑ and why it's not a suitable solution. Although, it was good to mention it because it made me dig deeper into Astro's internal and probably found the bug. It seems that style and script tags should not be escaped by default. I know it's a security concern but imho it should be implemented differently. I understand it's a good feature to have but it should not be blocking from using plain working html. I've isolated the culprit here. astro/packages/astro/src/runtime/server/jsx.ts Lines 49 to 50 in 045262e
where it seems that script tags are escaped by default. How to reproduce in stackblitz:
When doing so, the only working example will be: <script>{`
window.alert('hello world with jsx')
`}</script> and that's to be expected because it's regular JSX syntax (even though it's painful) |
I've made #6459, which should fix the rendering issue you pointed above @y-nk (Thanks for debugging it). But it won't support directly using |
thanks a lot @bluwy :) if that's any help for people like me, i'm softly planning to publish a remark plugin which will patch the remark parser and add curly braces and backticks to script and style tags. it's a quick hack but i'm not that confident yet to modify the great remark mdx 😂 |
I actually was planning the exact same hack if we were to implement support too 🙈 But since that deviates from Astro's semantics for script and style tags, I've decided not to for now. If you do get around creating that plugin, I think it's great to experiment how well that works first! |
Thanks! It doesn't work when we have > (greater than sign)
error |
[Update] |
What version of
astro
are you using?latest
Are you using an SSR adapter? If so, which one?
no
What package manager are you using?
npm@8
What operating system are you using?
macOS@latest
Describe the Bug
Problem
When trying to add a script tag in an mdx file, the script tag isn't parsed properly.
Without valid JSX syntax, as expected the content gets parsed as html, quotes are properly respected:
With valid JSX syntax, surprisingly the content of the tag gets escaped:
I also tried
is:raw
,is:inline
and theclient:*
combinations without success.Goal
To inject client only small scripting without having anything else to do than
<script>
. I know that remark-mdx would replace script tags to div, but since it works here... why not push it to the max?Workaround
Add a framework integration and rely on them by creating a third party script component such as:
...but i wished i didn't have to rely on [React/Vue/etc...] integration to do this.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-vuknlr?file=src/pages/index.mdx&on=stackblitz
Participation
The text was updated successfully, but these errors were encountered: