2
0

search.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. {% extends "layout.html" %}
  2. {% set title = _('Search') %}
  3. {% block body %}
  4. <noscript>
  5. <div id="fallback" class="admonition warning">
  6. <p class="last">
  7. {% trans trimmed %}Please activate JavaScript to enable the search
  8. functionality.{% endtrans %}
  9. </p>
  10. </div>
  11. </noscript>
  12. <div id="search-results">
  13. </div>
  14. {% endblock %}
  15. {% block footer %}
  16. {{ super() }}
  17. <script>
  18. function runSearchPageSearch() {
  19. const urlParams = new URLSearchParams(window.location.search)
  20. const query = urlParams.get('q')
  21. const searchResultsContainer = document.getElementById('search-results')
  22. addHeadingForQuery(query, searchResultsContainer)
  23. const docSearch = docSearchReady()
  24. const index = docSearch.client.initIndex('wagtail')
  25. index.search(
  26. query,
  27. {
  28. hitsPerPage: 50,
  29. facetFilters: [getVersionFacetFilter()]
  30. }
  31. )
  32. .then(({ hits }) => addResultsList(hits, query, searchResultsContainer))
  33. .catch((error) => console.log(error))
  34. }
  35. function addHeadingForQuery(query, parentElement) {
  36. const searchHeading = document.createElement('h1')
  37. searchHeading.textContent = `Search results for “${query}”`
  38. parentElement.appendChild(searchHeading)
  39. }
  40. function addResultsList(hits, query, parentElement) {
  41. const searchResultsList = document.createElement('ul')
  42. searchResultsList.className = "search"
  43. for (hit of hits) {
  44. const hitElement = createHitElement(hit, query)
  45. searchResultsList.appendChild(hitElement)
  46. }
  47. parentElement.appendChild(searchResultsList)
  48. }
  49. function createHitElement(hitData, query) {
  50. const pageURL = new URL(hitData.url)
  51. pageURL.hash = '';
  52. pageURL.searchParams.set('highlight', query);
  53. const anchorURL = new URL(hitData.url);
  54. anchorURL.searchParams.set('highlight', query);
  55. const result = hitData._highlightResult
  56. const hitListElement = document.createElement('li')
  57. const hierarchies = Object.values(result.hierarchy);
  58. const firstHierarchyLevel = hierarchies[0];
  59. const lastHierarchyLevel = hierarchies[hierarchies.length - 1];
  60. const pageLink = document.createElement('a')
  61. hitListElement.appendChild(pageLink)
  62. pageLink.innerHTML = firstHierarchyLevel.value
  63. pageLink.href = pageURL
  64. const contextElement = document.createElement('div')
  65. hitListElement.appendChild(contextElement)
  66. contextElement.className = 'context'
  67. if (lastHierarchyLevel && lastHierarchyLevel !== firstHierarchyLevel ) {
  68. const contextLinkContainer = document.createElement('div')
  69. contextElement.appendChild(contextLinkContainer)
  70. const contextLink = document.createElement('a')
  71. contextLinkContainer.appendChild(contextLink)
  72. contextLink.innerHTML = lastHierarchyLevel.value
  73. contextLink.href = anchorURL
  74. }
  75. if (result.content) {
  76. const contentElement = document.createElement('div')
  77. contentElement.innerHTML = result.content.value
  78. contextElement.appendChild(contentElement)
  79. }
  80. return hitListElement
  81. }
  82. window.addEventListener('DOMContentLoaded', runSearchPageSearch)
  83. </script>
  84. {% endblock %}