@@ -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
383387end
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-  .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-  .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-  .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-  .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, "  .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, "  .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, "  .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+  _light.png){.light-only}
470+  _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+  _light.jpeg){.light-only}
483+  _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, "  .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... )
449535end
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.
463549render (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, " " )
686780end
0 commit comments