layout.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {% extends "!layout.html" %}
  2. {% set docsearch_version = '2' %}
  3. {% set docsearch_base = 'https://cdn.jsdelivr.net/npm/docsearch.js@' ~ docsearch_version ~ '/dist/cdn/' %}
  4. {% block extrahead %}
  5. <link rel="stylesheet" href="{{ docsearch_base ~ 'docsearch.min.css' }}"/>
  6. <link rel="stylesheet" href="{{ pathto('_static/css/docsearch.overrides.css', 1) }}" />
  7. {% endblock %}
  8. {% block search %}
  9. <script async defer data-domain="docs.wagtail.io" src="https://plausible.io/js/plausible.js"></script>
  10. <script src="{{ docsearch_base }}docsearch.min.js"></script>
  11. <script>
  12. /**
  13. * Get version of the currently served docs.
  14. *
  15. * PR builds have their version set to the PR ID (e.g. "6753").
  16. * If the docs are built for a PR, use the "latest" search index.
  17. * Otherwise, use the search index for the current version.
  18. */
  19. function getReadTheDocsVersion() {
  20. const rtd_version = (window.READTHEDOCS_DATA || {}).version || 'latest'
  21. const version = rtd_version.match(/^\d+$/) ? 'latest' : rtd_version
  22. return version
  23. }
  24. function getVersionFacetFilter() {
  25. return `version:${getReadTheDocsVersion()}`;
  26. }
  27. /**
  28. * Return true (debug: on) for local builds or Read the Docs PR previews.
  29. *
  30. * The debug mode allows inspection of the dropodown.
  31. */
  32. function getSearchDebugMode() {
  33. let debug = false
  34. if (window.READTHEDOCS_DATA === undefined) {
  35. // When developing locally, the `window.READTHEDOCS_DATA` object does not exist.
  36. debug = true
  37. } else {
  38. // When PR preview on Readthedocs, then the version can be converted into
  39. // a number. This does not work for the production version identifiers
  40. // like 'stable', 'latest', 'v2.12', etc. In that case `Number()` is `NaN`.
  41. const versionNumber = Number(window.READTHEDOCS_DATA.version)
  42. debug = !isNaN(versionNumber)
  43. }
  44. return debug
  45. }
  46. function docSearchReady() {
  47. /**
  48. * Configure Algolia DocSearch.
  49. * See https://github.com/algolia/docsearch-configs/blob/master/configs/wagtail.json for index configuration.
  50. */
  51. const search = docsearch({
  52. apiKey: '8325c57d16798633e29d211c26c7b6f9',
  53. indexName: 'wagtail',
  54. inputSelector: '#searchbox [name="q"]',
  55. algoliaOptions: {
  56. facetFilters: [getVersionFacetFilter()],
  57. },
  58. autocompleteOptions: {
  59. // Do NOT automatically select the first suggestion in the dropdown.
  60. // https://github.com/algolia/autocomplete/blob/45fa32d008620cf52bf4a90530be338543dfba7f/README.md#global-options
  61. autoSelect: false
  62. },
  63. debug: getSearchDebugMode(),
  64. })
  65. // Change page styles when the dropdown is open, to lock scrolling.
  66. search.autocomplete.on('autocomplete:updated', function (event) {
  67. const isOpen = event.target.value.trim() !== '';
  68. document.body.classList.toggle('body--autocomplete-open', isOpen);
  69. });
  70. search.autocomplete.on('autocomplete:closed', function (event) {
  71. document.body.classList.toggle('body--autocomplete-open', false);
  72. });
  73. return search
  74. }
  75. docSearchReady();
  76. </script>
  77. {% endblock %}