Skip to content
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

Interpolation Error (?) #22

Open
jeremiahpslewis opened this issue Jul 14, 2021 · 5 comments
Open

Interpolation Error (?) #22

jeremiahpslewis opened this issue Jul 14, 2021 · 5 comments

Comments

@jeremiahpslewis
Copy link
Contributor

jeremiahpslewis commented Jul 14, 2021

using CommonMark

cat = "test!"

# Works:
cm"""<b>Well done, your cat is called $(cat) now.</b>"""
cm"""<b>Well done, your cat is called $cat now.</b>"""

# Doesn't work:
cm"""<p><b>Well done, your cat is called $(cat) now.</b></p>"""
cm"""<p><b>Well done, your cat is called $cat now.</b></p>"""

Does not interpolate variable $cat.

Seems related to the nested xml...

@MichaelHatherly
Copy link
Owner

Seems related to the nested xml...

Yeah, it's just being caused by certain types of HTML blocks and their precedence: https://github.com/MichaelHatherly/CommonMark.jl/blob/master/src/parsers/blocks.jl#L3-L11.

This is something that's defined within the spec so can't be adjusted here. There may be a way to have interpolation happen prior to the HTML tags being parsed, but quite honestly it'll likely just lead to a lot of weird edge cases.

@jeremiahpslewis
Copy link
Contributor Author

Don't think this is an 'issue', necessarily, in the sense that if you're writing html, you shouldn't necessarily want to / need to use markdown, maybe worth adding to a doc page at some point in the future, I'll post a snippet below that could be used.

@jeremiahpslewis
Copy link
Contributor Author

Interpolation and HTML Snippets: Oil and Water

CommonMark.jl works great with html snippets, like:

<p><b>Well done, your cat is called Jimmy now.</b></p>

it also works seamlessly with variable interpolation in CommonMark markdown:

# This is a dynamically populated $variable and calculation $(1+1)

Unfortunately, like oil and water, html snippets and variable interpolation don’t mix, so please don’t expect:

<p><b>Well done, your cat is called $cat now.</b></p>

to return anything but the html, with the interpolation step ignored:

<p><b>Well done, your cat is called $cat now.</b></p>

If you want html and interpolation, then you're looking for:

HTML("<p><b>Well done, your cat is called $cat now.</b></p>")

@MichaelHatherly
Copy link
Owner

Yeah, something to that effect in the readme would be worth adding. In addition probably worth noting other places where interpolation can't work:

julia> using CommonMark

julia> a = cm"""
       ```
       $(1 + 2)
       ```
       """
   │ $(1 + 2)


julia> using Markdown

julia> b = md"""
       ```
       $(1 + 2)
       ```
       """
  $(1 + 2)

Luckily Markdown also fails there...

If you want html and interpolation, then you're looking for:

Or just parsing the string with CommonMark after it's interpolated using

p = Parser()
p("<p>$(1 + 2)</p>")

@jeremiahpslewis
Copy link
Contributor Author

+1 agree. Still fighting though for a 'give to Cesar what is Cesar's' in terms of using html utilities for html strings ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants