Skip to content

Commit

Permalink
Keep default Search.init behavior if addons injected
Browse files Browse the repository at this point in the history
The extension removes the call to `Search.init` because it needs to perform some
injection before calling this method. The old implementation of the
`readthedocs-doc-embed.js` calls this method manually after modifying some
stuff.

However, with the introduction of addons, we don't want to manipulate the
defaults and the Sphinx default search should work out-of-the-box.

To keep the default behavior when addons is enabled, we check for addons
javascript and if it's present we call `_ready(Search.init);` as the default
Sphinx code does.

See readthedocs/addons#213 for more information.
  • Loading branch information
humitos committed Dec 12, 2023
1 parent 614109c commit ce1b8bf
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion readthedocs_ext/readthedocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,15 @@ def remove_search_init(app, exception):
)

if os.path.exists(searchtools_file):
replacement_text = '/* Search initialization removed for Read the Docs */'
replacement_text = """
/* Search initialization manipulated by Read the Docs */
/* See https://github.com/readthedocs/addons/issues/213 for more information */
const addonsInjected = document.querySelector('script[src="/_/static/javascript/readthedocs-addons.js"]');
if (addonsInjected) {
_ready(Sphinx.init);
}
"""
replacement_regex = re.compile(
r'''
^(\$\(document\).ready\(function\s*\(\)\s*{(?:\n|\r\n?)
Expand Down

0 comments on commit ce1b8bf

Please sign in to comment.