From 173f9cae225e9e9dc6f49aa4217895a906f1242d Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 23 May 2024 11:46:21 +0200 Subject: [PATCH] try wrapping script nodes in ClientOnly --- Project.toml | 13 +++++++------ src/writer.jl | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 6077ea34..7e97a742 100644 --- a/Project.toml +++ b/Project.toml @@ -8,11 +8,18 @@ ANSIColoredPrinters = "a4c015fc-c6ff-483c-b24f-f7ea428134e9" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" NodeJS_20_jll = "c7aee132-11e1-519c-8219-0a43005e73c2" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" +[weakdeps] +DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244" + +[extensions] +DocumenterVitepressDocumenterCitationsExt = "DocumenterCitations" + [compat] ANSIColoredPrinters = "0.0.1" DocStringExtensions = "0.9" @@ -21,9 +28,3 @@ DocumenterCitations = "1" IOCapture = "0.2" NodeJS_20_jll = "20" julia = "1.6" - -[weakdeps] -DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244" - -[extensions] -DocumenterVitepressDocumenterCitationsExt = "DocumenterCitations" diff --git a/src/writer.jl b/src/writer.jl index 2fb7e038..78ce6575 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -1,5 +1,6 @@ import Documenter: Documenter, Builder, Expanders, MarkdownAST import Documenter.DOM: escapehtml +import EzXML import ANSIColoredPrinters using Base64: base64decode, base64encode @@ -474,7 +475,7 @@ function render_mime(io::IO, mime::MIME"text/markdown", node, element, page, doc end function render_mime(io::IO, mime::MIME"text/html", node, element, page, doc; kwargs...) - println(io, element) + print_with_script_tags_wrapped(io, element) end function render_mime(io::IO, mime::MIME"image/svg+xml", node, element, page, doc; kwargs...) @@ -868,3 +869,37 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod println(io) println(io, "![]($image_path)") end + + +wrap(html) = "$html" # because html strings can be multiple sibling nodes + +function wrap_script_tags!(node) + if EzXML.nodetype(node) === EzXML.ELEMENT_NODE && EzXML.nodename(node) == "script" + parent = EzXML.parentnode(node) + nxt = EzXML.hasnextnode(node) ? EzXML.nextnode(node) : nothing + EzXML.unlink!(node) + wrappernode = EzXML.ElementNode("ClientOnly") + EzXML.link!(wrappernode, node) + if nxt === nothing + EzXML.link!(parent, wrappernode) + else + EzXML.linkprev!(nxt, wrappernode) + end + node = wrappernode + elseif EzXML.hasnode(node) + wrap_script_tags!(EzXML.firstnode(node)) + end + if EzXML.hasnextnode(node) + wrap_script_tags!(EzXML.nextnode(node)) + end + return +end + +function print_with_script_tags_wrapped(io, str) + xml = EzXML.parsexml(wrap(str)) + wrap_script_tags!(xml.root) + for node in EzXML.eachnode(xml.root) + EzXML.prettyprint(io, node) + end + return +end