-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
DOC use Algolia for the search bar #29666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Charlie-XIAO
wants to merge
19
commits into
scikit-learn:main
Choose a base branch
from
Charlie-XIAO:algolia
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3ea0588
DOC implement algolia docsearch
Charlie-XIAO b15c1fb
Merge remote-tracking branch 'upstream/main' into algolia
Charlie-XIAO 3d39672
rename and improve details
Charlie-XIAO 0b702da
some minor improvements
Charlie-XIAO d424926
fix switch between algolia and sphinx search
Charlie-XIAO 5200f2a
Merge remote-tracking branch 'upstream/main' into algolia
Charlie-XIAO e4e2a21
ci
Charlie-XIAO 766471d
built on top of glemaitre's work
Charlie-XIAO 46c74ad
versioning + typo fix
Charlie-XIAO b566fcb
Merge remote-tracking branch 'upstream/main' into algolia
Charlie-XIAO 85f478b
less results per page; longer snippet length
Charlie-XIAO 10a4c64
Merge remote-tracking branch 'upstream/main' into algolia
Charlie-XIAO 12e69b4
navigate to all results page on Enter instead of going to the search …
Charlie-XIAO 96e30ab
navigation when there are no hits
Charlie-XIAO 2d3eb64
workaround for not showing lvl0
glemaitre 287d600
Merge remote-tracking branch 'upstream/main' into algolia
Charlie-XIAO a5693b8
minor css edit
Charlie-XIAO bd1ec76
Merge branch 'main' into algolia
Charlie-XIAO 30921f0
pydata-sphinx-theme have integrated the docsearch:version metadata
Charlie-XIAO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* This script is used initialize Algolia DocSearch on the Algolia search page. It will | ||
* hydrate the search page (see `doc/templates/search.html`) and activate the search | ||
* functionalities. | ||
*/ | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
let timer; | ||
const timeout = 500; // Debounce search-as-you-type | ||
|
||
const searchClient = algoliasearch( | ||
SKLEARN_ALGOLIA_APP_ID, | ||
SKLEARN_ALGOLIA_API_KEY | ||
); | ||
|
||
const search = instantsearch({ | ||
indexName: SKLEARN_ALGOLIA_INDEX_NAME, | ||
initialUiState: { | ||
[SKLEARN_ALGOLIA_INDEX_NAME]: { | ||
query: new URLSearchParams(window.location.search).get("q") || "", | ||
}, | ||
}, | ||
searchClient, | ||
}); | ||
|
||
search.addWidgets([ | ||
// The powered-by widget as the heading | ||
instantsearch.widgets.poweredBy({ | ||
container: "#docsearch-powered-by-light", | ||
theme: "light", | ||
}), | ||
instantsearch.widgets.poweredBy({ | ||
container: "#docsearch-powered-by-dark", | ||
theme: "dark", | ||
}), | ||
// The search input box | ||
instantsearch.widgets.searchBox({ | ||
container: "#docsearch-container", | ||
placeholder: "Search the docs ...", | ||
autofocus: true, | ||
// Debounce the search input to avoid making too many requests | ||
queryHook(query, refine) { | ||
clearTimeout(timer); | ||
timer = setTimeout(() => refine(query), timeout); | ||
}, | ||
}), | ||
// The search statistics before the list of results | ||
instantsearch.widgets.stats({ | ||
container: "#docsearch-stats", | ||
templates: { | ||
text: (data, { html }) => { | ||
if (data.query === "") { | ||
return ""; | ||
} | ||
|
||
let count; | ||
if (data.hasManyResults) { | ||
count = `${data.nbHits} results`; | ||
} else if (data.hasOneResult) { | ||
count = "1 result"; | ||
} else { | ||
count = "no results"; | ||
} | ||
|
||
const stats = `Search finished, found ${count} matching the search query in ${data.processingTimeMS}ms.`; | ||
return html` | ||
<div class="sk-search-stats-heading">Search Results</div> | ||
<p class="sk-search-stats">${stats}</p> | ||
`; | ||
}, | ||
}, | ||
}), | ||
// The list of search results | ||
instantsearch.widgets.infiniteHits({ | ||
container: "#docsearch-hits", | ||
transformItems: (items, { results }) => { | ||
if (results.query === "") { | ||
return []; | ||
} | ||
return items; | ||
}, | ||
templates: { | ||
item: (hit, { html, components }) => { | ||
const hierarchy = Object.entries(hit._highlightResult.hierarchy); | ||
const lastKey = hierarchy[hierarchy.length - 1][0]; | ||
|
||
const sharedHTML = html` | ||
<a class="sk-search-item-header" href="${hit.url}"> | ||
${components.Highlight({ | ||
hit, | ||
attribute: `hierarchy.${lastKey}`, | ||
})} | ||
</a> | ||
<div class="sk-search-item-path"> | ||
${components.Highlight({ hit, attribute: "hierarchy.lvl0" })} | ||
${hierarchy.slice(1, -1).map(([key, _]) => { | ||
return html` | ||
<span class="sk-search-item-path-divider">»</span> | ||
${components.Highlight({ | ||
hit, | ||
attribute: `hierarchy.${key}`, | ||
})} | ||
`; | ||
})} | ||
</div> | ||
`; | ||
|
||
if (hit.type === "content") { | ||
return html` | ||
${sharedHTML} | ||
<p class="sk-search-item-context"> | ||
${components.Snippet({ hit, attribute: "content" })} | ||
</p> | ||
`; | ||
} else { | ||
return sharedHTML; | ||
} | ||
}, | ||
// We have stats widget that can imply "no results" | ||
empty: () => { | ||
return ""; | ||
}, | ||
}, | ||
}), | ||
// Additional configuration of the widgets | ||
instantsearch.widgets.configure({ | ||
hitsPerPage: 50, | ||
attributesToSnippet: ["content:60"], // Lengthen snippets to show more context | ||
}), | ||
]); | ||
|
||
search.start(); | ||
|
||
// Apart from the loading indicator in the search form, also show loading information | ||
// at the bottom so when clicking on "load more" we also have some feedback | ||
search.on("render", () => { | ||
const container = document.getElementById("docsearch-loading-indicator"); | ||
if (search.status === "stalled") { | ||
container.innerText = "Loading search results..."; | ||
container.style.marginTop = "0.4rem"; | ||
} else { | ||
container.innerText = ""; | ||
container.style.marginTop = "0"; | ||
} | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* This script is used initialize Algolia DocSearch on each page. It will hydrate the | ||
* container with ID `docsearch` (see `doc/templates/algolia-searchbox.html`) with the | ||
* Algolia search widget. | ||
*/ | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
// Figure out how to route to the search page from the current page where we will show | ||
// all search results | ||
const pagename = DOCUMENTATION_OPTIONS.pagename; | ||
let searchPageHref = "./"; | ||
for (let i = 0; i < pagename.split("/").length - 1; i++) { | ||
searchPageHref += "../"; | ||
} | ||
searchPageHref += "algolia-search.html"; | ||
|
||
// Function to navigate to the all results page | ||
const navigateToResultsPage = () => { | ||
const link = document.getElementById("sk-search-all-results-link"); | ||
if (link !== null) { | ||
// If there is the "see all results" link, just click it | ||
link.click(); | ||
return; | ||
} | ||
|
||
const inputBox = document.getElementById("docsearch-input"); | ||
if (inputBox === null || inputBox.value === "") { | ||
// If we cannot get the input box or the input box is empty, navigate to the | ||
// all results page with no query | ||
window.location.assign(searchPageHref); | ||
return; | ||
} | ||
// Navigate to the all results page with query constructed from the input | ||
const query = new URLSearchParams({ q: inputBox.value }); | ||
window.location.assign(`${searchPageHref}?${query}`); | ||
}; | ||
|
||
// Initialize the Algolia DocSearch widget | ||
docsearch({ | ||
container: "#docsearch", | ||
appId: SKLEARN_ALGOLIA_APP_ID, | ||
apiKey: SKLEARN_ALGOLIA_API_KEY, | ||
indexName: SKLEARN_ALGOLIA_INDEX_NAME, | ||
placeholder: "Search the docs ...", | ||
searchParameters: { attributesToHighlight: ["hierarchy.lvl0"] }, | ||
// Redirect to the search page with the corresponding query | ||
resultsFooterComponent: ({ state }) => ({ | ||
type: "a", | ||
ref: undefined, | ||
constructor: undefined, | ||
key: state.query, | ||
props: { | ||
id: "sk-search-all-results-link", | ||
href: `${searchPageHref}?q=${state.query}`, | ||
children: `Check all results...`, | ||
}, | ||
__v: null, | ||
}), | ||
navigator: { | ||
// Hack implementation to navigate to the search page instead of navigating to the | ||
// corresponding search result page; `navigateNewTab` and `navigateNewWindow` are | ||
// still left as the default behavior | ||
navigate: navigateToResultsPage, | ||
}, | ||
}); | ||
|
||
// The navigator API only works when there are search results; there are cases where | ||
// there are no hits, e.g. empty query + no history, in which case we need to manually | ||
// listen to the Enter key | ||
document.addEventListener("keydown", (e) => { | ||
if ( | ||
e.key === "Enter" && | ||
!e.shiftKey && | ||
!e.ctrlKey && | ||
!e.metaKey && | ||
!e.altKey | ||
) { | ||
const container = document.querySelector( | ||
".DocSearch.DocSearch-Container" | ||
); | ||
if (container === null) { | ||
return; | ||
} | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
navigateToResultsPage(); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently there is a bug that explain why I could not get the desired rendering in the instant box:
algolia/docsearch#2294