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

Try wrapping script nodes in ClientOnly #144

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
37 changes: 36 additions & 1 deletion src/writer.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Documenter: Documenter, Builder, Expanders, MarkdownAST
import Documenter.DOM: escapehtml
import EzXML

import ANSIColoredPrinters
using Base64: base64decode, base64encode
Expand Down Expand Up @@ -474,7 +475,7 @@
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...)
Expand Down Expand Up @@ -666,7 +667,7 @@
# Code blocks
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, code::MarkdownAST.CodeBlock, page, doc; kwargs...)
if startswith(code.info, "@")
@warn """

Check warning on line 670 in src/writer.jl

View workflow job for this annotation

GitHub Actions / build

DocumenterVitepress: un-expanded `@doctest` block encountered on page src/code_example.md. The first few lines of code in this node are: ``` julia> 1 + 1 2 ```
DocumenterVitepress: un-expanded `$(code.info)` block encountered on page $(page.source).
The first few lines of code in this node are:
```
Expand Down Expand Up @@ -868,3 +869,37 @@
println(io)
println(io, "![]($image_path)")
end


wrap(html) = "<WRAPPER>$html</WRAPPER>" # 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
Loading