Skip to content

Commit b4a03b7

Browse files
committed
feat: allow inline scripts in custom_scripts
1 parent 23937df commit b4a03b7

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/MultiDocumenter.jl

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`
9595
- `brand_image` is a `BrandImage(path, imgpath)`, which is rendered as the leftmost
9696
item in the global navigation
9797
- `custom_stylesheets` is a `Vector{String}` of relative stylesheet URLs injected into each page.
98-
- `custom_scripts` is a `Vector{String}` of relative script URLs injected into each page.
98+
- `custom_scripts` is a `Vector{Union{String, Docs.HTML}}`. Strings can be relative or absolute URLs, while
99+
`Docs.HTML` objects are inserted as the content of inline scripts.
99100
- `search_engine` inserts a global search bar if not `false`. See [`SearchConfig`](@ref) for more details.
100101
- `prettyurls` removes all `index.html` suffixes from links in the global navigation.
101102
"""
@@ -253,19 +254,34 @@ function make_global_scripts(custom_scripts, path)
253254
out = []
254255

255256
for script in custom_scripts
256-
script = startswith(script, r"https?://") ?
257-
script :
258-
replace(joinpath(path, script), raw"\\" => "/")
259-
js = Gumbo.HTMLElement{:script}(
260-
[],
261-
Gumbo.NullNode(),
262-
Dict(
263-
"src" => script,
264-
"type" => "text/javascript",
265-
"charset" => "utf-8",
266-
),
267-
)
268-
push!(out, js)
257+
if script isa Docs.HTML
258+
js = Gumbo.HTMLElement{:script}(
259+
[],
260+
Gumbo.NullNode(),
261+
Dict(
262+
"type" => "text/javascript",
263+
"charset" => "utf-8",
264+
),
265+
)
266+
push!(js, Gumbo.HTMLText(js, script.content))
267+
push!(out, js)
268+
elseif script isa AbstractString
269+
script = startswith(script, r"https?://") ?
270+
script :
271+
replace(joinpath(path, script), raw"\\" => "/")
272+
js = Gumbo.HTMLElement{:script}(
273+
[],
274+
Gumbo.NullNode(),
275+
Dict(
276+
"src" => script,
277+
"type" => "text/javascript",
278+
"charset" => "utf-8",
279+
),
280+
)
281+
push!(out, js)
282+
else
283+
throw(ArgumentError("`custom_scripts` may only contain elements of type `AbstractString` or `Docs.HTML`."))
284+
end
269285
end
270286

271287
return out

test/runtests.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ using Test
7373
search_engine = MultiDocumenter.SearchConfig(
7474
index_versions = ["stable", "dev"],
7575
engine = MultiDocumenter.FlexSearch
76-
)
76+
),
77+
custom_scripts = [
78+
"foo/bar.js",
79+
"https://foo.com/bar.js",
80+
Docs.HTML("const foo = 'bar';")
81+
]
7782
)
7883

79-
@testset "flexsearch" begin
84+
@testset "structure" begin
8085
@test isdir(outpath, "inf")
8186
@test isdir(outpath, "inf", "stable")
8287
@test isfile(outpath, "inf", "stable", "index.html")
@@ -85,7 +90,18 @@ using Test
8590
<!--This file is automatically generated by Documenter.jl-->
8691
<meta http-equiv="refresh" content="0; url=./stable/"/>
8792
"""
93+
end
94+
8895

96+
@testset "custom scripts" begin
97+
index = read(joinpath(outpath, "inf", "stable", "index.html"), String)
98+
99+
@test occursin("""<script charset="utf-8" src="../../foo/bar.js" type="text/javascript"></script>""", index)
100+
@test occursin("""<script charset="utf-8" src="https://foo.com/bar.js" type="text/javascript"></script>""", index)
101+
@test occursin("""<script charset="utf-8" type="text/javascript">const foo = 'bar';</script>""", index)
102+
end
103+
104+
@testset "flexsearch" begin
89105
@test isdir(outpath, "search-data")
90106
store_content = read(joinpath(outpath, "search-data", "store.json"), String)
91107
@test !isempty(store_content)

0 commit comments

Comments
 (0)