From aceb999eef6864503d3457bb7c170e761d142853 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 25 Feb 2019 13:19:50 +0100 Subject: [PATCH] use JSON instead of JS to store the search index and put it inside the search directory --- assets/html/search.js | 137 +++++++++++++++++++------------------- src/Writers/HTMLWriter.jl | 10 ++- 2 files changed, 74 insertions(+), 73 deletions(-) diff --git a/assets/html/search.js b/assets/html/search.js index 5d32c3aac9e..03100467c1c 100644 --- a/assets/html/search.js +++ b/assets/html/search.js @@ -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 @@ -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 = $('') - link.text(data.title) - link.attr('href', documenterBaseURL+'/'+result.ref) - cat = $('('+data.category+')') - 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 = $('') + link.text(data.title) + link.attr('href', documenterBaseURL + '/' + result.ref) + cat = $('(' + data.category + ')') + 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(); + }) + }); }) diff --git a/src/Writers/HTMLWriter.jl b/src/Writers/HTMLWriter.jl index 30401703672..d7df040bc82 100644 --- a/src/Writers/HTMLWriter.jl +++ b/src/Writers/HTMLWriter.jl @@ -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 @@ -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) @@ -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 @@ -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)], ) )