MichaelHatherly / CommonMark.jl

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

interpolation: Pass-through IOContext to JuliaValue in HTML render #58

Closed fonsp closed 1 year ago

fonsp commented 1 year ago

Hey Michael 👋

This PR makes the rendering IOContext available to interpolated Julia objects. Example of using the IO context in an HTML render:

struct MyType
end

function Base.show(io::IO, m::MIME"text/html", x::MyType)
    if get(io, :limit, false) === true
        write(io, "hello")
    else
        write(io, "<marquee> hello hello hello </marquee>")
    end
end

This PR will fix https://github.com/fonsp/Pluto.jl/issues/1807 , because Pluto and https://github.com/JuliaPluto/AbstractPlutoDingetjes.jl use IOContext to communicate which Pluto API is available to the renderer.

This PR is similar to https://github.com/JuliaPluto/HypertextLiteral.jl/pull/27

Example notebook that works after this PR:

# ╔═╡ 618a92b2-a418-40c6-8d71-2129b056aa5c
begin
    import Pkg
    Pkg.activate(temp=true)
    Pkg.add([
        Pkg.PackageSpec(name="PlutoUI")
        Pkg.PackageSpec(url="https://github.com/fonsp/CommonMark.jl", rev="patch-3")
    ])
end

# ╔═╡ 97d567f2-8203-11ed-1857-bdfd226ad0b7
using CommonMark

# ╔═╡ d9207c56-d2b9-48bb-b803-b94a0b685daa
using PlutoUI

# ╔═╡ 8ae91af4-8651-4bc3-bd42-98909c6ee5a7
@bind a Slider(5:9)

# ╔═╡ d4fd82c7-7eaa-4c2c-9c37-5a54986b4b49
a

# ╔═╡ 93c0ba03-ff14-492c-95f7-b454afb4fb47
bb = cm"$(@bind b Slider(5:9))"

# ╔═╡ 4331aad6-b709-464e-8ec8-0b2cea99484e
b
MichaelHatherly commented 1 year ago

Thanks @fonsp. Will release a new version shortly containing this fix.