10000 [3.12] Docs search: Replace jQuery with vanilla JavaScript (GH-106743… · python/cpython@30c8915 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30c8915

Browse files
[3.12] Docs search: Replace jQuery with vanilla JavaScript (GH-106743) (#106802)
Docs search: Replace jQuery with vanilla JavaScript (GH-106743) * Replace jQuery with vanilla JavaScript * Switch 'var' to 'const' or 'let' (cherry picked from commit c02ee45) Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
1 parent e4658bf commit 30c8915

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

Doc/tools/templates/search.html

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,62 @@
11
{% extends "!search.html" %}
22
{% block extrahead %}
33
{{ super() }}
4+
<meta name="robots" content="noindex">
45
<script type="text/javascript">
5-
var GLOSSARY_PAGE = 'glossary.html';
6+
const GLOSSARY_PAGE = 'glossary.html';
67

7-
jQuery(function() {
8-
$.getJSON("_static/glossary.json", function(glossary) {
9-
var RESULT_TEMPLATE = '<div style="display: none" class="admonition seealso" id="glossary-result">' +
8+
document.addEventListener('DOMContentLoaded', function() {
9+
fetch('_static/glossary.json')
10+
.then(function(response) {
11+
if (response.ok) {
12+
return response.json();
13+
} else {
14+
throw new Error('Failed to fetch glossary.json');
15+
}
16+
})
17+
.then(function(glossary) {
18+
const RESULT_TEMPLATE = '<div style="display: none" class="admonition seealso" id="glossary-result">' +
1019
' <p class="topic-title">' +
1120
' <a class="glossary-title" href="#"></a>' +
1221
' </p>' +
1322
' <div class="glossary-body"></div>' +
1423
'</div>';
15-
$("#search-results").prepend(RESULT_TEMPLATE);
24+
let searchResults = document.getElementById('search-results');
25+
searchResults.insertAdjacentHTML('afterbegin', RESULT_TEMPLATE);
1626

17-
var params = $.getQueryParameters();
18-
if (params.q) {
19-
var search_param = params.q[0].toLowerCase();
20-
var glossary_item = glossary[search_param];
21-
if (glossary_item) {
22-
var resultDiv = $("#glossary-result");
27+
const params = new URLSearchParams(document.location.search).get("q");
28+
if (params) {
29+
const searchParam = params.toLowerCase();
30+
const glossaryItem = glossary[searchParam];
31+
if (glossaryItem) {
32+
let resultDiv = document.getElementById('glossary-result');
2333

24-
// set up the title text with a link to the glossary page
25-
resultDiv.find(".glossary-title").text('Glossary: ' + glossary_item.title);
26-
var link_target = search_param.replace(/ /g, '-');
27-
resultDiv.find(".glossary-title").attr(
28-
'href', GLOSSARY_PAGE + '#term-' + link_target
29-
);
34+
// set up the title text with a link to the glossary page
35+
let glossaryTitle = resultDiv.querySelector('.glossary-title');
36+
glossaryTitle.textContent = 'Glossary: ' + glossaryItem.title;
37+
const linkTarget = searchParam.replace(/ /g, '-');
38+
glossaryTitle.href = GLOSSARY_PAGE + '#term-' + linkTarget;
3039

31-
// rewrite any anchor links (to other glossary terms)
32-
// to have a full reference to the glossary page
33-
var body = $(glossary_item.body).children();
34-
body.find("a[href^='#']").each(function() {
35-
var current_url = $(this).attr('href');
36-
$(this).attr('href', GLOSSARY_PAGE + current_url);
37-
});
38-
resultDiv.find(".glossary-body").html(body);
40+
// rewrite any anchor links (to other glossary terms)
41+
// to have a full reference to the glossary page
42+
let body = document.createElement('div');
43+
body.innerHTML = glossaryItem.body;
44+
const anchorLinks = body.querySelectorAll('a[href^="#"]');
45+
anchorLinks.forEach(function(link) {
46+
const currentUrl = link.getAttribute('href');
47+
link.href = GLOSSARY_PAGE + currentUrl;
48+
});
49+
resultDiv.querySelector('.glossary-body').appendChild(body);
3950

40-
resultDiv.show();
41-
} else {
42-
$("#glossary-result").hide('');
43-
}
51+
resultDiv.style.display = '';
52+
} else {
53+
document.getElementById('glossary-result').style.display = 'none';
4454
}
55+
}
56+
})
57+
.catch(function(error) {
58+
console.error(error);
4559
});
4660
});
4761
</script>
48-
{% endblock %}
62+
{% endblock %}

0 commit comments

Comments
 (0)
0