Skip to content

Commit b978ffd

Browse files
authored
Merge pull request #27 from JuliaComputing/sp/lowfi
feat: add lowfi mode for flexsearch
2 parents 140f752 + b4a2ebb commit b978ffd

File tree

5 files changed

+95
-56
lines changed

5 files changed

+95
-56
lines changed

assets/default/flexsearch_integration.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
index: [
99
{
1010
field: 'content',
11-
tokenize: 'forward',
12-
minlength: 3,
13-
resolution: 9
1411
}
1512
]
1613
},
17-
encoder: 'simple',
18-
fastupdate: false,
19-
optimize: true,
20-
context: true,
2114
});
2215

2316
let importDone = null

flexsearch/gensearch-lowfi.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { Document } = require('flexsearch')
2+
const fs = require('fs')
3+
const process = require('process')
4+
const path = require('path')
5+
6+
var flexsearchIdx = new Document({
7+
document: {
8+
id: 'id',
9+
store: ['title', 'pagetitle', 'ref'],
10+
index: [
11+
{
12+
field: 'content',
13+
tokenize: 'forward',
14+
minlength: 3,
15+
resolution: 5
16+
}
17+
]
18+
},
19+
encoder: 'advanced',
20+
fastupdate: false,
21+
optimize: true,
22+
context: false,
23+
});
24+
25+
const idx = require(process.cwd() + '/index.json')
26+
27+
idx.forEach(doc => {
28+
flexsearchIdx.add(doc)
29+
})
30+
31+
fs.mkdirSync(path.join(process.cwd(), 'search-data'))
32+
flexsearchIdx.export((key, data) => {
33+
if (data) {
34+
const p = path.join(process.cwd(), 'search-data', key + '.json')
35+
try {
36+
fs.writeFileSync(p, data)
37+
console.log(' ' + key)
38+
} catch (err) {
39+
console.error(err)
40+
}
41+
}
42+
})

flexsearch/gensearch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var flexsearchIdx = new Document({
1212
field: 'content',
1313
tokenize: 'forward',
1414
minlength: 3,
15-
resolution: 9
15+
resolution: 5
1616
}
1717
]
1818
},
19-
encoder: 'simple',
19+
encoder: 'advanced',
2020
fastupdate: false,
2121
optimize: true,
22-
context: true,
22+
context: false,
2323
});
2424

2525
const idx = require(process.cwd() + '/index.json')

src/MultiDocumenter.jl

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import Gumbo, AbstractTrees
44
using HypertextLiteral
55

66
"""
7-
SearchConfig(index_versions = ["stable"], engine = MultiDocumenter.FlexSearch)
7+
SearchConfig(index_versions = ["stable"], engine = MultiDocumenter.FlexSearch, lowfi = false)
88
99
`index_versions` is a vector of relative paths used for generating the search index. Only
1010
the first matching path is considered.
1111
`engine` may be `MultiDocumenter.FlexSearch`, `MultiDocumenter.Stork`, or a module that conforms
1212
to the expected API (which is currently undocumented).
13+
`lowfi = true` will try to minimize search index size. Only relevant for flexsearch.
1314
"""
1415
Base.@kwdef mutable struct SearchConfig
1516
index_versions = ["stable", "dev"]
1617
engine = FlexSearch
18+
lowfi = false
1719
end
1820

1921
struct MultiDocRef
@@ -280,7 +282,7 @@ function inject_styles_and_global_navigation(
280282
end
281283
pushfirst!(custom_stylesheets, joinpath("assets", "default", "multidoc.css"))
282284

283-
for (root, _, files) in walkdir(dir)
285+
@sync for (root, _, files) in walkdir(dir)
284286
for file in files
285287
path = joinpath(root, file)
286288
if file == "documenter.js"
@@ -294,60 +296,61 @@ function inject_styles_and_global_navigation(
294296

295297
islink(path) && continue
296298
isfile(path) || continue
297-
298-
stylesheets = make_global_stylesheet(custom_stylesheets, relpath(dir, root))
299-
scripts = make_global_scripts(custom_scripts, relpath(dir, root))
300-
301-
302299
page = read(path, String)
303300
if startswith(
304301
page,
305302
"<!--This file is automatically generated by Documenter.jl-->",
306303
)
307304
continue
308305
end
309-
doc = Gumbo.parsehtml(page)
310-
injected = 0
311-
312-
for el in AbstractTrees.PreOrderDFS(doc.root)
313-
injected >= 2 && break
314306

315-
if el isa Gumbo.HTMLElement
316-
if Gumbo.tag(el) == :head
317-
for stylesheet in stylesheets
318-
stylesheet.parent = el
319-
push!(el.children, stylesheet)
307+
Threads.@spawn begin
308+
stylesheets = make_global_stylesheet(custom_stylesheets, relpath(dir, root))
309+
scripts = make_global_scripts(custom_scripts, relpath(dir, root))
310+
311+
doc = Gumbo.parsehtml(page)
312+
injected = 0
313+
314+
for el in AbstractTrees.PreOrderDFS(doc.root)
315+
injected >= 2 && break
316+
317+
if el isa Gumbo.HTMLElement
318+
if Gumbo.tag(el) == :head
319+
for stylesheet in stylesheets
320+
stylesheet.parent = el
321+
push!(el.children, stylesheet)
322+
end
323+
for script in scripts
324+
script.parent = el
325+
pushfirst!(el.children, script)
326+
end
327+
injected += 1
328+
elseif Gumbo.tag(el) == :body && !isempty(el.children)
329+
documenter_div = first(el.children)
330+
if documenter_div isa Gumbo.HTMLElement &&
331+
Gumbo.getattr(documenter_div, "id", "") == "documenter"
332+
@debug "Could not detect Documenter page layout in $path. This may be due to an old version of Documenter."
333+
end
334+
# inject global navigation as first element in body
335+
336+
global_nav = make_global_nav(
337+
dir,
338+
docs,
339+
root,
340+
brand_image,
341+
search_engine,
342+
prettyurls,
343+
)
344+
global_nav.parent = el
345+
pushfirst!(el.children, global_nav)
346+
injected += 1
320347
end
321-
for script in scripts
322-
script.parent = el
323-
pushfirst!(el.children, script)
324-
end
325-
injected += 1
326-
elseif Gumbo.tag(el) == :body && !isempty(el.children)
327-
documenter_div = first(el.children)
328-
if documenter_div isa Gumbo.HTMLElement &&
329-
Gumbo.getattr(documenter_div, "id", "") == "documenter"
330-
@debug "Could not detect Documenter page layout in $path. This may be due to an old version of Documenter."
331-
end
332-
# inject global navigation as first element in body
333-
334-
global_nav = make_global_nav(
335-
dir,
336-
docs,
337-
root,
338-
brand_image,
339-
search_engine,
340-
prettyurls,
341-
)
342-
global_nav.parent = el
343-
pushfirst!(el.children, global_nav)
344-
injected += 1
345348
end
346349
end
347-
end
348350

349-
open(path, "w") do io
350-
print(io, doc)
351+
open(path, "w") do io
352+
print(io, doc)
353+
end
351354
end
352355
end
353356
end

src/search/flexsearch.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ function build_search_index(root, docs, config)
143143
ID[] = 0
144144
idx = generate_index(root, docs, config)
145145
to_json_index(idx, joinpath(root, "index.json"))
146-
println("Writing flexsearch index:")
146+
file = config.lowfi ? "gensearch-lowfi.js" : "gensearch.js"
147+
println("Writing $(config.lowfi ? "lowfi" : "") flexsearch index:")
147148
cd(root) do
148149
run(
149-
`$(NodeJS.nodejs_cmd()) $(joinpath(@__DIR__, "..", "..", "flexsearch", "gensearch.js"))`,
150+
`$(NodeJS.nodejs_cmd()) $(joinpath(@__DIR__, "..", "..", "flexsearch", file))`,
150151
)
151152
end
152153
return nothing

0 commit comments

Comments
 (0)