Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change search_index to be a JSON file #2517

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions assets/html/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ update_search

/////// SEARCH WORKER ///////

function worker_function(documenterSearchIndex, documenterBaseURL, filters) {
function worker_function(Index, documenterBaseURL, filters) {
importScripts(
"https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/index.min.js"
);

let data = documenterSearchIndex.map((x, key) => {
let data = Index.map((x, key) => {
x["id"] = key; // minisearch requires a unique for each object
return x;
});
Expand Down Expand Up @@ -360,13 +360,13 @@ function worker_function(documenterSearchIndex, documenterBaseURL, filters) {

// `worker = Threads.@spawn worker_function(documenterSearchIndex)`, but in JavaScript!
const filters = [
...new Set(documenterSearchIndex["docs"].map((x) => x.category)),
...new Set(documenterSearchIndex.map((x) => x.category)),
];
const worker_str =
"(" +
worker_function.toString() +
")(" +
JSON.stringify(documenterSearchIndex["docs"]) +
JSON.stringify(documenterSearchIndex) +
"," +
JSON.stringify(documenterBaseURL) +
"," +
Expand Down
2 changes: 1 addition & 1 deletion docs/src/man/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ build/
├── index.html
├── search
│   └── index.html
└── search_index.js
└── search_index.json
```

!!! note
Expand Down
18 changes: 14 additions & 4 deletions src/html/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ function render(doc::Documenter.Document, settings::HTML=HTML())
end

ctx = HTMLContext(doc, settings)
ctx.search_index_js = "search_index.js"
ctx.search_index_js = "search_index.json"
ctx.themeswap_js = copy_asset("themeswap.js", doc)
ctx.warner_js = copy_asset("warner.js", doc)

Expand Down Expand Up @@ -807,9 +807,8 @@ function render(doc::Documenter.Document, settings::HTML=HTML())
all(size_limit_successes) || throw(HTMLSizeThresholdError())

open(joinpath(doc.user.build, ctx.search_index_js), "w") do io
println(io, "var documenterSearchIndex = {\"docs\":")
# convert Vector{SearchRecord} to a JSON string + do additional JS escaping
println(io, JSDependencies.json_jsescape(ctx.search_index), "\n}")
println(io, JSDependencies.json_jsescape(ctx.search_index))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be double checked, but JSON might need a different escaping function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like json_jsescape only escapes some newline unicode, so it turns already valid JSON to also be valid javascript (also, according to comments in the source code, this is not required since 2019).

More closely looking at the function, it calls JSON.json(), and then escapes the problematic unicode in whatever is returned, so this should be fine (at least for this change).

end

write_inventory(doc, ctx)
Expand Down Expand Up @@ -959,6 +958,17 @@ function render_head(ctx, navnode)
RD.katex_css,
]

fetchstring = """
fetch("$(relhref(src, ctx.search_index_js))")
.then(result => result.json())
.then(json =>
{
documenterSearchIndex = json;
}
);
"""
minifiedfetchstring = replace(fetchstring, r"\s+" => "") #minified string

head(
meta[:charset=>"UTF-8"],
meta[:name => "viewport", :content => "width=device-width, initial-scale=1.0"],
Expand Down Expand Up @@ -994,7 +1004,7 @@ function render_head(ctx, navnode)
:src => RD.requirejs_cdn,
Symbol("data-main") => relhref(src, ctx.documenter_js)
],
script[:src => relhref(src, ctx.search_index_js)],
script(minifiedfetchstring), # loads search index into js variable documenterSearchIndex

script[:src => relhref(src, "siteinfo.js")],
script[:src => relhref(src, "../versions.js")],
Expand Down
Loading