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

HypertextLiteral.jl + CommonMark.jl = 🤯 #31

Closed
fonsp opened this issue Dec 16, 2021 · 10 comments
Closed

HypertextLiteral.jl + CommonMark.jl = 🤯 #31

fonsp opened this issue Dec 16, 2021 · 10 comments

Comments

@fonsp
Copy link
Contributor

fonsp commented Dec 16, 2021

I made a combination of HypertextLiteral.jl by @clarkevans and CommonMark.jl, and I think it is really cool!!

DEMO NOTEBOOK

Screenshots

Schermafbeelding 2021-12-16 om 13 17 09

Schermafbeelding 2021-12-16 om 13 15 48

Features

The list of features is really simple to explain: it is everything that CommonMark gives, plus everything that HypertextLiteral gives! This includes:

  • CommonMark! Markdown but less glitchy!
  • Really flexible interpolation support with infinite nesting and syntax highlighting (since it is a @md(""" macro instead of md""")
  • Interpolate Julia objects into <script> to automatically convert to JS literals
  • Context-aware HTML escaping
  • Automatic quote wrapping for HTML attributes
  • Use a Dict or NamedTuple for the style attribute inside an HTML tag

Implementation

Also cool: the code is extremely short!

macro md(expr)
	cm_parser = CommonMark.Parser()
	quote
		result = @htl($expr)
		htl_output = repr(MIME"text/html"(), result)

		$(cm_parser)(htl_output)
	end
end

It is essentially the @htl macro for HypertextLiteral.jl, but the result is passed through a CommonMark parser. This works, because:

  • CommonMark allows raw HTML
  • HypertextLiteral leaves literal content unchanged, so hello *world* appears exactly

Let me know what you think! Perhaps it can be added to this package as a new macro, or we can make a new package.

@MichaelHatherly
Copy link
Owner

That's awesome. I'm quite surprised that commonmark's HTML handling is actually sufficient to parse htl_output correctly.

@MichaelHatherly
Copy link
Owner

Regarding including it here, I'd like to remove deps where possible since the eventual end goal is a standard lib. Might be a PlutoUI macro, though whether @md is actually "UI" is debatable I guess...

@fonsp
Copy link
Contributor Author

fonsp commented Dec 16, 2021

That makes sense! I really hope it becomes a stdlib!

I like the idea of including it in PlutoUI. Let's continue testing it to see if there are any unforseen problems, and then release it!

@fonsp
Copy link
Contributor Author

fonsp commented Dec 16, 2021

We will discuss this at the weekly pluto developers call in 1 hour: https://julialang.org/community/#events @MichaelHatherly @jeremiahpslewis feel free to join if you have time!

@MichaelHatherly
Copy link
Owner

https://github.com/MichaelHatherly/CommonMark.jl/blob/master/src/extensions/interpolation.jl#L121-L152 is the set of extensions that get used in cm"" which is probably what you'd actually want to use in the "real" @mdl macro. It's an internal method so probably don't use it directly, we could export a better named version of that one perhaps.

@fonsp
Copy link
Contributor Author

fonsp commented Dec 17, 2021

Thanks for the pointers! I will add those

@fonsp
Copy link
Contributor Author

fonsp commented Dec 17, 2021

FYI I made https://github.com/JuliaPluto/MarkdownLiteral.jl ! I am still wondering what the best name would be, my favourites are:

  • @md (this is not used by any package yet: [1] [2]), but it might be confusingly close to md"""
  • @markdown (also not taken) is probably my favourite because it is unintimidating and clear. But it does not capture the HTL magic
  • @markdownliteral maybe? Long names are fine because we will eventually use a GUI button
  • @mdl is accurate but something abstract to force people to remember

@MichaelHatherly
Copy link
Owner

Could go with @mdx since the syntax supported is kind of like https://mdxjs.com/ though not quite.

@lukeburns
Copy link

This is great, so much easier to interpolate in latex expressions!

@MichaelHatherly
Copy link
Owner

I'll close this issue now since there isn't much more needed here.

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

3 participants