-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Here's what I did to add a custom button to my sphinx-book-theme:
- Add a
js/customAlgoliaDocSearchBtn.jscontaining:
// #########################################
// _static/js/customAlgoliaDocSearchBtn.js
// - This script allows .
// - This script should be loaded deferred
// - Initial template suggestion thanks from yafimvo @
// https://github.com/jupyter-book/jupyter-book/issues/1324#issuecomment-1512757574
// #########################################
// Declare the selector for the search button
const SEARCH_BUTTON_SELECTOR = '.search-button__button';
const searchButtons = document.querySelectorAll(SEARCH_BUTTON_SELECTOR);
// On click, for all search bars found, emulate CTRL+K to bring up search UI for both buttons
searchButtons.forEach(button => {
button.addEventListener('click', () => {
console.log('[js/customAlgoliaDocSearchBtn.js] Clicked search btn -> emulating CTRL+K');
const event = new KeyboardEvent('keydown', {
key: 'k',
ctrlKey: true,
bubbles: true // Allows the event to propagate up through the DOM
});
document.dispatchEvent(event);
});
});- In
conf.py, add:
html_js_files.append(("js/customAlgoliaDocSearchBtn.js", {"defer": "defer"}))I forgot if I did anything else, but that's just about it. You could wrap this in your extension and allow a custom button that'll just simply trigger a CTLR+K to hook into your existing code. You'd just need a conf.py var that replaces the current SEARCH_BUTTON_SELECTOR for it to magically work.
Metadata
Metadata
Assignees
Labels
No labels