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

fix(doc): search links #5725

Merged
merged 1 commit into from
Feb 26, 2021
Merged
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
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