Skip to content

Commit 689370a

Browse files
authored
Merge pull request #36 from LuxDL/as/fix_paths
try to fix more path issues
2 parents fa144c5 + 71ff605 commit 689370a

File tree

4 files changed

+175
-68
lines changed

4 files changed

+175
-68
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DocumenterVitepress"
22
uuid = "4710194d-e776-4893-9690-8d956a29c365"
33
authors = ["Lazaro Alonso <[email protected]>", "Anshul Singhvi <[email protected]>"]
4-
version = "0.0.4"
4+
version = "0.0.5"
55

66
[deps]
77
ANSIColoredPrinters = "a4c015fc-c6ff-483c-b24f-f7ea428134e9"

docs/src/.vitepress/theme/style.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,16 @@ mjx-container > svg {
167167
--vp-c-brand-3: #91dd33;
168168
--vp-c-sponsor: #91dd33;
169169
--vitest-c-sponsor-hover: #e51370;
170+
}
171+
172+
/**
173+
* Change images from light to dark theme
174+
* -------------------------------------------------------------------------- */
175+
176+
:root:not(.dark) .dark-only {
177+
display: none;
178+
}
179+
180+
:root:is(.dark) .light-only {
181+
display: none;
170182
}

src/vitepress_config.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ function modify_config_file(doc, settings, deploy_decision)
1010
if !isdir(joinpath(doc.user.build, settings.md_output_path, ".vitepress", "theme"))
1111
cp(joinpath(dirname(@__DIR__), "docs", "src", ".vitepress", "theme"), joinpath(doc.user.build, settings.md_output_path, ".vitepress", "theme"); follow_symlinks = true)
1212
end
13-
cp(joinpath(@__DIR__, "docs", "src", ".vitepress", "config.mts"), vitepress_config_file)
13+
cp(joinpath(dirname(@__DIR__), "docs", "src", ".vitepress", "config.mts"), vitepress_config_file)
14+
cp(joinpath(dirname(@__DIR__), "docs", "src", "components"), joinpath(doc.user.build, settings.md_output_path, "components"))
1415
end
1516

1617
config = read(vitepress_config_file, String)
17-
replacers = Vector{Pair{String, String}}()
18+
replacers = Vector{Pair{<: AbstractString, <: AbstractString}}()
1819

1920

2021
# # Vitepress base path
@@ -69,13 +70,13 @@ function pagelist2str(doc, page::String)
6970
else
7071
elements[idx].text[1]
7172
end
72-
return "{ text: '$name', link: '/$(splitext(page)[1])', $(sidebar_items(doc, page)) }"
73+
return "{ text: '$name', link: '/$(splitext(page)[1])' }" # , $(sidebar_items(doc, page)) }"
7374
end
7475

7576
function pagelist2str(doc, name_page::Pair{String, String})
7677
name, page = name_page
7778
# This is the simplest and easiest case.
78-
return "{ text: '$name', link: '/$(splitext(page)[1])', $(sidebar_items(doc, page)) }"
79+
return "{ text: '$name', link: '/$(splitext(page)[1])' }" # , $(sidebar_items(doc, page)) }"
7980
end
8081

8182
function pagelist2str(doc, name_contents::Pair{String, <: AbstractVector})

src/writer.jl

Lines changed: 157 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ function render(doc::Documenter.Document, settings::MarkdownVitepress=MarkdownVi
115115
end
116116
end
117117

118+
mkpath(joinpath(builddir, "final_site"))
119+
118120
# We manually obtain the Documenter deploy configuration,
119121
# so we can use it to set Vitepress's settings.
120122
# TODO: make it so that the user does not have to provide a repo url!
@@ -157,7 +159,9 @@ function render(doc::Documenter.Document, settings::MarkdownVitepress=MarkdownVi
157159
rm(joinpath(dirname(builddir), "package-lock.json"))
158160
end
159161
end
160-
touch(joinpath(builddir, "final_site", ".nojekyll"))
162+
# This is only useful if placed in the root of the `docs` folder, and we don't
163+
# have any names which conflict with Jekyll (beginning with _ or .) in any case.
164+
# touch(joinpath(builddir, "final_site", ".nojekyll"))
161165

162166
# Clean up afterwards
163167
clean_md_output = isnothing(settings.clean_md_output) ? deploy_decision.all_ok : settings.clean_md_output
@@ -383,69 +387,151 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod
383387
end
384388

385389
# Select the "best" rendering MIME for markdown output!
386-
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, d::Dict{MIME, Any}, page, doc; kwargs...)
390+
391+
"""
392+
mime_priority(mime::MIME)::Float64
393+
394+
This function returns a priority for a given MIME type, which
395+
is used to select the best MIME type for rendering a given
396+
element.
397+
"""
398+
function mime_priority end
399+
mime_priority(::MIME"text/markdown") = 1.0
400+
mime_priority(::MIME"text/html") = 2.0
401+
mime_priority(::MIME"image/svg+xml") = 3.0
402+
mime_priority(::MIME"image/png") = 4.0
403+
mime_priority(::MIME"image/webp") = 5.0
404+
mime_priority(::MIME"image/jpeg") = 6.0
405+
mime_priority(::MIME"image/png+lightdark") = 7.0
406+
mime_priority(::MIME"image/jpeg+lightdark") = 8.0
407+
mime_priority(::MIME"image/svg+xml+lightdark") = 9.0
408+
mime_priority(::MIME"image/gif") = 10.0
409+
mime_priority(::MIME"video/mp4") = 11.0
410+
mime_priority(::MIME"text/plain") = 12.0
411+
mime_priority(::MIME) = Inf
412+
413+
function render_mime(io::IO, mime::MIME, node, element, page, doc; kwargs...)
414+
@warn("DocumenterVitepress: Unknown MIME type $mime provided and no alternatives given. Ignoring render!")
415+
end
416+
417+
function render_mime(io::IO, mime::MIME"text/markdown", node, element, page, doc; kwargs...)
418+
println(io, element)
419+
end
420+
421+
function render_mime(io::IO, mime::MIME"text/html", node, element, page, doc; kwargs...)
422+
println(io, element)
423+
end
424+
425+
function render_mime(io::IO, mime::MIME"image/svg+xml", node, element, page, doc; kwargs...)
426+
# NOTE: It seems that we can't simply save the SVG images as a file and include them
427+
# as browsers seem to need to have the xmlns attribute set in the <svg> tag if you
428+
# want to include it with <img>. However, setting that attribute is up to the code
429+
# creating the SVG image.
430+
image_text = d[MIME"image/svg+xml"()]
431+
# Additionally, Vitepress complains about the XML version and encoding string below,
432+
# so we just remove this bad hombre!
433+
bad_hombre_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |> lowercase
434+
location = findfirst(bad_hombre_string, lowercase(image_text))
435+
if !isnothing(location)
436+
image_text = replace(image_text, image_text[location] => "")
437+
end
438+
println(io, image_text)
439+
end
440+
441+
function render_mime(io::IO, mime::MIME"image/png", node, element, page, doc; kwargs...)
387442
filename = String(rand('a':'z', 7))
388-
if haskey(d, MIME"text/markdown"())
389-
println(io, d[MIME"text/markdown"()])
390-
elseif haskey(d, MIME"text/html"())
391-
println(io, d[MIME"text/html"()])
392-
elseif haskey(d, MIME"image/svg+xml"())
393-
# NOTE: It seems that we can't simply save the SVG images as a file and include them
394-
# as browsers seem to need to have the xmlns attribute set in the <svg> tag if you
395-
# want to include it with <img>. However, setting that attribute is up to the code
396-
# creating the SVG image.
397-
image_text = d[MIME"image/svg+xml"()]
398-
# Additionally, Vitepress complains about the XML version and encoding string below,
399-
# so we just remove this bad hombre!
400-
bad_hombre_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |> lowercase
401-
location = findfirst(bad_hombre_string, lowercase(image_text))
402-
if !isnothing(location)
403-
image_text = replace(image_text, image_text[location] => "")
404-
end
405-
println(io, image_text)
406-
elseif haskey(d, MIME"image/png"())
407-
write(joinpath(dirname(page.build), "$(filename).png"),
408-
base64decode(d[MIME"image/png"()]))
409-
println(io,
410-
"""
411-
![]($(filename).png)
412-
""")
413-
elseif haskey(d, MIME"image/webp"())
414-
write(joinpath(dirname(page.build), "$(filename).webp"),
415-
base64decode(d[MIME"image/webp"()]))
416-
println(io,
417-
"""
418-
![]($(filename).webp)
419-
""")
420-
elseif haskey(d, MIME"image/jpeg"())
421-
write(joinpath(dirname(page.build), "$(filename).jpeg"),
422-
base64decode(d[MIME"image/jpeg"()]))
423-
println(io,
424-
"""
425-
![]($(filename).jpeg)
426-
""")
427-
elseif haskey(d, MIME"image/gif"())
428-
write(joinpath(dirname(page.build), "$(filename).gif"),
429-
base64decode(d[MIME"image/gif"()]))
430-
println(io,
431-
"""
432-
![]($(filename).gif)
433-
""")
434-
elseif haskey(d, MIME"video/mp4"())
435-
write(joinpath(dirname(page.build), "$(filename).gif"),
436-
base64decode(d[MIME"image/gif"()]))
437-
println(io,
438-
"""
439-
<video src="$filename.mp4" controls="controls" autoplay="autoplay"></video>)
440-
""")
441-
elseif haskey(d, MIME"text/plain"())
442-
text = d[MIME"text/plain"()]
443-
out = repr(MIME"text/plain"(), ANSIColoredPrinters.PlainTextPrinter(IOBuffer(text)))
444-
render(io, mime, node, Markdown.Code(out), page, doc; kwargs...)
445-
else
446-
error("this should never happen.")
443+
write(joinpath(dirname(page.build), md_output_path, "$(filename).png"),
444+
base64decode(element))
445+
println(io, "![]($(filename).png)")
446+
end
447+
448+
function render_mime(io::IO, mime::MIME"image/webp", node, element, page, doc; kwargs...)
449+
filename = String(rand('a':'z', 7))
450+
write(joinpath(dirname(page.build), md_output_path, "$(filename).webp"),
451+
base64decode(element))
452+
println(io, "![]($(filename).webp)")
453+
end
454+
455+
function render_mime(io::IO, mime::MIME"image/jpeg", node, element, page, doc; kwargs...)
456+
filename = String(rand('a':'z', 7))
457+
write(joinpath(dirname(page.build), md_output_path, "$(filename).jpeg"),
458+
base64decode(element))
459+
println(io, "![]($(filename).jpeg)")
460+
end
461+
462+
function render_mime(io::IO, mime::MIME"image/png+lightdark", node, element, page, doc; kwargs...)
463+
fig_light, fig_dark, backend = element
464+
filename = String(rand('a':'z', 7))
465+
write(joinpath(dirname(page.build), md_output_path, "$(filename)_light.png"), fig_light)
466+
write(joinpath(dirname(page.build), md_output_path, "$(filename)_dark.png"), fig_dark)
467+
println(io,
468+
"""
469+
![]($(filename)_light.png){.light-only}
470+
![]($(filename)_dark.png){.dark-only}
471+
"""
472+
)
473+
end
474+
475+
function render_mime(io::IO, mime::MIME"image/jpeg+lightdark", node, element, page, doc; kwargs...)
476+
fig_light, fig_dark, backend = element
477+
filename = String(rand('a':'z', 7))
478+
Main.Makie.save(joinpath(dirname(page.build), md_output_path, "$(filename)_light.jpeg"), fig_light)
479+
Main.Makie.save(joinpath(dirname(page.build), md_output_path, "$(filename)_dark.jpeg"), fig_dark)
480+
println(io,
481+
"""
482+
![]($(filename)_light.jpeg){.light-only}
483+
![]($(filename)_dark.jpeg){.dark-only}
484+
"""
485+
)
486+
end
487+
488+
function render_mime(io::IO, mime::MIME"image/svg+xml+lightdark", node, element, page, doc; kwargs...)
489+
fig_light, fig_dark, backend = element
490+
filename = String(rand('a':'z', 7))
491+
Main.Makie.save(joinpath(dirname(page.build), md_output_path, "$(filename)_light.svg"), fig_light)
492+
Main.Makie.save(joinpath(dirname(page.build), md_output_path, "$(filename)_dark.svg"), fig_dark)
493+
println(io,
494+
"""
495+
<img src = "$(filename)_light.svg" style=".light-only"></img>
496+
<img src = "$(filename)_dark.svg" style=".dark-only"></img>
497+
"""
498+
)
499+
end
500+
501+
function render_mime(io::IO, mime::MIME"image/gif", node, element, page, doc; kwargs...)
502+
filename = String(rand('a':'z', 7))
503+
write(joinpath(dirname(page.build), md_output_path, "$(filename).gif"),
504+
base64decode(element))
505+
println(io, "![]($(filename).gif)")
506+
end
507+
508+
function render_mime(io::IO, mime::MIME"video/mp4", node, element, page, doc; kwargs...)
509+
filename = String(rand('a':'z', 7))
510+
write(joinpath(dirname(page.build), md_output_path, "$(filename).mp4"),
511+
base64decode(element))
512+
println(io, "<video src='$filename.mp4' controls='controls' autoplay='autoplay'></video>")
513+
end
514+
515+
function render_mime(io::IO, mime::MIME"text/plain", node, element, page, doc; kwargs...)
516+
return render(io, mime, node, Markdown.Code(element), page, doc; kwargs...)
517+
end
518+
519+
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, d::Dict{MIME, Any}, page, doc; kwargs...)
520+
521+
settings_ind = findfirst(x -> x isa MarkdownVitepress, doc.user.format)
522+
settings = doc.user.format[settings_ind]
523+
md_output_path = settings.md_output_path
524+
525+
available_mimes = keys(d)
526+
if isempty(available_mimes)
527+
return nothing
447528
end
448-
return nothing
529+
# Sort the available mimes by priority
530+
sorted_mimes = sort(collect(available_mimes), by = mime_priority)
531+
# Select the best MIME type for rendering
532+
best_mime = sorted_mimes[1]
533+
# Render the best MIME type
534+
render_mime(io, best_mime, node, d[best_mime], page, doc; kwargs...)
449535
end
450536

451537
## Basic Nodes. AKA: any other content that hasn't been handled yet.
@@ -462,7 +548,7 @@ render(io::IO, ::MIME"text/plain", node::Documenter.MarkdownAST.Node, str::Abstr
462548
# the rest of the build, and so we just leave them in place and print a blank line in their place.
463549
render(io::IO, ::MIME"text/plain", n::Documenter.MarkdownAST.Node, node::Documenter.MetaNode, page, doc; kwargs...) = println(io, "\n")
464550
# In the original AST, SetupNodes were just mapped to empty Markdown.MD() objects.
465-
render(io, mime, node::MarkdownAST.Node, ::Documenter.SetupNode, page, doc; kwargs...) = nothing
551+
render(io::IO, mime::MIME"text/plain", node::MarkdownAST.Node, ::Documenter.SetupNode, page, doc; kwargs...) = nothing
466552

467553

468554
# Raw nodes are used to insert raw HTML into the output. We just print it as is.
@@ -683,4 +769,12 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod
683769
print(io, "[")
684770
render(io, mime, node, node.children, page, doc; kwargs...)
685771
print(io, "]($(replace(path, " " => "%20")))")
772+
end
773+
774+
# Documenter.jl local images
775+
function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, image::Documenter.LocalImage, page, doc; kwargs...)
776+
# Main.@infiltrate
777+
image_path = relpath(joinpath(doc.user.build, image.path), dirname(page.build))
778+
println(io)
779+
println(io, "![]($image_path)")
686780
end

0 commit comments

Comments
 (0)