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

Commit 20bacfa

Browse files
committed
Use new stable API
1 parent b34c283 commit 20bacfa

File tree

8 files changed

+62
-106
lines changed

8 files changed

+62
-106
lines changed

docs/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
#
31
# Configuration file for the Sphinx documentation builder.
42
#
53
# This file does only contain a selection of the most common options. For a

docs/js-api-reference.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Following are the functions that are defined in `rtd_sphinx_search.js`_,
1010
.. js:autofunction:: _is_string
1111
.. js:autofunction:: _is_array
1212
.. js:autofunction:: get_section_html
13-
.. js:autofunction:: getHighlightListData
1413
.. js:autofunction:: get_domain_html
1514
.. js:autofunction:: generateSingleResult
1615
.. js:autofunction:: generateSuggestionsList

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.1",
44
"description": "Enable search-as-you-type feature.",
55
"scripts": {
6-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"build": "gulp"
77
},
88
"repository": {
99
"type": "git",

sphinx_search/static/js/rtd_sphinx_search.js

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,18 @@ const get_section_html = (sectionData, page_link) => {
178178
</a> \
179179
<br class="br-for-hits">';
180180

181-
let section_subheading = sectionData._source.title;
182-
let highlight = sectionData.highlight;
183-
if (getHighlightListData(highlight, "sections.title")) {
184-
section_subheading = getHighlightListData(
185-
highlight,
186-
"sections.title"
187-
)[0];
181+
let section_subheading = sectionData.title;
182+
let highlights = sectionData.highlights;
183+
if (highlights.title.length) {
184+
section_subheading = highlights.title[0];
188185
}
189186

190187
let section_content = [
191-
sectionData._source.content.substring(0, MAX_SUBSTRING_LIMIT) + " ..."
188+
sectionData.content.substring(0, MAX_SUBSTRING_LIMIT) + " ..."
192189
];
193190

194-
if (getHighlightListData(highlight, "sections.content")) {
195-
let highlight_content = getHighlightListData(
196-
highlight,
197-
"sections.content"
198-
);
191+
if (highlights.content.length) {
192+
let highlight_content = highlights.content;
199193
section_content = [];
200194
for (
201195
let j = 0;
@@ -206,7 +200,7 @@ const get_section_html = (sectionData, page_link) => {
206200
}
207201
}
208202

209-
let section_link = `${page_link}#${sectionData._source.id}`;
203+
let section_link = `${page_link}#${sectionData.id}`;
210204

211205
let section_id = "hit__" + COUNT;
212206

@@ -220,22 +214,6 @@ const get_section_html = (sectionData, page_link) => {
220214
return section_html;
221215
};
222216

223-
/**
224-
* Returns value of the corresponding key (if present),
225-
* else returns false.
226-
*
227-
* @param {Object} data object containing the data used for highlighting
228-
* @param {String} key key whose values is to be returned
229-
* @return {Array|Boolean} if key is present, it will return its value. Otherwise, return false
230-
*/
231-
const getHighlightListData = (data, key) => {
232-
if (_is_array(data[key])) {
233-
return data[key];
234-
} else {
235-
return false;
236-
}
237-
};
238-
239217
/**
240218
* Generate and return html structure
241219
* for a sphinx domain result.
@@ -258,38 +236,28 @@ const get_domain_html = (domainData, page_link) => {
258236
</a> \
259237
<br class="br-for-hits">';
260238

261-
let domain_link = `${page_link}#${domainData._source.anchor}`;
262-
let domain_role_name = domainData._source.role_name;
263-
let domain_name = domainData._source.name;
264-
let domain_docstrings =
265-
domainData._source.docstrings.substr(0, MAX_SUBSTRING_LIMIT) + " ...";
239+
let domain_link = `${page_link}#${domainData.id}`;
240+
let domain_role_name = domainData.role;
241+
let domain_name = domainData.name;
242+
let domain_content =
243+
domainData.content.substr(0, MAX_SUBSTRING_LIMIT) + " ...";
266244

267-
// take values from highlighted fields (if present)
268-
if (domainData.highlight !== undefined && domainData.highlight !== null) {
269-
let highlight = domainData.highlight;
270-
271-
let name = getHighlightListData(highlight, "domains.name");
272-
let docstrings = getHighlightListData(highlight, "domains.docstrings");
273-
274-
if (name) {
275-
domain_name = name[0];
276-
}
277-
278-
if (docstrings) {
279-
domain_docstrings = docstrings[0];
280-
}
245+
let highlights = domainData.highlights;
246+
if (highlights.name.length) {
247+
domain_name = highlights.name[0];
248+
}
249+
if (highlights.content.length) {
250+
domain_content = highlights.content[0];
281251
}
282252

283-
let domain_subheading = domain_name;
284-
let domain_content = domain_docstrings;
285253
let domain_id = "hit__" + COUNT;
286254
domain_role_name = "[" + domain_role_name + "]";
287255

288256
let domain_html = $u.template(domain_template, {
289257
domain_link: domain_link,
290258
domain_id: domain_id,
291259
domain_content: domain_content,
292-
domain_subheading: domain_subheading,
260+
domain_subheading: domain_name,
293261
domain_role_name: domain_role_name
294262
});
295263

@@ -312,17 +280,12 @@ const generateSingleResult = (resultData, projectName) => {
312280
</h2> \
313281
</a>';
314282

315-
let page_link = resultData.link;
283+
let page_link = resultData.path;
316284
let page_title = resultData.title;
285+
let highlights = resultData.highlights;
317286

318-
// if title is present in highlighted field, use that.
319-
if (resultData.highlight !== undefined && resultData.highlight !== null) {
320-
if (
321-
resultData.highlight.title !== undefined &&
322-
resultData.highlight.title !== null
323-
) {
324-
page_title = resultData.highlight.title;
325-
}
287+
if (highlights.title.length) {
288+
page_title = highlights.title[0];
326289
}
327290

328291
// if result is not from the same project,
@@ -347,19 +310,19 @@ const generateSingleResult = (resultData, projectName) => {
347310
page_title: page_title
348311
});
349312

350-
for (let i = 0; i < resultData.inner_hits.length; ++i) {
351-
const type = resultData.inner_hits[i].type;
313+
for (let i = 0; i < resultData.blocks.length; ++i) {
314+
let block = resultData.blocks[i];
352315
COUNT += 1;
353316
let html_structure = "";
354317

355-
if (type === "sections") {
318+
if (block.type === "section") {
356319
html_structure = get_section_html(
357-
resultData.inner_hits[i],
320+
block,
358321
page_link
359322
);
360-
} else if (type === "domains") {
323+
} else if (block.type === "domain") {
361324
html_structure = get_domain_html(
362-
resultData.inner_hits[i],
325+
block,
363326
page_link
364327
);
365328
}
@@ -673,12 +636,12 @@ window.addEventListener("DOMContentLoaded", evt => {
673636
q: SEARCH_QUERY,
674637
project: project,
675638
version: version,
676-
language: language
639+
language: language,
677640
};
678641

679642
const search_url =
680643
api_host +
681-
"/api/v2/docsearch/?" +
644+
"/api/v2/search/?" +
682645
convertObjToUrlParams(search_params);
683646

684647
if (typeof SEARCH_QUERY === "string" && SEARCH_QUERY.length > 0) {

sphinx_search/static/js/rtd_sphinx_search.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/dummy_results.json

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,43 @@
77
"project": "sphinx",
88
"version": "latest",
99
"title": "Developing a “Hello world” extension",
10-
"path": "development/tutorials/helloworld",
1110
"link": "http://localhost:8000/docs/template/projects/sphinx/en/latest/development/tutorials/helloworld",
12-
"highlight": {
11+
"highlights": {
1312
"title": [
1413
"Developing a “<em>Hello</em> <em>world</em>” extension"
1514
]
1615
},
17-
"inner_hits": [
16+
"blocks": [
1817
{
19-
"type": "sections",
20-
"_source": {
21-
"id": "overview",
22-
"title": "Overview",
23-
"content": "We want the extension to add the following to Sphinx:\nA helloworld directive, that will simply output the text “hello world”."
24-
},
25-
"highlight": {
26-
"sections.content": ["<em>world</em>”."]
18+
"type": "section",
19+
"id": "overview",
20+
"title": "Overview",
21+
"content": "We want the extension to add the following to Sphinx:\nA helloworld directive, that will simply output the text “hello world”.",
22+
"highlights": {
23+
"title": [],
24+
"content": ["<em>world</em>”."]
2725
}
2826
},
2927
{
30-
"type": "sections",
31-
"_source": {
32-
"id": "developing-a-hello-world-extension",
33-
"title": "Developing a “Hello world” extension",
34-
"content": "Only basic information is provided in this tutorial. For more information, refer to the other tutorials that go into more details.\n\nWarning\nFor this extension, you will need some basic understanding of docutils and Python.\n"
35-
},
36-
"highlight": {
37-
"sections.title": [
28+
"type": "section",
29+
"id": "developing-a-hello-world-extension",
30+
"title": "Developing a “Hello world” extension",
31+
"content": "Only basic information is provided in this tutorial. For more information, refer to the other tutorials that go into more details.\n\nWarning\nFor this extension, you will need some basic understanding of docutils and Python.\n",
32+
"highlights": {
33+
"title": [
3834
"Developing a “<em>Hello</em> <em>world</em>” extension"
39-
]
35+
],
36+
"content": []
4037
}
4138
},
4239
{
43-
"type": "sections",
44-
"_source": {
45-
"id": "using-the-extension",
46-
"title": "Using the extension",
47-
"content": "The extension has to be declared in your conf.py file to make Sphinx aware of it. There are two steps necessary here:\nAdd the _ext directory to the Python path using sys.path.append. This should be placed at the top of the file.\nUpdate or create the extensions list and add the extension file name to the list\nFor example:\nimport os import sys sys.path.append(os.path.abspath(\"./_ext\")) extensions = ['helloworld']\nTip\nWe’re not distributing this extension as a Python package, we need to modify the Python path so Sphinx can find our extension. This is why we need the call to sys.path.append.\nYou can now use the extension in a file. For example:\nSome intro text here... .. helloworld:: Some more text here...\nThe sample above would generate:\nSome intro text here... Hello World! Some more text here..."
48-
},
49-
"highlight": {
50-
"sections.content": [
40+
"type": "section",
41+
"id": "using-the-extension",
42+
"title": "Using the extension",
43+
"content": "The extension has to be declared in your conf.py file to make Sphinx aware of it. There are two steps necessary here:\nAdd the _ext directory to the Python path using sys.path.append. This should be placed at the top of the file.\nUpdate or create the extensions list and add the extension file name to the list\nFor example:\nimport os import sys sys.path.append(os.path.abspath(\"./_ext\")) extensions = ['helloworld']\nTip\nWe’re not distributing this extension as a Python package, we need to modify the Python path so Sphinx can find our extension. This is why we need the call to sys.path.append.\nYou can now use the extension in a file. For example:\nSome intro text here... .. helloworld:: Some more text here...\nThe sample above would generate:\nSome intro text here... Hello World! Some more text here...",
44+
"highlights": {
45+
"title": [],
46+
"content": [
5147
"<em>Hello</em> <em>World</em>! Some more text here..."
5248
]
5349
}

tests/test_ui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_closing_the_modal_by_escape_button(selenium, app, status, warning):
214214
with InjectJsManager(path, SCRIPT_TAG) as _:
215215
selenium.get(f'file://{path}')
216216
open_search_modal(selenium)
217-
217+
218218
search_outer_wrapper = selenium.find_element_by_class_name(
219219
'search__outer__wrapper'
220220
)
@@ -611,7 +611,7 @@ def test_writing_query_adds_rtd_search_as_url_param(selenium, app, status, warni
611611
search_outer_input.send_keys(Keys.BACK_SPACE)
612612

613613
if i != query_len -1:
614-
614+
615615
current_query = query[:query_len - i - 1]
616616
current_url = parse.unquote(selenium.current_url)
617617
query_in_url = current_url[current_url.find('rtd_search'):]

tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __enter__(self):
3030
)
3131
f.seek(0)
3232
f.write(new_content)
33-
33+
3434
return self._file
3535

3636
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -129,7 +129,7 @@ def get_ajax_overwrite_func(type_, **kwargs):
129129
elif type_ == 'dummy_results':
130130
with open(DUMMY_RESULTS, 'r') as f:
131131
dummy_res = f.read()
132-
132+
133133
ajax_func = f'''
134134
<script>
135135
$.ajax = function(params) {{

0 commit comments

Comments
 (0)