Skip to content

Commit

Permalink
use JSON instead of JS to store the search index
Browse files Browse the repository at this point in the history
and put it inside the search directory
  • Loading branch information
visr committed Feb 25, 2019
1 parent 301edc3 commit aceb999
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 73 deletions.
137 changes: 70 additions & 67 deletions assets/html/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ require(["jquery", "lunr", "lodash"], function($, lunr, _) {
'yet',
'you',
'your'
])
])

// add . as a separator, because otherwise "title": "Documenter.Anchors.add!"
// would not find anything if searching for "add!", only for the entire qualification
Expand All @@ -177,74 +177,77 @@ require(["jquery", "lunr", "lodash"], function($, lunr, _) {
lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'juliaStopWordFilter')
lunr.Pipeline.registerFunction(lunr.trimmer, 'juliaTrimmer')

var index = lunr(function () {
this.ref('location')
this.field('title')
this.field('text')
documenterSearchIndex['docs'].forEach(function(e) {
this.add(e)
}, this)
})
var store = {}
$.getJSON("search_index.json", function (documenterSearchIndex) {

documenterSearchIndex['docs'].forEach(function(e) {
store[e.location] = {title: e.title, category: e.category}
})
var index = lunr(function () {
this.ref('location')
this.field('title')
this.field('text')
documenterSearchIndex.forEach(function (e) {
this.add(e)
}, this)
})
var store = {}

$(function(){
function update_search(querystring) {
tokens = lunr.tokenizer(querystring)
results = index.query(function (q) {
tokens.forEach(function (t) {
q.term(t.toString(), {
fields: ["title"],
boost: 100,
usePipeline: false,
editDistance: 0,
wildcard: lunr.Query.wildcard.NONE
})
q.term(t.toString(), {
fields: ["title"],
boost: 10,
usePipeline: false,
editDistance: 2,
wildcard: lunr.Query.wildcard.NONE
})
q.term(t.toString(), {
fields: ["text"],
boost: 1,
usePipeline: true,
editDistance: 0,
wildcard: lunr.Query.wildcard.NONE
documenterSearchIndex.forEach(function (e) {
store[e.location] = { title: e.title, category: e.category }
})

$(function () {
function update_search(querystring) {
tokens = lunr.tokenizer(querystring)
results = index.query(function (q) {
tokens.forEach(function (t) {
q.term(t.toString(), {
fields: ["title"],
boost: 100,
usePipeline: false,
editDistance: 0,
wildcard: lunr.Query.wildcard.NONE
})
q.term(t.toString(), {
fields: ["title"],
boost: 10,
usePipeline: false,
editDistance: 2,
wildcard: lunr.Query.wildcard.NONE
})
q.term(t.toString(), {
fields: ["text"],
boost: 1,
usePipeline: true,
editDistance: 0,
wildcard: lunr.Query.wildcard.NONE
})
})
})
})
$('#search-info').text("Number of results: " + results.length)
$('#search-results').empty()
results.forEach(function(result) {
data = store[result.ref]
link = $('<a>')
link.text(data.title)
link.attr('href', documenterBaseURL+'/'+result.ref)
cat = $('<span class="category">('+data.category+')</span>')
li = $('<li>').append(link).append(" ").append(cat)
$('#search-results').append(li)
})
}

function update_search_box() {
querystring = $('#search-query').val()
update_search(querystring)
}

$('#search-query').keyup(_.debounce(update_search_box, 250))
$('#search-query').change(update_search_box)

search_query_uri = parseUri(window.location).queryKey["q"]
if(search_query_uri !== undefined) {
search_query = decodeURIComponent(search_query_uri.replace(/\+/g, '%20'))
$("#search-query").val(search_query)
}
update_search_box();
})
$('#search-info').text("Number of results: " + results.length)
$('#search-results').empty()
results.forEach(function (result) {
data = store[result.ref]
link = $('<a>')
link.text(data.title)
link.attr('href', documenterBaseURL + '/' + result.ref)
cat = $('<span class="category">(' + data.category + ')</span>')
li = $('<li>').append(link).append(" ").append(cat)
$('#search-results').append(li)
})
}

function update_search_box() {
querystring = $('#search-query').val()
update_search(querystring)
}

$('#search-query').keyup(_.debounce(update_search_box, 250))
$('#search-query').change(update_search_box)

search_query_uri = parseUri(window.location).queryKey["q"]
if (search_query_uri !== undefined) {
search_query = decodeURIComponent(search_query_uri.replace(/\+/g, '%20'))
$("#search-query").val(search_query)
}
update_search_box();
})
});
})
10 changes: 4 additions & 6 deletions src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mutable struct HTMLContext
documenter_js :: String
search_js :: String
search_index :: Vector{SearchRecord}
search_index_js :: String
search_index_json :: String
search_navnode :: Documents.NavNode
local_assets :: Vector{String}
end
Expand Down Expand Up @@ -231,7 +231,7 @@ function render(doc::Documents.Document, settings::HTML=HTML())
!isempty(doc.user.sitename) || error("HTML output requires `sitename`.")

ctx = HTMLContext(doc, settings)
ctx.search_index_js = "search_index.js"
ctx.search_index_json = "search/search_index.json"

copy_asset("arrow.svg", doc)

Expand All @@ -255,10 +255,8 @@ function render(doc::Documents.Document, settings::HTML=HTML())

render_search(ctx)

open(joinpath(doc.user.build, ctx.search_index_js), "w") do io
println(io, "var documenterSearchIndex = {\"docs\":")
open(joinpath(doc.user.build, ctx.search_index_json), "w") do io
JSON.print(io, ctx.search_index)
println(io, "\n}")
end
end

Expand Down Expand Up @@ -419,7 +417,7 @@ function render_search(ctx)
html[:lang=>"en"](
head,
body(navmenu, article),
script[:src => relhref(src, ctx.search_index_js)],
script[:src => relhref(src, ctx.search_index_json)],
script[:src => relhref(src, ctx.search_js)],
)
)
Expand Down

0 comments on commit aceb999

Please sign in to comment.