Skip to content

Commit 7b2d10f

Browse files
committed
feat: add algolia search
1 parent 35ba9d7 commit 7b2d10f

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

mkdocs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ plugins:
7070
- tags:
7171
tags_file: tags.md
7272

73+
algolia:
74+
app_id: !ENV ALGOLIA_APP_ID
75+
api_key: !ENV ALGOLIA_API_KEY
76+
index_name: gpac
77+
7378
hooks:
7479
- scripts/mkdocs_hooks.py
7580
markdown_extensions:

overrides/base.html

+51-10
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@
8282
{% endfor %}
8383
{% endif %}
8484
{% block extrahead %}{% endblock %}
85-
86-
87-
85+
{% if config.algolia %}
86+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3" />
87+
<link rel="preconnect" href="https://{{ config.algolia.app_id }}-dsn.algolia.net" crossorigin />
88+
{% endif %}
8889
</head>
8990
{% set direction = config.theme.direction or lang.t("direction") %}
9091
{% if config.theme.palette %}
@@ -396,13 +397,53 @@
396397
{% endblock %}
397398
{% block scripts %}
398399
<script src="{{ 'assets/javascripts/bundle.83f73b43.min.js' | url }}"></script>
399-
400-
<script type="module" src="{{ 'javascripts/main.js' | url }}"></script>
401-
{% for script in config.extra_javascript %}
402-
{{ script | script_tag }}
403-
{% endfor %}
400+
<script type="module" src="{{ 'javascripts/main.js' | url }}"></script>
401+
{% for script in config.extra_javascript %}
402+
{{ script | script_tag }}
403+
{% endfor %}
404404
{% endblock %}
405-
406-
</body>
405+
{% if config.algolia %}
406+
<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@3"></script>
407+
<script type="text/javascript">
408+
document.addEventListener("DOMContentLoaded", function() {
409+
const searchInput = document.querySelector('.md-search');
410+
const algoliaContainer = document.createElement('div');
411+
algoliaContainer.className = 'algolia-container';
412+
document.body.appendChild(algoliaContainer);
413+
414+
if (searchInput) {
415+
// Remove existing event listeners by replacing the element
416+
const newSearchInput = searchInput.cloneNode(true);
417+
searchInput.parentNode.replaceChild(newSearchInput, searchInput);
418+
419+
docsearch({
420+
appId: '{{ config.algolia.app_id }}',
421+
apiKey: '{{ config.algolia.api_key }}',
422+
indexName: '{{ config.algolia.index_name }}',
423+
container: algoliaContainer,
424+
transformItems(items) {
425+
return items.map((item) => ({
426+
...item,
427+
hierarchy: Object.entries(item.hierarchy).reduce((acc, [key, value]) => {
428+
if (value) {
429+
acc[key] = value.trim().replace('¶', '');
430+
} else {
431+
acc[key] = value;
432+
}
433+
return acc;
434+
}, {}),
435+
}
436+
));
437+
}
438+
});
439+
440+
newSearchInput.addEventListener('click', function() {
441+
const algoliaButton = document.querySelector('.algolia-container button');
442+
if (algoliaButton) algoliaButton.click();
443+
});
444+
}
445+
});
446+
</script>
447+
{% endif %}
407448
</body>
408449
</html>

0 commit comments

Comments
 (0)