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

HTML Search: Use anchor for search preview #11944

Merged
merged 5 commits into from
Feb 27, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Features added
Bugs fixed
----------

* #11944: Use anchor in search preview.
Patch by Will Lachance.
* #11668: Raise a useful error when ``theme.conf`` is missing.
Patch by Vinay Sajip.
* #11622: Ensure that the order of keys in ``searchindex.js`` is deterministic.
Expand Down
19 changes: 15 additions & 4 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const _displayItem = (item, searchTerms, highlightTerms) => {
.then((data) => {
if (data)
listItem.appendChild(
Search.makeSearchSummary(data, searchTerms)
Search.makeSearchSummary(data, searchTerms, anchor)
);
// highlight search terms in the summary
if (SPHINX_HIGHLIGHT_ENABLED) // set in sphinx_highlight.js
Expand Down Expand Up @@ -160,11 +160,22 @@ const Search = {
_queued_query: null,
_pulse_status: -1,

htmlToText: (htmlString) => {
htmlToText: (htmlString, anchor) => {
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
if (anchor) {
const anchorContent = htmlElement.querySelector(anchor);
picnixz marked this conversation as resolved.
Show resolved Hide resolved
if (anchorContent) return anchorContent.textContent;

console.warn(
`Anchor block not found. Sphinx search tries to obtain it via '${anchor}'. Check your theme or template.`
);
}

// if anchor not specified or not found, fall back to main content
const docContent = htmlElement.querySelector('[role="main"]');
if (docContent) return docContent.textContent;

console.warn(
"Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
wlach marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down Expand Up @@ -549,8 +560,8 @@ const Search = {
* search summary for a given text. keywords is a list
* of stemmed words.
*/
makeSearchSummary: (htmlText, keywords) => {
const text = Search.htmlToText(htmlText);
makeSearchSummary: (htmlText, keywords, anchor) => {
const text = Search.htmlToText(htmlText, anchor);
if (text === "") return null;

const textLower = text.toLowerCase();
Expand Down
32 changes: 32 additions & 0 deletions tests/js/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ describe('Basic html theme search', function() {

});

describe("htmlToText", function() {

const testHTML = `<html>
<div class="body" role="main">
<section id="getting-started">
<h1>Getting Started</h1>
<p>Some text</p>
</section>
<section id="other-section">
<h1>Other Section</h1>
<p>Other text</p>
</section>
<section id="yet-another-section">
<h1>Yet Another Section</h1>
<p>More text</p>
</section>
</div>
</html>`;

it("basic case", () => {
expect(Search.htmlToText(testHTML).trim().split(/\s+/)).toEqual([
Copy link
Contributor

Choose a reason for hiding this comment

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

I have some ideas about the reason, but just to check they match reality: why are we testing htmlToText and not makeSearchSummary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's easier to test the lower-level construct to verify that it doesn't have any bugs. makeSearchSummary has a bunch of magic numbers and heuristics in it which seems much more subject to change.

'Getting', 'Started', 'Some', 'text',
'Other', 'Section', 'Other', 'text',
'Yet', 'Another', 'Section', 'More', 'text'
]);
});

it("will start reading from the anchor", () => {
expect(Search.htmlToText(testHTML, '#other-section').trim().split(/\s+/)).toEqual(['Other', 'Section', 'Other', 'text']);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the trim...split behaviour here to avoid being too-precise about whitespace in the output?

If so, an alternative could be to make a less-restrictive assertion; something like retrievedText.startsWith("Other Section").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right, just trying not to test whitespace. I think I prefer to check for all the tokens in the output, feels more comprehensive to me.

});
});

// This is regression test for https://github.com/sphinx-doc/sphinx/issues/3150
describe('splitQuery regression tests', () => {

Expand Down