Skip to content

Commit 98d6b9d

Browse files
committed
feat: option to fix canonical URLs of a multidocumenter build
1 parent 5da2ba5 commit 98d6b9d

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.6.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
8+
DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8"
89
Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2"
910
Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
1011
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"

src/MultiDocumenter.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module MultiDocumenter
22

3+
import DocumenterTools
34
import Gumbo, AbstractTrees
45
using HypertextLiteral
56
import Git: git
@@ -25,13 +26,15 @@ struct MultiDocRef
2526
path::String
2627
name::String
2728

29+
fix_canonical_url::Bool
30+
2831
# these are not actually used internally
2932
giturl::String
3033
branch::String
3134
end
3235

33-
function MultiDocRef(; upstream, name, path, giturl = "", branch = "gh-pages")
34-
MultiDocRef(upstream, path, name, giturl, branch)
36+
function MultiDocRef(; upstream, name, path, giturl = "", branch = "gh-pages", fix_canonical_url=true)
37+
MultiDocRef(upstream, path, name, fix_canonical_url, giturl, branch)
3538
end
3639

3740
struct DropdownNav
@@ -76,6 +79,7 @@ end
7679
include("renderers.jl")
7780
include("search/flexsearch.jl")
7881
include("search/stork.jl")
82+
include("canonical.jl")
7983

8084
const DEFAULT_ENGINE = SearchConfig(index_versions = ["stable", "dev"], engine = FlexSearch)
8185

@@ -91,6 +95,7 @@ const DEFAULT_ENGINE = SearchConfig(index_versions = ["stable", "dev"], engine =
9195
prettyurls = true,
9296
rootpath = "/",
9397
hide_previews = true,
98+
canonical = nothing,
9499
)
95100
96101
Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`.
@@ -105,6 +110,9 @@ Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`
105110
- `prettyurls` removes all `index.html` suffixes from links in the global navigation.
106111
- `rootpath` is the path your site ends up being deployed at, e.g. `/foo/` if it's hosted at `https://bar.com/foo`
107112
- `hide_previews` removes preview builds from the aggregated documentation.
113+
- `canonical`: if set to the root URL of the MultiDocumenter site, will check and, if necessary, update the
114+
canonical URL tags for each package site to point to the directory. Similar to the `canonical` argument of
115+
`Documenter.HTML` constructor.
108116
"""
109117
function make(
110118
outdir,
@@ -117,10 +125,12 @@ function make(
117125
prettyurls = true,
118126
rootpath = "/",
119127
hide_previews = true,
128+
canonical::Union{AbstractString, Nothing} = nothing,
120129
)
121130
maybe_clone(flatten_multidocrefs(docs))
122131

123-
dir = make_output_structure(flatten_multidocrefs(docs), prettyurls, hide_previews)
132+
canonical = rstrip(canonical, '/')
133+
dir = make_output_structure(flatten_multidocrefs(docs), prettyurls, hide_previews; canonical)
124134
out_assets = joinpath(dir, "assets")
125135
if assets_dir !== nothing && isdir(assets_dir)
126136
cp(assets_dir, out_assets)
@@ -192,7 +202,10 @@ function maybe_clone(docs::Vector{MultiDocRef})
192202
end
193203
end
194204

195-
function make_output_structure(docs::Vector{MultiDocRef}, prettyurls, hide_previews)
205+
function make_output_structure(
206+
docs::Vector{MultiDocRef}, prettyurls, hide_previews;
207+
canonical::Union{AbstractString, Nothing}
208+
)
196209
dir = mktempdir()
197210

198211
for doc in docs
@@ -210,6 +223,8 @@ function make_output_structure(docs::Vector{MultiDocRef}, prettyurls, hide_previ
210223
if hide_previews && isdir(previewpath)
211224
rm(previewpath, recursive = true)
212225
end
226+
227+
fix_canonical_url!(doc; canonical, root_dir=dir)
213228
end
214229

215230
open(joinpath(dir, "index.html"), "w") do io

src/canonical.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This files contains the functions used to implement the canonical URL
2+
# update functionality.
3+
function fix_canonical_url!(
4+
doc::MultiDocRef; canonical::Union{AbstractString, Nothing}, root_dir::AbstractString
5+
)
6+
# If the user didn't set `canonical`, then we don't need to do anything
7+
isnothing(canonical) && return nothing
8+
# The user can also disable the canonical URL fixing on a per-package basis
9+
doc.fix_canonical_url || return nothing
10+
# Determine the canonical URL and fix them in the HTML files
11+
documenter_directory_root = joinpath(root_dir, doc.path)
12+
try
13+
DocumenterTools.update_canonical_links(
14+
documenter_directory_root;
15+
canonical = join((canonical, doc.path), '/')
16+
)
17+
catch e
18+
@error "Unable to update canonical URLs for this package" doc exception = (e, catch_backtrace())
19+
end
20+
end

0 commit comments

Comments
 (0)