Skip to content

Commit 8b3cd58

Browse files
authored
feat: add algolia search support (#34)
1 parent a292525 commit 8b3cd58

File tree

11 files changed

+522
-175
lines changed

11 files changed

+522
-175
lines changed

.changeset/witty-carrots-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/doom": minor
3+
---
4+
5+
feat: add algolia search support

docs/public/Robots.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Algolia-Crawler-Verif: 7A640E13F04C80C2

doom.config.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
lang: zh
22
title: Doom
33
logoText: Doom
4+
themeConfig:
5+
socialLinks:
6+
- icon: github
7+
mode: link
8+
content: https://github.com/alauda/doom
9+
410
api:
511
crds:
612
- docs/shared/crds/*.yaml
@@ -25,8 +31,7 @@ lint:
2531
cspell:
2632
words:
2733
- katanomi
28-
themeConfig:
29-
socialLinks:
30-
- icon: github
31-
mode: link
32-
content: https://github.com/alauda/doom
34+
algolia:
35+
appId:
36+
apiKey:
37+
indexName:

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"./package.json": "./package.json",
1616
"./config": "./lib/config.js",
1717
"./runtime": "./lib/runtime/index.js",
18+
"./theme": "./lib/theme.js",
1819
"./types": "./lib/types.js"
1920
},
2021
"files": [
@@ -64,6 +65,7 @@
6465
"@rsbuild/plugin-sass": "^1.3.1",
6566
"@rsbuild/plugin-yaml": "^1.0.2",
6667
"@rspress/core": "2.0.0-beta.8",
68+
"@rspress/plugin-algolia": "2.0.0-beta.8",
6769
"@shikijs/transformers": "^3.4.2",
6870
"chokidar": "^4.0.3",
6971
"cli-progress": "^3.12.0",
@@ -96,7 +98,7 @@
9698
"swagger2openapi": "^7.0.8",
9799
"tinyglobby": "^0.2.14",
98100
"type-fest": "^4.41.0",
99-
"typescript-eslint": "^8.32.1",
101+
"typescript-eslint": "^8.33.0",
100102
"x-fetch": "^0.2.6",
101103
"yaml": "^2.8.0",
102104
"yoctocolors": "^2.1.1"
@@ -110,7 +112,7 @@
110112
"@total-typescript/ts-reset": "^0.6.1",
111113
"@types/cli-progress": "^3.11.6",
112114
"@types/ejs": "^3.1.5",
113-
"@types/node": "^22.15.21",
115+
"@types/node": "^22.15.23",
114116
"@types/picomatch": "^4.0.0",
115117
"@types/react": "^19.1.6",
116118
"@types/react-dom": "^19.1.5",

src/cli/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ program
103103
FALSY_VALUES.has(value) ? false : TRUTHY_VALUES.has(value) || value,
104104
false,
105105
)
106+
.option(
107+
'-a, --algolia',
108+
'Whether to enable Algolia search',
109+
parseBoolean,
110+
false,
111+
)
106112
.option(
107113
'-n, --no-open [boolean]',
108114
'Do not open the browser after starting the server',

src/cli/load-config.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const getCommonConfig = async ({
114114
exclude,
115115
redirect,
116116
editRepo,
117+
algolia,
117118
}: {
118119
config: UserConfig
119120
configFilePath?: string
@@ -130,6 +131,7 @@ const getCommonConfig = async ({
130131
exclude?: string[]
131132
redirect?: 'auto' | 'never' | 'only-default-lang'
132133
editRepo?: boolean | string
134+
algolia?: boolean
133135
}): Promise<UserConfig> => {
134136
const fallbackToZh = 'lang' in config && !config.lang
135137
root = resolveDocRoot(CWD, root, config.root)
@@ -285,12 +287,30 @@ const getCommonConfig = async ({
285287
open,
286288
},
287289
tools: {
288-
rspack: {
289-
resolve: {
290-
extensionAlias: {
291-
'.js': ['.ts', '.tsx', '.js'],
290+
rspack(rspackConfig, { mergeConfig, rspack }) {
291+
return mergeConfig(rspackConfig, {
292+
resolve: {
293+
extensionAlias: {
294+
'.js': ['.ts', '.tsx', '.js'],
295+
},
292296
},
293-
},
297+
plugins:
298+
algolia && config.algolia
299+
? [
300+
new rspack.DefinePlugin({
301+
'process.env.ALGOLIA_APP_ID': JSON.stringify(
302+
config.algolia.appId,
303+
),
304+
'process.env.ALGOLIA_API_KEY': JSON.stringify(
305+
config.algolia.apiKey,
306+
),
307+
'process.env.ALGOLIA_INDEX_NAME': JSON.stringify(
308+
config.algolia.indexName,
309+
),
310+
}),
311+
]
312+
: undefined,
313+
})
294314
},
295315
},
296316
},
@@ -323,6 +343,7 @@ export async function loadConfig(
323343
outDir,
324344
redirect,
325345
editRepo,
346+
algolia,
326347
}: GlobalCliOptions = {},
327348
): Promise<{
328349
config: UserConfig
@@ -402,6 +423,7 @@ export async function loadConfig(
402423
exclude,
403424
redirect,
404425
editRepo,
426+
algolia,
405427
})
406428

407429
base = commonConfig.base!

src/theme.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Search as OriginalSearch } from '@rspress/core/theme'
2+
import { Search as AlgoliaSearch } from '@rspress/plugin-algolia/runtime'
3+
4+
const Search =
5+
process.env.ALGOLIA_APP_ID &&
6+
process.env.ALGOLIA_API_KEY &&
7+
process.env.ALGOLIA_INDEX_NAME
8+
? () => (
9+
<AlgoliaSearch
10+
docSearchProps={{
11+
appId: process.env.ALGOLIA_APP_ID!,
12+
apiKey: process.env.ALGOLIA_API_KEY!,
13+
indexName: process.env.ALGOLIA_INDEX_NAME!,
14+
}}
15+
/>
16+
)
17+
: OriginalSearch
18+
19+
// eslint-disable-next-line import-x/export
20+
export * from '@rspress/core/theme'
21+
// eslint-disable-next-line import-x/export
22+
export { Search }

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface GlobalCliOptions {
3030
outDir?: string
3131
redirect?: 'auto' | 'never' | 'only-default-lang'
3232
editRepo?: boolean | string
33+
algolia?: boolean
3334
}
3435

3536
export interface TranslateOptions {
@@ -41,6 +42,12 @@ export interface LintOptions {
4142
cspellOptions?: Partial<Options>
4243
}
4344

45+
export interface AlgoliaOptions {
46+
appId: string
47+
apiKey: string
48+
indexName: string
49+
}
50+
4451
declare module '@rspress/shared' {
4552
interface UserConfig {
4653
prefix?: string
@@ -56,6 +63,7 @@ declare module '@rspress/shared' {
5663
translate?: TranslateOptions
5764
editRepoBaseUrl?: string
5865
lint?: LintOptions
66+
algolia?: AlgoliaOptions
5967
}
6068

6169
interface SiteData {

theme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@alauda/doom/theme'

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"target": "ESNext",
1313
"paths": {
1414
"@alauda/doom/config": ["./src/config.ts"],
15-
"@alauda/doom/runtime": ["./src/runtime/index.ts"]
15+
"@alauda/doom/runtime": ["./src/runtime/index.ts"],
16+
"@alauda/doom/theme": ["./src/theme.tsx"]
1617
},
1718
"stripInternal": true,
1819
"verbatimModuleSyntax": true

0 commit comments

Comments
 (0)