Skip to content

Commit 5df2642

Browse files
committed
Add breadcrumbs to search results
1 parent ed40d39 commit 5df2642

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed

docs/_static/searchtools.js

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
*/
1111

12+
var title_documentation = 'Plone Documentation';
1213

1314
if (!Scorer) {
1415
/**
@@ -199,10 +200,6 @@ var Search = {
199200
}
200201
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
201202

202-
// console.debug('SEARCH: searching for:');
203-
// console.info('required: ', searchterms);
204-
// console.info('excluded: ', excluded);
205-
206203
// prepare search
207204
var terms = this._index.terms;
208205
var titleterms = this._index.titleterms;
@@ -257,8 +254,28 @@ var Search = {
257254
}
258255
});
259256

257+
function _getBreadcrumbs(item, linkUrl) {
258+
let path = item[0];
259+
let parentTitles = item[6];
260+
let markup = path.split('/')
261+
.slice(0, -1);
262+
markup = markup.map((el, index) => {
263+
return {
264+
"path": markup.slice(0, index+1).join('/'),
265+
"title": parentTitles[index]
266+
}
267+
})
268+
markup = markup
269+
.map((el) => {
270+
let foo = `<a href="/${el.path}"> ${el.title}</a>`
271+
return foo;
272+
})
273+
markup.push(`<span class="lastbreadcrumb">${item[1]}</span>`)
274+
markup = markup.join('<span class="pathseparator">&gt;</span>');
275+
return markup
276+
}
260277

261-
// print the results
278+
// Print the results.
262279
var resultCount = results.length;
263280
function displayNextItem() {
264281
// results left, load the summary and display it
@@ -283,11 +300,16 @@ var Search = {
283300
requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX;
284301
linkUrl = item[0] + DOCUMENTATION_OPTIONS.LINK_SUFFIX;
285302
}
286-
listItem.append($('<a/>').attr('href',
287-
linkUrl +
288-
highlightstring + item[2]).html(item[1]));
303+
let breadcrumbs = _getBreadcrumbs(item, linkUrl);
304+
breadcrumbs = $("<div class='breadcrumbs'>" + breadcrumbs + "</div>");
305+
listItem.append(breadcrumbs);
306+
let headline = $('<h3/>');
307+
headline.append($('<a/>').attr('href',
308+
linkUrl +
309+
highlightstring + item[2]).html(item[1]));
289310

290-
listItem.append($('<span class="title_doc_section">' + item[6] + '</span>'));
311+
// headline.append($('<span class="title_doc_section">' + item[6] + '</span>'));
312+
listItem.append(headline);
291313

292314
if (item[3]) {
293315
listItem.append($('<span> (' + item[3] + ')</span>'));
@@ -321,9 +343,9 @@ var Search = {
321343
Search.stopPulse();
322344
Search.title.text(_('Search Results'));
323345
if (!resultCount)
324-
Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.'));
346+
Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly. Searching for multiple words only shows matches that contain all words.'));
325347
else
326-
Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
348+
Search.status.text(_('Found %s page(s) matching the search query.').replace('%s', resultCount));
327349
Search.status.fadeIn(500);
328350
}
329351
}
@@ -344,7 +366,9 @@ var Search = {
344366
var results = [];
345367

346368
for (var prefix in objects) {
347-
for (var name in objects[prefix]) {
369+
for (var iMatch = 0; iMatch != objects[prefix].length; ++iMatch) {
370+
var match = objects[prefix][iMatch];
371+
var name = match[4];
348372
var fullname = (prefix ? prefix + '.' : '') + name;
349373
var fullnameLower = fullname.toLowerCase()
350374
if (fullnameLower.indexOf(object) > -1) {
@@ -358,7 +382,6 @@ var Search = {
358382
} else if (parts[parts.length - 1].indexOf(object) > -1) {
359383
score += Scorer.objPartialMatch;
360384
}
361-
var match = objects[prefix][name];
362385
var objname = objnames[match[1]][2];
363386
var title = titles[match[0]];
364387
// If more than one term searched for, we require other words to be
@@ -496,19 +519,41 @@ var Search = {
496519
break;
497520
}
498521
}
499-
500522
// if we have still a valid result we can add it to the result list
501523
if (valid) {
524+
/**
525+
* file: index
526+
* docnames: array of paths
527+
* titles: array of titles
528+
*/
529+
502530
// select one (max) score for the file.
503531
// for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
504532
var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
505-
function getParentTitle(f) {
533+
534+
function get1stLevelAncestor(f) {
506535
let parentdocname = docnames[f].split('/')[0] + '/index';
507536
let parentID = docnames.indexOf(parentdocname);
508-
let title = parentID === -1 ? 'Plone Documentation' : titles[parentID];
537+
let title = parentID === -1 ? title_documentation : titles[parentID];
509538
return title
510539
}
511-
results.push([docnames[file], titles[file], '', null, score, filenames[file], getParentTitle(file)]);
540+
function getParentTitles(idx) {
541+
let path = docnames[idx]
542+
543+
let foo = path.split('/').slice(0, -1);
544+
foo = foo.map((el, index) => {
545+
return `${foo.slice(0, index+1).join('/')}/index`
546+
})
547+
548+
let parentTitles = foo.map(el => {
549+
let parentId = docnames.indexOf(el);
550+
let title = parentId === -1 ? title_documentation : titles[parentId];
551+
return title
552+
})
553+
return parentTitles
554+
}
555+
556+
results.push([docnames[file], titles[file], '', null, score, filenames[file], getParentTitles(file)]);
512557
}
513558
}
514559
return results;

0 commit comments

Comments
 (0)