Skip to content

Commit 562f6b0

Browse files
committed
feat: integrate Algolia search and improve code block styling in docs
1 parent 4ec8975 commit 562f6b0

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

docusaurus.config.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ const config = {
106106
},
107107
],
108108
],
109-
110109
themeConfig: {
111110
image: '/img/defang-social-card-2.png',
112111
navbar: {
@@ -205,6 +204,39 @@ const config = {
205204
theme: lightCodeTheme,
206205
darkTheme: darkCodeTheme,
207206
},
207+
algolia: {
208+
// The application ID provided by Algolia
209+
appId: 'O3VFCOSSZ6',
210+
211+
// Public API key: it is safe to commit it
212+
apiKey: '43fd4ebb5bb9875aa8764793ff9a09ff',
213+
214+
indexName: 'Defang Docs',
215+
216+
// Optional: see doc section below
217+
contextualSearch: true,
218+
219+
// Optional: Specify domains where the navigation should occur through window.location instead on history.push. Useful when our Algolia config crawls multiple documentation sites and we want to navigate with window.location.href to them.
220+
externalUrlRegex: 'external\\.com|domain\\.com',
221+
222+
// Optional: Replace parts of the item URLs from Algolia. Useful when using the same search index for multiple deployments using a different baseUrl. You can use regexp or string in the `from` param. For example: localhost:3000 vs myCompany.com/docs
223+
replaceSearchResultPathname: {
224+
from: '/docs/', // or as RegExp: /\/docs\//
225+
to: '/',
226+
},
227+
228+
// Optional: Algolia search parameters
229+
searchParameters: {},
230+
231+
// Optional: path for search page that enabled by default (`false` to disable it)
232+
searchPagePath: 'search',
233+
234+
// Optional: whether the insights feature is enabled or not on Docsearch (`false` by default)
235+
insights: false,
236+
237+
// Optional: whether you want to use the new Ask AI feature (undefined by default)
238+
askAi: 'MUO629LjH0L5',
239+
},
208240
},
209241
plugins: [
210242
async function shadcnTailwindPlugin() {
@@ -221,7 +253,6 @@ const config = {
221253
},
222254
};
223255
},
224-
require.resolve('docusaurus-lunr-search'),
225256
[
226257
'@docusaurus/plugin-client-redirects',
227258
{

src/css/custom.css

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
--ifm-code-background: var(--prism-background-color);
6262
--ifm-pre-background: var(--prism-background-color);
6363
--ifm-footer-background-color: transparent;
64-
--ifm-code-border-radius: 1.25rem;
6564
--ifm-tabs-border-bottom: 1px solid hsla(var(--border) / 0.7);
6665
--ifm-tabs-border-radius: 0.875rem;
6766
--ifm-footer-title-color: hsl(var(--foreground));
@@ -392,10 +391,20 @@ img.unstyled {
392391
/**
393392
* Page Specific
394393
*/
395-
[class*="docs-doc-id-intro/intro"] .theme-doc-markdown .card h2 {
394+
.theme-doc-markdown .card h2 {
396395
@apply text-2xl;
397396
}
398397

399398
.navbar__item.navbar__link.navbar__item-get_started {
400399
@apply hidden sm:block;
401-
}
400+
}
401+
402+
/* selector for code tags with no classes */
403+
code:not([class]),
404+
code[class=""] {
405+
@apply rounded-[0.5rem] bg-muted px-1.5 py-0.5;
406+
}
407+
pre code {
408+
@apply bg-white dark:bg-card dark:border dark:border-solid dark:border-gray-600;
409+
}
410+

src/theme/Layout/index.js

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,61 @@ export default function LayoutWrapper(props) {
1111
}, [location]);
1212

1313
useEffect(() => {
14-
const search = window.document.getElementById("search_input_react");
15-
search.onchange = (e) => {
16-
if (searchTrackingTimeout) {
17-
clearTimeout(searchTrackingTimeout);
14+
if (typeof window === "undefined") {
15+
return undefined;
16+
}
17+
18+
const attachSearchHandler = () => {
19+
const search =
20+
window.document.getElementById("docsearch-input") ??
21+
window.document.getElementById("search_input_react");
22+
23+
if (!search) {
24+
return undefined;
1825
}
19-
searchTrackingTimeout = setTimeout(() => {
20-
window?.analytics?.track("docsSearch", {
21-
searchQuery: e.target.value,
22-
});
23-
}, 1000);
26+
27+
const handleChange = (event) => {
28+
if (searchTrackingTimeout) {
29+
clearTimeout(searchTrackingTimeout);
30+
}
31+
searchTrackingTimeout = setTimeout(() => {
32+
window?.analytics?.track("docsSearch", {
33+
searchQuery: event.target.value,
34+
});
35+
}, 1000);
36+
};
37+
38+
search.addEventListener("change", handleChange);
39+
40+
return () => {
41+
search.removeEventListener("change", handleChange);
42+
if (searchTrackingTimeout) {
43+
clearTimeout(searchTrackingTimeout);
44+
}
45+
};
46+
};
47+
48+
let cleanupHandler = attachSearchHandler();
49+
50+
if (cleanupHandler) {
51+
return cleanupHandler;
2452
}
53+
54+
const observer = new MutationObserver(() => {
55+
if (!cleanupHandler) {
56+
cleanupHandler = attachSearchHandler();
57+
}
58+
if (cleanupHandler) {
59+
observer.disconnect();
60+
}
61+
});
62+
63+
observer.observe(window.document.body, { childList: true, subtree: true });
64+
65+
return () => {
66+
observer.disconnect();
67+
cleanupHandler?.();
68+
};
2569
}, []);
2670

2771
return (

0 commit comments

Comments
 (0)