Skip to content

Commit

Permalink
fix(doc): search links (#5725)
Browse files Browse the repository at this point in the history
Signed-off-by: richardlt <[email protected]>
  • Loading branch information
richardlt authored Feb 26, 2021
1 parent 9622b23 commit 2a0f93d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/layouts/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{{- if ne $page.Type "json" -}}
{{- if and $index (gt $index 0) -}},{{- end }}
{
"uri": "{{ $page.Permalink }}",
"uri": "{{ $page.RelPermalink }}",
"title": "{{ htmlEscape $page.Title}}",
"tags": [{{ range $tindex, $tag := $page.Params.tags }}{{ if $tindex }}, {{ end }}"{{ $tag| htmlEscape }}"{{ end }}],
"description": "{{ htmlEscape .Description}}",
"content": {{$page.Plain | jsonify}}
}
{{- end -}}
{{- end -}}]
{{- end -}}]
41 changes: 22 additions & 19 deletions docs/static/js/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var lunrIndex, pagesIndex;
var resultDetails = [];
var resultDetails = [];

function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
Expand All @@ -8,9 +8,11 @@ function endsWith(str, suffix) {
// Initialize lunrjs using our generated index file
function initLunr() {
// First retrieve the index file
$.getJSON($('#indexJSON').attr('href'))
.done(function(index) {
pagesIndex = index;
var indexHref = $('#indexJSON').attr('href');
var indexBaseHref = indexHref.replace('/index.json', '');
$.getJSON(indexHref)
.done(function (index) {
pagesIndex = index;
// Set up lunrjs by declaring the fields we use
// Also provide their boost level for the ranking
//lunrIndex = new lunr.Index
Expand All @@ -26,14 +28,15 @@ function initLunr() {
boost: 2
});
// Feed lunr with each file and let lunr actually index them
pagesIndex.forEach(function(page) {
pagesIndex.forEach(function (page) {
page.indexBaseHref = indexBaseHref;
this.add(page);
}, this);
this.pipeline.remove(this.stemmer)
});

})
.fail(function(jqxhr, textStatus, error) {
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.error("Error getting index.json file:", err);
});
Expand All @@ -44,48 +47,48 @@ function initLunr() {
*/
function search(query) {
// Find the item in our index corresponding to the lunr one to have more info
return lunrIndex.search(query).map(function(result) {
return pagesIndex.filter(function(page) {
return lunrIndex.search(query).map(function (result) {
return pagesIndex.filter(function (page) {
return page.uri === result.ref;
})[0];
});
}

// Let's get started
initLunr();
$( document ).ready(function() {
$(document).ready(function () {
var searchList = new autoComplete({
/* selector for the search box element */
selector: $("#search-query").get(0),
/* source is the callback to perform the search */
source: function(term, response) {
source: function (term, response) {
response(search(term));
},
/* renderItem displays individual search results */
renderItem: function(item, term) {
renderItem: function (item, term) {
var numContextWords = 2;
var text = item.content.match(
"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}" +
term+"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}");
"(?:\\s?(?:[\\w]+)\\s?){0," + numContextWords + "}" +
term + "(?:\\s?(?:[\\w]+)\\s?){0," + numContextWords + "}");
item.context = text;

var pathItem = item.uri;
if (endsWith(pathItem,"/")) {
pathItem = pathItem.substring(0, pathItem.length-1);
if (endsWith(pathItem, "/")) {
pathItem = pathItem.substring(0, pathItem.length - 1);
};

return '<div class="autocomplete-suggestion" ' +
'data-term="' + term + '" ' +
'data-title="' + item.title + '" ' +
'data-uri="'+ item.uri + '" ' +
'data-uri="' + item.indexBaseHref + item.uri + '" ' +
'data-context="' + item.context + '">' +
'» ' + item.title + " - " + pathItem +
'<div class="context">' +
(item.context || '') +'</div>' +
(item.context || '') + '</div>' +
'</div>';
},
/* onSelect callback fires when a search suggestion is chosen */
onSelect: function(e, term, item) {
onSelect: function (e, term, item) {
location.href = item.getAttribute('data-uri');
}
});
Expand Down

0 comments on commit 2a0f93d

Please sign in to comment.