From 785270320836949ef616fb7a728417c8ada7193c Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 1 Jun 2025 12:57:23 -0400 Subject: [PATCH 1/2] allow configuring autocomplete limit, and default it to 10 instead of 8 --- assets/js/autocomplete/suggestions.js | 5 ++++- assets/js/search-bar.js | 10 ++++++++++ assets/test/autocomplete/suggestions.spec.js | 2 +- lib/mix/tasks/docs.ex | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/assets/js/autocomplete/suggestions.js b/assets/js/autocomplete/suggestions.js index e21777227..45ec3609e 100644 --- a/assets/js/autocomplete/suggestions.js +++ b/assets/js/autocomplete/suggestions.js @@ -21,6 +21,8 @@ const SUGGESTION_CATEGORY = { section: 'section' } +const DEFAULT_AUTOCOMPLETE_LIMIT = 10 + /** * Returns a list of autocomplete suggestion objects matching the given term. * @@ -28,7 +30,8 @@ const SUGGESTION_CATEGORY = { * @param {Number} limit The maximum number of results to return. * @returns {Suggestion[]} List of suggestions sorted and limited. */ -export function getSuggestions (query, limit = 8) { +export function getSuggestions (query, explicitLimit = null) { + const limit = explicitLimit || window.autocompleteLimit || DEFAULT_AUTOCOMPLETE_LIMIT || 10 if (isBlank(query)) { return [] } diff --git a/assets/js/search-bar.js b/assets/js/search-bar.js index 2e50e04e4..9d57b07c3 100644 --- a/assets/js/search-bar.js +++ b/assets/js/search-bar.js @@ -26,6 +26,7 @@ if (!isEmbedded) { function initialize () { addEventListeners() + setAutocompleteLimit() window.onTogglePreviewClick = function (event, open) { event.preventDefault() @@ -59,6 +60,15 @@ export function focusSearchInput () { searchInput.focus() } +function setAutocompleteLimit () { + const searchInput = qs(SEARCH_INPUT_SELECTOR) + const autocompleteLimit = parseInt(document.querySelector('meta[name="exdoc:autocomplete-limit"]').content) + if (autocompleteLimit) { + window.autocompleteLimit = autocompleteLimit + } + searchInput.setAttribute('autocomplete-limit', autocompleteLimit) +} + function addEventListeners () { const searchInput = qs(SEARCH_INPUT_SELECTOR) diff --git a/assets/test/autocomplete/suggestions.spec.js b/assets/test/autocomplete/suggestions.spec.js index 1f2357a9f..62b787341 100644 --- a/assets/test/autocomplete/suggestions.spec.js +++ b/assets/test/autocomplete/suggestions.spec.js @@ -123,7 +123,7 @@ describe('getSuggestions', () => { }) it('returns max 8 results', () => { - expect(getSuggestions('e').length).to.eql(8) + expect(getSuggestions('e').length).to.eql(10) }) it('returns no results if no match found', () => { diff --git a/lib/mix/tasks/docs.ex b/lib/mix/tasks/docs.ex index 9a6389590..6e1d15edd 100644 --- a/lib/mix/tasks/docs.ex +++ b/lib/mix/tasks/docs.ex @@ -365,6 +365,9 @@ defmodule Mix.Tasks.Docs do * `exdoc:autocomplete` - when set to "off", it disables autocompletion. + * `exdoc:autocomplete-limit` - Set to an integer to configure how many results + appear in the autocomplete dropdown. Defaults to 10. + * `exdoc:full-text-search-url` - the URL to use when performing full text search. The search string will be appended to the URL as an encoded parameter. You could use this to bring a custom search engine to your From 00ac7e227f4e73a828bcd2319dce422d7dd89292 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 1 Jun 2025 13:32:28 -0400 Subject: [PATCH 2/2] Update assets/js/autocomplete/suggestions.js --- assets/js/autocomplete/suggestions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/autocomplete/suggestions.js b/assets/js/autocomplete/suggestions.js index 45ec3609e..7733da49b 100644 --- a/assets/js/autocomplete/suggestions.js +++ b/assets/js/autocomplete/suggestions.js @@ -31,7 +31,7 @@ const DEFAULT_AUTOCOMPLETE_LIMIT = 10 * @returns {Suggestion[]} List of suggestions sorted and limited. */ export function getSuggestions (query, explicitLimit = null) { - const limit = explicitLimit || window.autocompleteLimit || DEFAULT_AUTOCOMPLETE_LIMIT || 10 + const limit = explicitLimit || window.autocompleteLimit || DEFAULT_AUTOCOMPLETE_LIMIT if (isBlank(query)) { return [] }