Skip to content

Commit

Permalink
feat: improve compat with non-documenter pages
Browse files Browse the repository at this point in the history
- sticky header
- parse all `article`s for search index
- require Gumbo version that correctly handles relevant WS
- rely less on Documenter CSS being loaded
- improve flexsearch index loading logic
- inject custom JS into DOM instead of Documenter script
  • Loading branch information
pfitzseb committed Oct 24, 2022
1 parent 482a9d9 commit b867511
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[compat]
AbstractTrees = "0.4"
Gumbo = "0.8"
Gumbo = "0.8.2"
HypertextLiteral = "0.9"
JSON = "0.20,0.21"
NodeJS = "1"
Expand Down
7 changes: 6 additions & 1 deletion assets/default/flexsearch.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
border-radius: 3.2px;
color: #999;
background-color: unset;
height: 1.8rem;
height: 28px;
font-family: Glober;
width: 20em;
font-size: 14px;
Expand Down Expand Up @@ -92,6 +92,11 @@
display: block;
overflow: hidden;
text-overflow: ellipsis;
color: black;
}

.theme--documenter-dark #multi-page-nav .suggestion a {
color: white;
}

#multi-page-nav .suggestion a:hover,
Expand Down
49 changes: 29 additions & 20 deletions assets/default/flexsearch_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,39 @@
},
});

let importDone = null
let successfullyLoadedIndex = null

function loadIndex(flexsearchIdx) {
const input = document.getElementById('search-input')
input.setAttribute('placeholder', 'Loading...')
importDone = false
successfullyLoadedIndex = false
const keys = ['content.cfg', 'content.ctx', 'content.map', 'reg', 'store']
for (const key of keys) {
fetch('/search-data/' + key + '.json').then(r => {
if (r.ok) {
r.json().then(idx => {
flexsearchIdx.import(key, idx)
if (key === keys[keys.length - 1]) {
setTimeout(() => {
importDone = true
input.setAttribute('placeholder', 'Search...')
}, 100)
}
})
} else {
input.setAttribute('placeholder', 'Error loading search index.')
}
const promises = keys.map(key => {
return new Promise((resolve, reject) => {
fetch('/search-data/' + key + '.json').then(r => {
if (r && r.ok) {
r.json().then(idx => {
flexsearchIdx.import(key, idx)
resolve()
}).catch(() => {
reject()
})
} else {
reject()
}
}).catch(() => {
reject()
})
})
}
})

Promise.all(promises).then(() => {
input.setAttribute('placeholder', 'Search...')
successfullyLoadedIndex = true
}).catch(() => {
input.setAttribute('placeholder', 'Error loading search data...')
successfullyLoadedIndex = false
})
}

function registerSearchListener() {
Expand All @@ -46,9 +55,9 @@
let lastQuery = ''

function runSearch() {
if (importDone === null) {
if (successfullyLoadedIndex === null) {
loadIndex(flexsearchIdx)
} else if (importDone === false) {
} else if (successfullyLoadedIndex === false) {
return
}
const query = input.value
Expand Down
36 changes: 18 additions & 18 deletions assets/default/multidoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ html {
height: var(--navbar-height);
z-index: 10;
padding: 0 1rem;
position: fixed;
position: sticky;
display: flex;
top: 0;
background-color: #282f2f;
Expand All @@ -25,15 +25,19 @@ html {
display: none;
margin-left: auto;
font-size: 24px;
color: white;
margin-left: auto;
line-height: var(--navbar-height);
height: var(--navbar-height);
padding: 0;
fill: white;
cursor: pointer;
border: none;
background: none;
padding-inline: none;
}

#multi-page-nav #multidoc-toggler:before {
content: "\f0c9";
font-family: "Font Awesome 5 Free";
#multi-page-nav #multidoc-toggler > svg {
line-height: unset;
width: 24px;
height: 24px;
}

#multi-page-nav>* {
Expand Down Expand Up @@ -67,6 +71,7 @@ html {
border: none;
text-align: left;
background: none;
padding: 0 12px;
}

#multi-page-nav .nav-expanded .dropdown-label {
Expand Down Expand Up @@ -132,6 +137,9 @@ html {

#multi-page-nav .nav-mega-dropdown-container .column-header {
color: #fff;
font-size: 16px;
padding: 5px;
margin: 0;
text-transform: uppercase;
}

Expand Down Expand Up @@ -165,12 +173,11 @@ html {
font-weight: bold;
}

/* Documenter css tweaks */

#documenter .docs-main header.docs-navbar {
top: var(--navbar-height);
#multi-page-nav .column-content {
padding: 0;
}

/* Documenter css tweaks */
.docs-sidebar {
padding-top: calc(var(--navbar-height) + 1rem) !important;
}
Expand All @@ -179,13 +186,6 @@ html {
top: 0;
}

@media screen and (min-width: 1056px) {
#documenter {
margin-top: calc(var(--navbar-height)) !important;
}
}

/* headroom.js for the multi-page navbar */
@media screen and (max-width: 1055px) {
#multi-page-nav {
position: sticky;
Expand Down
30 changes: 30 additions & 0 deletions assets/default/multidoc_injector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function multidocInjector() {
document
.getElementById("multidoc-toggler")
.addEventListener("click", function () {
document.getElementById("nav-items").classList.toggle("hidden-on-mobile");
});
document.body.addEventListener("click", function (ev) {
if (!ev.target.matches(".nav-dropdown-container")) {
Array.prototype.forEach.call(
document.getElementsByClassName("dropdown-label"),
function (el) {
el.parentElement.classList.remove("nav-expanded");
}
);
}
if (ev.target.matches(".dropdown-label")) {
ev.target.parentElement.classList.add("nav-expanded");
}
});
}

if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
// call on next available tick
setTimeout(multidocInjector, 1);
} else {
document.addEventListener("DOMContentLoaded", multidocInjector);
}
29 changes: 0 additions & 29 deletions assets/multidoc_injector.js

This file was deleted.

21 changes: 10 additions & 11 deletions src/MultiDocumenter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`
- `assets_dir` is copied into `outdir/assets`
- `brand_image` is a `BrandImage(path, imgpath)`, which is rendered as the leftmost
item in the global navigation
- `custom_stylesheets` is a `Vector{String}` of stylesheets injected into each page.
- `custom_scripts` is a `Vector{String}` of scripts injected into each page.
- `custom_stylesheets` is a `Vector{String}` of relative stylesheet URLs injected into each page.
- `custom_scripts` is a `Vector{String}` of relative script URLs injected into each page.
- `search_engine` inserts a global search bar if not `false`. See [`SearchConfig`](@ref) for more details.
- `prettyurls` removes all `index.html` suffixes from links in the global navigation.
"""
Expand Down Expand Up @@ -216,7 +216,11 @@ function make_global_nav(
$([render(doc, dir, thispagepath, prettyurls) for doc in docs])
$(search_engine.engine.render())
</div>
<a id="multidoc-toggler"></a>
<button id="multidoc-toggler">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"></path>
</svg>
</button>
</nav>
"""

Expand All @@ -233,7 +237,7 @@ function make_global_stylesheet(custom_stylesheets, path)
Dict(
"rel" => "stylesheet",
"type" => "text/css",
"href" => joinpath(path, stylesheet),
"href" => string(path, "/", stylesheet),
),
)
push!(out, style)
Expand All @@ -250,7 +254,7 @@ function make_global_scripts(custom_scripts, path)
[],
Gumbo.NullNode(),
Dict(
"src" => joinpath(path, script),
"src" => string(path, "/", script),
"type" => "text/javascript",
"charset" => "utf-8",
),
Expand Down Expand Up @@ -281,16 +285,11 @@ function inject_styles_and_global_navigation(
search_engine.engine.inject_styles!(custom_stylesheets)
end
pushfirst!(custom_stylesheets, joinpath("assets", "default", "multidoc.css"))
pushfirst!(custom_scripts, joinpath("assets", "default", "multidoc_injector.js"))

@sync for (root, _, files) in walkdir(dir)
for file in files
path = joinpath(root, file)
if file == "documenter.js"
open(path, "a") do io
println(io, js_injector())
end
continue
end

endswith(file, ".html") || continue

Expand Down
2 changes: 1 addition & 1 deletion src/renderers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
function render(doc::Column, dir, thispagepath, prettyurls)
return @htl """
<div class="nav-mega-column">
<h5 class="column-header">$(doc.name)</h5>
<div class="column-header">$(doc.name)</div>
<ul class="column-content">
$([render(doc, dir, thispagepath, prettyurls) for doc in doc.children])
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/search/flexsearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function add_to_index!(index, ref, file)
title = Gumbo.text(el)
doc.title = first(split(title, " · "))
end
if Gumbo.tag(el) == :article && Gumbo.getattr(el, "class", "") == "content"
if Gumbo.tag(el) == :article
add_fragment(doc, el)
break
end
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ using Test
name = "JuliaInterpreter",
giturl = "https://github.com/JuliaDebug/JuliaInterpreter.jl.git",
),
MultiDocumenter.MultiDocRef(
upstream = joinpath(clonedir, "Lux"),
path = "lux",
name = "Lux",
giturl = "https://github.com/avik-pal/Lux.jl",
),
]),
MultiDocumenter.Column("Column 2", [
MultiDocumenter.MultiDocRef(
Expand Down

0 comments on commit b867511

Please sign in to comment.