Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

[Update] Support Page Sections & Sphinx Domains #19

Merged
merged 43 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e24214d
Update the extension to support page sections and sphinx domains
dojutsu-user Jun 29, 2019
8b56cee
fix tests
dojutsu-user Jun 29, 2019
3cdedc7
set count to zero
dojutsu-user Jun 29, 2019
a477e9c
set margin
dojutsu-user Jun 29, 2019
eecb619
update docs
dojutsu-user Jun 30, 2019
03f1808
change debounce time
dojutsu-user Jun 30, 2019
2173fc4
update debounce time
dojutsu-user Jun 30, 2019
66d8861
add min js file
dojutsu-user Jul 1, 2019
31f142f
use templating langugage
dojutsu-user Jul 2, 2019
abf809c
add missing bracket
dojutsu-user Jul 2, 2019
c115c52
show sorted results
dojutsu-user Jul 2, 2019
24c84f5
add comments
dojutsu-user Jul 2, 2019
156a8b9
Fix test
dojutsu-user Jul 2, 2019
d533cfc
show more data for domain objects
dojutsu-user Jul 3, 2019
73f3b4b
remove external templating language
dojutsu-user Jul 3, 2019
2d9763b
fix horizontal scroll bar
dojutsu-user Jul 3, 2019
1e39d47
show subprojecs with font reduced
dojutsu-user Jul 4, 2019
e4796a7
fix clashing css class
dojutsu-user Jul 4, 2019
6e869e2
use more .template
dojutsu-user Jul 4, 2019
a789e81
ui improvements and make extension compatible with sphinx <= 1.8
dojutsu-user Jul 4, 2019
604204b
edit tox.ini
dojutsu-user Jul 4, 2019
4a48c8a
Merge branch 'master' into update-extension-with-sections
dojutsu-user Jul 5, 2019
7ed4096
remove sphinx17 from tox
dojutsu-user Jul 5, 2019
84fc171
ignore RemovedInSphinx40Warning in pytest.ini
dojutsu-user Jul 5, 2019
5119812
add test
dojutsu-user Jul 8, 2019
85d082d
use sphinx 18 syntax
dojutsu-user Jul 8, 2019
8dce55e
don't use minified file
dojutsu-user Jul 8, 2019
c735b75
remove html structure comments and from the docs
dojutsu-user Jul 9, 2019
047a438
show max 3 results from page sections
dojutsu-user Jul 9, 2019
d8c9d46
improvements
dojutsu-user Jul 9, 2019
40fef13
use helper functions
dojutsu-user Jul 9, 2019
ab1dbbb
use 100 as constant
dojutsu-user Jul 10, 2019
2957f9a
use minified files
dojutsu-user Jul 10, 2019
7c1dfcf
update minified files
dojutsu-user Jul 10, 2019
dbddbd4
add checkbox for searching in current section
dojutsu-user Jul 10, 2019
bf7dfec
remove checkbox for now
dojutsu-user Jul 10, 2019
ed8764e
clean some code
dojutsu-user Jul 10, 2019
75a0cd9
fix tests
dojutsu-user Jul 10, 2019
46a383b
Merge branch 'master' into update-extension-with-sections
dojutsu-user Jul 11, 2019
2746b35
update minified files
dojutsu-user Jul 11, 2019
b164cb2
provide setting for choosing between minified and unminified files
dojutsu-user Jul 12, 2019
ffa8bc3
follow sphinx convention
dojutsu-user Jul 12, 2019
a79e44d
update docs
dojutsu-user Jul 14, 2019
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
62 changes: 41 additions & 21 deletions sphinx_search/_static/js/rtd_sphinx_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const get_section_html = (sectionData, page_link) => {
"... " + sectionData.highlight["sections.content"][0] + " ...";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're accessing only the first item of the list here? Should this be a constant about how many to include?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were getting only one result per section. So there has to be only one element in the array.
But I have refactored the code now and it is much better. We can show as many results from a section as we want. Here's a demo:
Peek 2019-07-09 13-42
I have used it with maximum 3 results from a section

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently I have used the approach of getting the value at index 0 directly everywhere.
I will be refactoring them also. (search.html and main site search)

}

let section_link = `${page_link.href}#${sectionData._source.id}`;
let section_link = `${page_link}#${sectionData._source.id}`;

let section_id = "hit__" + COUNT;

Expand Down Expand Up @@ -149,7 +149,7 @@ const get_domain_html = (domainData, page_link) => {
</a> \
<br class="br-for-hits">';

let domain_link = `${page_link.href}#${domainData._source.anchor}`;
let domain_link = `${page_link}#${domainData._source.anchor}`;
let domain_role_name = domainData._source.role_name;
let domain_type_display = domainData._source.type_display;
let domain_doc_display = domainData._source.doc_display;
Expand Down Expand Up @@ -297,31 +297,34 @@ const get_domain_html = (domainData, page_link) => {
*/
const generateSingleResult = (resultData, projectName) => {
let content = createDomNode("div");
let page_link = createDomNode("a", {
href: `${resultData.link}${
DOCUMENTATION_OPTIONS.FILE_SUFFIX
}?highlight=${SEARCH_QUERY}`
});
let title = createDomNode("h2", { class: "search__result__title" });

let page_link_template =
'<a href="<%= page_link %>"> \
<h2 class="search__result__title"> \
<%= page_title %> \
</h2> \
</a>';

let page_link = `${resultData.link}${DOCUMENTATION_OPTIONS.FILE_SUFFIX}`;
let page_link_highlight =
page_link + "?highlight=" + encodeURIComponent(SEARCH_QUERY);

let page_title = resultData.title;

// if title is present in highlighted field, use that.
if (resultData.highlight !== undefined && resultData.highlight !== null) {
if (
resultData.highlight.title !== undefined &&
resultData.highlight.title !== null
) {
title.innerHTML = resultData.highlight.title;
} else {
title.innerHTML = resultData.title;
page_title = resultData.highlight.title;
}
} else {
title.innerHTML = resultData.title;
}

// if result is not from the same project,
// then it must be from subproject.
if (projectName !== resultData.project) {
title.innerHTML +=
page_title +=
" " +
$u.template(
'<small class="rtd_ui_search_subtitle"> \
Expand All @@ -333,9 +336,12 @@ const generateSingleResult = (resultData, projectName) => {
);
}

page_link.appendChild(title);
content.appendChild(page_link);
content.appendChild(createDomNode("br"));
page_title += "<br>";

content.innerHTML += $u.template(page_link_template, {
page_link: page_link_highlight,
page_title: page_title
});

for (let i = 0; i < resultData.inner_hits.length; ++i) {
const type = resultData.inner_hits[i].type;
Expand Down Expand Up @@ -440,7 +446,21 @@ const addActive = current_focus => {
* @return {Object} Input field node
*/
const getInputField = () => {
const inputField = document.querySelector("div[role='search'] input");
let inputField;

// on search some pages (like search.html),
// no div is present with role="search",
// in that case, use the other query to select
// the input field
try {
inputField = document.querySelector("div[role='search'] input");
if (inputField === undefined || inputField === null) {
throw "'div[role='search'] input' not found";
}
} catch (err) {
inputField = document.querySelector("input[name='q']");
}

return inputField;
};

Expand Down Expand Up @@ -737,9 +757,9 @@ window.addEventListener("DOMContentLoaded", evt => {
} else {
// submit search form if there
// is no active item.
const form = document.querySelector(
"div[role='search'] form"
);
const input_field = getInputField();
const form = input_field.parentElement;

search_bar.value = SEARCH_QUERY || "";
form.submit();
}
Expand Down
Loading