MichaelHatherly / CommonMark.jl

A CommonMark-compliant Markdown parser for Julia.
Other
84 stars 11 forks source link

Content inside `<script>` inside paragraph #37

Closed fonsp closed 2 years ago

fonsp commented 2 years ago

I am having some trouble using <script> tags inside CommonMark, because the script contents are changed.

I have the following commonmark input:

asdf

asdf <script>
console.log("123")
console.log(1 *2* 3)
</script>

When parsed with the default CommonMark parser and rendered to text/html, we get:

<p>asdf</p>
<p>asdf <script>
console.log(&quot;123&quot;)
console.log(1 <em>2</em> 3)
</script></p>

(Also, with the TypographyRule, the directed quotes don't work in JS):

<p>asdf</p>
<p>asdf <script>
console.log(“123”)
console.log(1 <em>2</em> 3)
</script></p>

Would it be possible to special-case <script> and leave the contents as-is?

Test code ```julia # ╔═╡ fc0d1ac3-073f-444c-8120-ac4259735d48 repr(MIME"text/html"(), cm_parser(""" asdf asdf """)) |> Text # ╔═╡ aeafb8c2-083c-47c9-a86e-2d1c2dff445e begin cm_parser = CommonMark.Parser() CommonMark.enable!(cm_parser, [ # CommonMark.AdmonitionRule(), # CommonMark.AttributeRule(), # CommonMark.AutoIdentifierRule(), # CommonMark.CitationRule(), # CommonMark.FootnoteRule(), # CommonMark.MathRule(), # CommonMark.RawContentRule(), # CommonMark.TableRule(), # CommonMark.TypographyRule(), ]) end ```
fonsp commented 2 years ago

Also note the second asdf which is necessary for the MWE. It triggers the <script> to be inside a paragraph.

MichaelHatherly commented 2 years ago

https://spec.commonmark.org/0.30/#html-blocks the spec requires <script> as the beginning of the line. If you put a \n between asdf and <script> does it work correctly?

MichaelHatherly commented 2 years ago

https://spec.commonmark.org/dingus/?text=asdf%0A%0Aasdf%20%3Cscript%3E%0Aconsole.log(%22123%22)%0Aconsole.log(1%20*2*%203)%0A%3C%2Fscript%3E reference implementation looks to have the apparent same behavior as CM.jl judging by your MWE.

fonsp commented 2 years ago

Cool, thanks!

fonsp commented 2 years ago

The context here is Pluto components that use a <script>, and you want to use them inline.

E.g. this does not work:

@mdx("""
Hello $(Scrubbable(123))
""")

I guess the solution is to do add newlines within the widget, like:

<plutoui-scrubbable>

<script>
...
</script></plutoui-scrubbable>

But then I need to solve https://github.com/fonsp/Pluto.jl/issues/1574 🤔