Skip to content

Commit 4f68ebb

Browse files
feat(docusaurus-adapter): support docsearch key and adapter docs (#2858)
* feat(docusaurus-adapter): Support docsearch key and adapter docs * chore(release): include docusaurus adapter in shipjs publish list * chore(docusaurus-adapter): remove libtmp and pin initial version * fix: issue with footer * fix: contextuals * fix all ts errors * fix: lint * fix lint
1 parent 6933434 commit 4f68ebb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3859
-147
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lib
2+
libtmp
3+
*.tsbuildinfo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.tsbuildinfo*
2+
tsconfig*
3+
__tests__
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `@docsearch/docusaurus-adapter`
2+
3+
Algolia search component for Docusaurus.
4+
5+
## Usage
6+
7+
Prefer configuring the adapter with `themeConfig.docsearch`:
8+
9+
```js
10+
// docusaurus.config.js
11+
export default {
12+
// ...
13+
themeConfig: {
14+
docsearch: {
15+
appId: 'APP_ID',
16+
apiKey: 'SEARCH_API_KEY',
17+
indexName: 'INDEX_NAME',
18+
askAi: {
19+
assistantId: 'ASSISTANT_ID',
20+
sidePanel: true,
21+
},
22+
},
23+
},
24+
};
25+
```
26+
27+
`themeConfig.algolia` is still supported as a backward-compatible alias.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"name": "@docsearch/docusaurus-adapter",
3+
"version": "4.5.4",
4+
"description": "Algolia search component for Docusaurus.",
5+
"main": "lib/index.js",
6+
"sideEffects": [
7+
"*.css"
8+
],
9+
"exports": {
10+
"./client": {
11+
"types": "./lib/client/index.d.ts",
12+
"default": "./lib/client/index.js"
13+
},
14+
".": {
15+
"types": "./src/theme-search-algolia.d.ts",
16+
"default": "./lib/index.js"
17+
}
18+
},
19+
"types": "src/theme-search-algolia.d.ts",
20+
"publishConfig": {
21+
"access": "public"
22+
},
23+
"repository": {
24+
"type": "git",
25+
"url": "https://github.com/algolia/docsearch.git",
26+
"directory": "adapters/docusaurus-theme-search-algolia"
27+
},
28+
"license": "MIT",
29+
"scripts": {
30+
"build": "yarn exec tsc --build --force && node ./scripts/copy-assets.mjs && node ./scripts/format-theme.mjs",
31+
"build:clean": "yarn clean && yarn build",
32+
"clean": "rm -rf lib tsconfig.*.tsbuildinfo",
33+
"watch": "run-p -c copy:watch build:watch",
34+
"build:watch": "yarn exec tsc --build --watch",
35+
"copy:watch": "node ./scripts/copy-assets.mjs --watch"
36+
},
37+
"dependencies": {
38+
"@docsearch/react": "^4.5.3",
39+
"@docusaurus/core": "^3.9.2",
40+
"@docusaurus/plugin-content-docs": "^3.9.2",
41+
"@docusaurus/theme-common": "^3.9.2",
42+
"@docusaurus/theme-translations": "^3.9.2",
43+
"algoliasearch": "^5.37.0",
44+
"algoliasearch-helper": "^3.26.0",
45+
"clsx": "^2.0.0",
46+
"eta": "^2.2.0",
47+
"fs-extra": "^11.1.1",
48+
"joi": "^17.9.2",
49+
"lodash": "^4.17.21",
50+
"tslib": "^2.6.0",
51+
"utility-types": "^3.10.0"
52+
},
53+
"devDependencies": {
54+
"@docusaurus/core": "^3.9.2",
55+
"@docusaurus/module-type-aliases": "3.9.2",
56+
"@docusaurus/theme-classic": "3.9.2",
57+
"@types/fs-extra": "^11.0.4",
58+
"@types/lodash": "^4.17.10",
59+
"typescript": "5.7.3"
60+
},
61+
"peerDependencies": {
62+
"react": "^18.0.0 || ^19.0.0",
63+
"react-dom": "^18.0.0 || ^19.0.0"
64+
},
65+
"engines": {
66+
"node": ">=20.0"
67+
}
68+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import fse from 'fs-extra';
5+
6+
const WATCH_FLAG = '--watch';
7+
const ASSET_EXTENSIONS = new Set(['.css']);
8+
const IGNORED_EXTENSIONS = new Set(['.ts', '.tsx', '.d.ts']);
9+
10+
const cwd = process.cwd();
11+
const srcRoot = path.join(cwd, 'src', 'theme');
12+
const destRoot = path.join(cwd, 'lib', 'theme');
13+
14+
async function copyAssetFile(filePath) {
15+
const relativePath = path.relative(srcRoot, filePath);
16+
const destPath = path.join(destRoot, relativePath);
17+
18+
await fse.ensureDir(path.dirname(destPath));
19+
await fse.copyFile(filePath, destPath);
20+
}
21+
22+
async function copyAssetsOnce() {
23+
if (!(await fse.pathExists(srcRoot))) {
24+
return;
25+
}
26+
27+
const entries = await fse.readdir(srcRoot, { recursive: true });
28+
const filePaths = entries
29+
.filter((entry) => typeof entry === 'string')
30+
.map((entry) => path.join(srcRoot, entry))
31+
.filter((entryPath) => fs.statSync(entryPath).isFile());
32+
33+
await Promise.all(
34+
filePaths
35+
.filter((filePath) => {
36+
const extension = path.extname(filePath);
37+
if (IGNORED_EXTENSIONS.has(extension)) {
38+
return false;
39+
}
40+
return ASSET_EXTENSIONS.has(extension);
41+
})
42+
.map((filePath) => copyAssetFile(filePath)),
43+
);
44+
}
45+
46+
function watchAssets() {
47+
if (!fs.existsSync(srcRoot)) {
48+
return;
49+
}
50+
51+
copyAssetsOnce();
52+
53+
fs.watch(srcRoot, { recursive: true }, (_eventType, filename) => {
54+
if (!filename) {
55+
return;
56+
}
57+
const filePath = path.join(srcRoot, filename);
58+
const extension = path.extname(filePath);
59+
if (IGNORED_EXTENSIONS.has(extension) || !ASSET_EXTENSIONS.has(extension)) {
60+
return;
61+
}
62+
if (!fs.existsSync(filePath)) {
63+
return;
64+
}
65+
copyAssetFile(filePath);
66+
});
67+
}
68+
69+
if (process.argv.includes(WATCH_FLAG)) {
70+
watchAssets();
71+
} else {
72+
copyAssetsOnce();
73+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { execSync } from 'node:child_process';
2+
3+
import { glob } from 'glob';
4+
5+
const pattern = 'lib/theme/**/*.js';
6+
const files = glob.sync(pattern);
7+
8+
if (files.length > 0) {
9+
try {
10+
execSync(`prettier --config ../../.prettierrc --write "${pattern}"`, {
11+
stdio: 'inherit',
12+
});
13+
} catch (error) {
14+
throw new Error(`Prettier failed: ${error instanceof Error ? error.message : String(error)}`);
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. And its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import assert from 'node:assert/strict';
9+
10+
import { describe, it } from 'vitest';
11+
12+
import { mergeFacetFilters } from '../client/utils';
13+
14+
describe('mergeFacetFilters', () => {
15+
it('merges [string,string]', () => {
16+
assert.deepStrictEqual(mergeFacetFilters('f1', 'f2'), ['f1', 'f2']);
17+
});
18+
19+
it('merges [string,array]', () => {
20+
assert.deepStrictEqual(mergeFacetFilters('f1', ['f2', 'f3']), ['f1', 'f2', 'f3']);
21+
});
22+
23+
it('merges [string,undefined]', () => {
24+
assert.deepStrictEqual(mergeFacetFilters('f1', undefined), 'f1');
25+
});
26+
27+
it('merges [undefined,string]', () => {
28+
assert.deepStrictEqual(mergeFacetFilters(undefined, 'f1'), 'f1');
29+
});
30+
31+
it('merges [array,undefined]', () => {
32+
assert.deepStrictEqual(mergeFacetFilters(['f1', 'f2'], undefined), ['f1', 'f2']);
33+
});
34+
35+
it('merges [undefined,array]', () => {
36+
assert.deepStrictEqual(mergeFacetFilters(undefined, ['f1', 'f2']), ['f1', 'f2']);
37+
});
38+
39+
it('merges [array,array]', () => {
40+
assert.deepStrictEqual(mergeFacetFilters(['f1'], ['f2']), ['f1', 'f2']);
41+
42+
assert.deepStrictEqual(mergeFacetFilters(['f1', 'f2'], ['f3', 'f4']), ['f1', 'f2', 'f3', 'f4']);
43+
});
44+
});

0 commit comments

Comments
 (0)