Skip to content

Commit 076755d

Browse files
authored
Merge pull request #48 from JuliaComputing/sp/rootpath-
feature: add rootpath arg
2 parents 5f5d88f + 83d2000 commit 076755d

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

assets/default/flexsearch_integration.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
input.setAttribute('placeholder', 'Loading...')
2121
successfullyLoadedIndex = false
2222
const keys = ['content.cfg', 'content.ctx', 'content.map', 'reg', 'store']
23+
const rootPath = window.MULTIDOCUMENTER_ROOT_PATH ?? '/'
2324
const promises = keys.map(key => {
2425
return new Promise((resolve, reject) => {
25-
fetch('/search-data/' + key + '.json').then(r => {
26+
fetch(`${rootPath}search-data/${key}.json`).then(r => {
2627
if (r && r.ok) {
2728
r.json().then(idx => {
2829
flexsearchIdx.import(key, idx)

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ MultiDocumenter.make(
6565
search_engine = MultiDocumenter.SearchConfig(
6666
index_versions = ["stable"],
6767
engine = MultiDocumenter.FlexSearch
68-
)
68+
),
69+
rootpath = "/MultiDocumenter.jl/",
6970
)
7071

7172
gitroot = normpath(joinpath(@__DIR__, ".."))

src/MultiDocumenter.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ function walk_outputs(f, root, docs::Vector{MultiDocRef}, dirs::Vector{String})
6363
for file in files
6464
file == "index.html" || continue
6565

66-
f(chop(r, head = length(root), tail = 0), joinpath(r, file))
66+
# +1 for path separator
67+
f(chop(r, head = length(root) + 1, tail = 0), joinpath(r, file))
6768
end
6869
end
6970
break
@@ -86,7 +87,8 @@ const DEFAULT_ENGINE = SearchConfig(index_versions = ["stable", "dev"], engine =
8687
custom_stylesheets = [],
8788
custom_scripts = [],
8889
search_engine = SearchConfig(),
89-
prettyurls = true
90+
prettyurls = true,
91+
rootpath = "/"
9092
)
9193
9294
Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`.
@@ -99,6 +101,7 @@ Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`
99101
`Docs.HTML` objects are inserted as the content of inline scripts.
100102
- `search_engine` inserts a global search bar if not `false`. See [`SearchConfig`](@ref) for more details.
101103
- `prettyurls` removes all `index.html` suffixes from links in the global navigation.
104+
-`rootpath` is the path your site ends up being deployed at, e.g. `/foo/` if it's hosted at `https://bar.com/foo`
102105
"""
103106
function make(
104107
outdir,
@@ -109,6 +112,7 @@ function make(
109112
custom_scripts = [],
110113
search_engine = DEFAULT_ENGINE,
111114
prettyurls = true,
115+
rootpath = "/",
112116
)
113117
maybe_clone(flatten_multidocrefs(docs))
114118

@@ -135,10 +139,11 @@ function make(
135139
custom_scripts,
136140
search_engine,
137141
prettyurls,
142+
rootpath,
138143
)
139144

140145
if search_engine != false
141-
search_engine.engine.build_search_index(dir, flatten_multidocrefs(docs), search_engine)
146+
search_engine.engine.build_search_index(dir, flatten_multidocrefs(docs), search_engine, rootpath)
142147
end
143148

144149
cp(dir, outdir; force = true)
@@ -300,10 +305,11 @@ function inject_styles_and_global_navigation(
300305
custom_scripts,
301306
search_engine,
302307
prettyurls,
308+
rootpath,
303309
)
304310

305311
if search_engine != false
306-
search_engine.engine.inject_script!(custom_scripts)
312+
search_engine.engine.inject_script!(custom_scripts, rootpath)
307313
search_engine.engine.inject_styles!(custom_stylesheets)
308314
end
309315
pushfirst!(custom_stylesheets, joinpath("assets", "default", "multidoc.css"))

src/search/flexsearch.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,20 @@ function add_to_index!(index, ref, file)
8484
push!(index.documents, doc)
8585
end
8686

87-
function generate_index(root, docs, config)
87+
function generate_index(root, docs, config, rootpath)
8888
search_index = SearchIndex()
8989
walk_outputs(root, docs, config.index_versions) do path, file
90-
add_to_index!(search_index, path, file)
90+
ref = replace(joinpath(rootpath, path), raw"\\" => "/")
91+
add_to_index!(search_index, ref, file)
9192
end
9293

9394
return search_index
9495
end
9596

96-
function inject_script!(custom_scripts)
97+
function inject_script!(custom_scripts, rootpath)
9798
pushfirst!(custom_scripts, joinpath("assets", "default", "flexsearch_integration.js"))
9899
pushfirst!(custom_scripts, joinpath("assets", "default", "flexsearch.bundle.js"))
100+
pushfirst!(custom_scripts, Docs.HTML("window.MULTIDOCUMENTER_ROOT_PATH = '$(rootpath)'"))
99101
end
100102

101103
function inject_styles!(custom_styles)
@@ -139,9 +141,9 @@ function to_json_index(index::SearchIndex, file)
139141
end
140142
end
141143

142-
function build_search_index(root, docs, config)
144+
function build_search_index(root, docs, config, rootpath)
143145
ID[] = 0
144-
idx = generate_index(root, docs, config)
146+
idx = generate_index(root, docs, config, rootpath)
145147
to_json_index(idx, joinpath(root, "index.json"))
146148
file = config.lowfi ? "gensearch-lowfi.js" : "gensearch.js"
147149
println("Writing $(config.lowfi ? "lowfi" : "") flexsearch index:")

src/search/stork.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function has_stork()
1414
return has_stork
1515
end
1616

17-
function build_search_index(root, docs, config)
17+
function build_search_index(root, docs, config, _)
1818
config = make_stork_config(root, docs, config)
1919
config_path = joinpath(root, "stork.config.toml")
2020
index_path = joinpath(root, "stork.st")
@@ -61,7 +61,7 @@ function add_to_index!(files, ref, path)
6161
return
6262
end
6363

64-
function inject_script!(custom_scripts)
64+
function inject_script!(custom_scripts, _)
6565
pushfirst!(custom_scripts, joinpath("assets", "default", "stork.js"))
6666
pushfirst!(custom_scripts, joinpath("assets", "default", "stork_integration.js"))
6767
end

test/runtests.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ using Test
6767

6868
outpath = joinpath(@__DIR__, "out")
6969

70+
rootpath = "/MultiDocumenter.jl/"
71+
7072
MultiDocumenter.make(
7173
outpath,
7274
docs;
@@ -78,7 +80,8 @@ using Test
7880
"foo/bar.js",
7981
"https://foo.com/bar.js",
8082
Docs.HTML("const foo = 'bar';")
81-
]
83+
],
84+
rootpath = rootpath,
8285
)
8386

8487
@testset "structure" begin
@@ -96,6 +99,7 @@ using Test
9699
@testset "custom scripts" begin
97100
index = read(joinpath(outpath, "inf", "stable", "index.html"), String)
98101

102+
@test occursin("""<script charset="utf-8" type="text/javascript">window.MULTIDOCUMENTER_ROOT_PATH = '$rootpath'</script>""", index)
99103
@test occursin("""<script charset="utf-8" src="../../foo/bar.js" type="text/javascript"></script>""", index)
100104
@test occursin("""<script charset="utf-8" src="https://foo.com/bar.js" type="text/javascript"></script>""", index)
101105
@test occursin("""<script charset="utf-8" type="text/javascript">const foo = 'bar';</script>""", index)
@@ -107,7 +111,8 @@ using Test
107111
@test !isempty(store_content)
108112
@test occursin("Infiltrator.jl", store_content)
109113
@test occursin("@infiltrate", store_content)
110-
@test occursin("/inf/stable/", store_content)
114+
@test occursin("$(rootpath)inf/stable/", store_content)
115+
@test occursin("$(rootpath)inf/stable/", store_content)
111116
@test !occursin("/inf/dev/", store_content)
112117
end
113118

0 commit comments

Comments
 (0)