Skip to content

Commit 7852703

Browse files
committed
allow configuring autocomplete limit, and default it to 10 instead of 8
1 parent d6292ad commit 7852703

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

assets/js/autocomplete/suggestions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ const SUGGESTION_CATEGORY = {
2121
section: 'section'
2222
}
2323

24+
const DEFAULT_AUTOCOMPLETE_LIMIT = 10
25+
2426
/**
2527
* Returns a list of autocomplete suggestion objects matching the given term.
2628
*
2729
* @param {String} query The query string to search for.
2830
* @param {Number} limit The maximum number of results to return.
2931
* @returns {Suggestion[]} List of suggestions sorted and limited.
3032
*/
31-
export function getSuggestions (query, limit = 8) {
33+
export function getSuggestions (query, explicitLimit = null) {
34+
const limit = explicitLimit || window.autocompleteLimit || DEFAULT_AUTOCOMPLETE_LIMIT || 10
3235
if (isBlank(query)) {
3336
return []
3437
}

assets/js/search-bar.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if (!isEmbedded) {
2626

2727
function initialize () {
2828
addEventListeners()
29+
setAutocompleteLimit()
2930

3031
window.onTogglePreviewClick = function (event, open) {
3132
event.preventDefault()
@@ -59,6 +60,15 @@ export function focusSearchInput () {
5960
searchInput.focus()
6061
}
6162

63+
function setAutocompleteLimit () {
64+
const searchInput = qs(SEARCH_INPUT_SELECTOR)
65+
const autocompleteLimit = parseInt(document.querySelector('meta[name="exdoc:autocomplete-limit"]').content)
66+
if (autocompleteLimit) {
67+
window.autocompleteLimit = autocompleteLimit
68+
}
69+
searchInput.setAttribute('autocomplete-limit', autocompleteLimit)
70+
}
71+
6272
function addEventListeners () {
6373
const searchInput = qs(SEARCH_INPUT_SELECTOR)
6474

assets/test/autocomplete/suggestions.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('getSuggestions', () => {
123123
})
124124

125125
it('returns max 8 results', () => {
126-
expect(getSuggestions('e').length).to.eql(8)
126+
expect(getSuggestions('e').length).to.eql(10)
127127
})
128128

129129
it('returns no results if no match found', () => {

lib/mix/tasks/docs.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ defmodule Mix.Tasks.Docs do
365365
366366
* `exdoc:autocomplete` - when set to "off", it disables autocompletion.
367367
368+
* `exdoc:autocomplete-limit` - Set to an integer to configure how many results
369+
appear in the autocomplete dropdown. Defaults to 10.
370+
368371
* `exdoc:full-text-search-url` - the URL to use when performing full text
369372
search. The search string will be appended to the URL as an encoded
370373
parameter. You could use this to bring a custom search engine to your

0 commit comments

Comments
 (0)