Skip to content

Commit

Permalink
Use a JS class, prepend result-kind- to the class name
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jul 11, 2024
1 parent bb3c7a0 commit ebbcb36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Deprecated
Features added
--------------

* #12474: Support type-dependent search result highlighting via CSS
Patch by Tim Hoffmann
* #12474: Support type-dependent search result highlighting via CSS.
Patch by Tim Hoffmann.
* #11165: Support the `officially recommended`_ ``.jinja`` suffix for template
files.
Patch by James Addison and Adam Turner
Expand Down
23 changes: 9 additions & 14 deletions doc/_themes/sphinx13/static/sphinx13.css
Original file line number Diff line number Diff line change
Expand Up @@ -682,24 +682,19 @@ div.sphinx-feature > p.admonition-title::before {
ul.search {
padding-left: 30px;
}

ul.search li {
padding: 5px 0 5px 10px;
list-style: initial;
list-style-type: "\25A1"; /* Unicode: White Square */
}

ul.search li.index {
list-style: "\1F4D1"; /* Unicode: Bookmark Tabs */
ul.search li.result-kind-index {
list-style-type: "\1F4D1"; /* Unicode: Bookmark Tabs */
}

ul.search li.object {
list-style: "\1F4E6"; /* Unicode: Package */
ul.search li.result-kind-object {
list-style-type: "\1F4E6"; /* Unicode: Package */
}

ul.search li.title {
list-style: "\1F4C4"; /* Unicode: Page Facing Up */
ul.search li.result-kind-title {
list-style-type: "\1F4C4"; /* Unicode: Page Facing Up */
}

ul.search li.text {
list-style: "\1F4C4"; /* Unicode: Page Facing Up */
ul.search li.result-kind-text {
list-style-type: "\1F4C4"; /* Unicode: Page Facing Up */
}
34 changes: 19 additions & 15 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (typeof Scorer === "undefined") {
// and returns the new score.
/*
score: result => {
const [docname, title, anchor, descr, score, filename, resultType] = result
const [docname, title, anchor, descr, score, filename, resultKind] = result
return score
},
*/
Expand All @@ -47,11 +47,12 @@ if (typeof Scorer === "undefined") {
};
}

const SearchResultType = {
index: "index",
object: "object",
text: "text",
title: "title",
// Global search result kind enum, used by themes to style search results.
class SearchResultKind {
static get index() { return "index"; }
static get object() { return "object"; }
static get text() { return "text"; }
static get title() { return "title"; }
}

const _removeChildren = (element) => {
Expand All @@ -71,10 +72,13 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY;
const contentRoot = document.documentElement.dataset.content_root;

const [docName, title, anchor, descr, score, _filename, resultType] = item;
const [docName, title, anchor, descr, score, _filename, resultKind] = item;

let listItem = document.createElement("li");
listItem.classList.add(resultType)
// Add a class representing the item's type:
// can be used by a theme's CSS selector for styling
// See SearchResultKind for the class names.
listItem.classList.add(`result-kind-${resultKind}`);
let requestUrl;
let linkUrl;
if (docBuilder === "dirhtml") {
Expand Down Expand Up @@ -146,7 +150,7 @@ const _displayNextItem = (
else _finishSearch(resultCount);
};
// Helper function used by query() to order search results.
// Each input is an array of [docname, title, anchor, descr, score, filename, resultType].
// Each input is an array of [docname, title, anchor, descr, score, filename, resultKind].
// Order the results by score (in opposite order of appearance, since the
// `_displayNextItem` function uses pop() to retrieve items) and then alphabetically.
const _orderResultsByScoreThenName = (a, b) => {
Expand Down Expand Up @@ -256,7 +260,7 @@ const Search = {
searchSummary.classList.add("search-summary");
searchSummary.innerText = "";
const searchList = document.createElement("ul");
searchList.setAttribute("role", "list")
searchList.setAttribute("role", "list");
searchList.classList.add("search");

const out = document.getElementById("search-results");
Expand Down Expand Up @@ -327,7 +331,7 @@ const Search = {
const indexEntries = Search._index.indexentries;

// Collect multiple result groups to be sorted separately and then ordered.
// Each is an array of [docname, title, anchor, descr, score, filename, resultType].
// Each is an array of [docname, title, anchor, descr, score, filename, resultKind].
const normalResults = [];
const nonMainIndexResults = [];

Expand All @@ -345,7 +349,7 @@ const Search = {
null,
score,
filenames[file],
SearchResultType.title,
SearchResultKind.title,
]);
}
}
Expand All @@ -363,7 +367,7 @@ const Search = {
null,
score,
filenames[file],
SearchResultType.index,
SearchResultKind.index,
];
if (isMain) {
normalResults.push(result);
Expand Down Expand Up @@ -485,7 +489,7 @@ const Search = {
descr,
score,
filenames[match[0]],
SearchResultType.object,
SearchResultKind.object,
]);
};
Object.keys(objects).forEach((prefix) =>
Expand Down Expand Up @@ -596,7 +600,7 @@ const Search = {
null,
score,
filenames[file],
SearchResultType.text,
SearchResultKind.text,
]);
}
return results;
Expand Down

0 comments on commit ebbcb36

Please sign in to comment.