Skip to content

Commit 37a2241

Browse files
committed
De-duplicate on document title only
1 parent ee188f7 commit 37a2241

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

sphinx/themes/basic/static/searchtools.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
6464
const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY;
6565
const contentRoot = document.documentElement.dataset.content_root;
6666

67-
const [docName, title, anchor, descr, score, _filename] = item;
67+
const [docName, title, _isDocumentTitle, anchor, descr, score] = item;
6868

6969
let listItem = document.createElement("li");
7070
let requestUrl;
@@ -314,6 +314,7 @@ const Search = {
314314
results.push([
315315
docNames[file],
316316
isDocumentTitle ? title : `${titles[file]} > ${title}`,
317+
isDocumentTitle,
317318
anchor,
318319
null,
319320
score,
@@ -331,6 +332,7 @@ const Search = {
331332
results.push([
332333
docNames[file],
333334
titles[file],
335+
false,
334336
id ? "#" + id : "",
335337
null,
336338
score,
@@ -355,8 +357,8 @@ const Search = {
355357
// display function below uses pop() to retrieve items) and then
356358
// alphabetically
357359
results.sort((a, b) => {
358-
const leftScore = a[4];
359-
const rightScore = b[4];
360+
const leftScore = a[5];
361+
const rightScore = b[5];
360362
if (leftScore === rightScore) {
361363
// same score: sort alphabetically
362364
const leftTitle = a[1].toLowerCase();
@@ -371,8 +373,11 @@ const Search = {
371373
// note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept
372374
let seen = new Set();
373375
results = results.reverse().reduce((acc, result) => {
374-
// de-duplicate on file, title, and description
375-
let resultStr = result.slice(0, 2).concat(result[3]).map(v => String(v)).join(',');
376+
// de-duplicate on file, title, description, and (if not the title section) anchor
377+
// we omit the anchor for the title section as otherwise we'll get two entries for the
378+
// entire document
379+
let [docname, title, isDocumentTitle, anchor, descr, score, filename] = result;
380+
let resultStr = [docname, title, isDocumentTitle ? '' : anchor, descr].map(v => String(v)).join(',');
376381
if (!seen.has(resultStr)) {
377382
acc.push(result);
378383
seen.add(resultStr);
@@ -446,6 +451,7 @@ const Search = {
446451
results.push([
447452
docNames[match[0]],
448453
fullname,
454+
false,
449455
"#" + anchor,
450456
descr,
451457
score,
@@ -557,6 +563,7 @@ const Search = {
557563
results.push([
558564
docNames[file],
559565
titles[file],
566+
false,
560567
"",
561568
null,
562569
score,

tests/js/searchtools.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Basic html theme search', function() {
1919
hits = [[
2020
"index",
2121
"<no title>",
22+
false,
2223
"",
2324
null,
2425
5,
@@ -76,7 +77,7 @@ describe('Basic html theme search', function() {
7677
let { results } = Search.query('main page');
7778
// should only be one result
7879
expect(results).toEqual([
79-
[ 'index', 'Main Page', '#main-page', null, 100, 'index.rst' ],
80+
[ 'index', 'Main Page', true, '#main-page', null, 100, 'index.rst' ],
8081
]);
8182
});
8283
})

0 commit comments

Comments
 (0)