Skip to content

Commit ee8e447

Browse files
committed
fix(docs): use different algolia indexes as needed
1 parent 4ea4da2 commit ee8e447

File tree

5 files changed

+90
-29
lines changed

5 files changed

+90
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ yarn-error.log*
3939
.env.development.local
4040
.env.test.local
4141
.env.production.local
42+
.env.algolia
4243

4344
# other
4445
*.tsbuildinfo

apps/docs/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NEXT_PUBLIC_ALGOLIA_APP_ID=D8A7D4VBAW
2+
NEXT_PUBLIC_ALGOLIA_API_KEY=9a93702ac5feb1874e6ecc334127c069

apps/docs/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALGOLIA_INDEX_NAME=next_react_md_dev_search

apps/docs/scripts/createEnv.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ const env = {
3232
NEXT_PUBLIC_GITHUB_FILE_LINK: `${githubUrl}/blob/${commit}`,
3333
NEXT_PUBLIC_BRANCH_NAME: branchName,
3434
NEXT_PUBLIC_RMD_VERSION: packageJson.version,
35-
NEXT_PUBLIC_ALGOLIA_APP_ID: "D8A7D4VBAW",
36-
NEXT_PUBLIC_ALGOLIA_API_KEY: "9a93702ac5feb1874e6ecc334127c069",
37-
NEXT_PUBLIC_ALGOLIA_INDEX_NAME: "next_react_md_dev_search",
3835
} satisfies Record<string, string>;
3936

4037
const stringifiedEnv = Object.entries(env)

apps/docs/scripts/updateAlgoliaIndex.ts

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { searchClient } from "@algolia/client-search";
2+
import confirm from "@inquirer/confirm";
23
import { parseMdx } from "docs-generator/scripts/algolia/parseMdx";
34
import {
45
type HeadingWithDescription,
@@ -20,15 +21,13 @@ import { getBlogs } from "../src/app/(main)/(markdown)/blog/data.js";
2021
import { titleCase } from "../src/utils/strings.js";
2122
import { CORE_SRC } from "./constants.js";
2223

23-
const BASE_URL = "https://react-md.dev";
24-
25-
async function indexMdxPages(): Promise<readonly IndexedItem[]> {
24+
async function indexMdxPages(baseUrl: string): Promise<readonly IndexedItem[]> {
2625
const mdxPages = await glob("src/app/**/page.mdx");
2726
return await Promise.all(
2827
mdxPages.map(async (mdxFilePath) => {
2928
try {
3029
return parseMdx({
31-
baseUrl: BASE_URL,
30+
baseUrl,
3231
mdxFilePath,
3332
});
3433
} catch (e) {
@@ -45,7 +44,9 @@ interface SassDocGroup {
4544
variables: Map<string, FormattedVariableItem>;
4645
}
4746

48-
async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
47+
async function indexSassDocPages(
48+
baseUrl: string
49+
): Promise<readonly IndexedItem[]> {
4950
const { mixins, functions, variables } = await generate({ src: CORE_SRC });
5051

5152
const grouped = new Map<string, SassDocGroup>();
@@ -108,7 +109,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
108109
grouped.forEach((item, group) => {
109110
const groupTitle = titleCase(group, "-");
110111
const pathname = `/sassdoc/${getGroupName(group)}`;
111-
const url = `${BASE_URL}${pathname}`;
112+
const url = `${baseUrl}${pathname}`;
112113
items.push({
113114
objectID: url,
114115
url,
@@ -129,7 +130,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
129130
].forEach((sassdocItem) => {
130131
const { name, description, type } = sassdocItem;
131132
const itemPathname = getSassDocLink(sassdocItem);
132-
const url = `${BASE_URL}${itemPathname}`;
133+
const url = `${baseUrl}${itemPathname}`;
133134
const itemType =
134135
type === "mixin" || type === "function" ? type : "variable";
135136

@@ -149,7 +150,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
149150
return items;
150151
}
151152

152-
async function indexBlogs(): Promise<readonly IndexedItem[]> {
153+
async function indexBlogs(baseUrl: string): Promise<readonly IndexedItem[]> {
153154
const items: IndexedItem[] = [];
154155
const blogRoot = resolve(process.cwd(), "src/app/(main)/(markdown)/blog");
155156
const blogs = await getBlogs(blogRoot);
@@ -169,8 +170,8 @@ async function indexBlogs(): Promise<readonly IndexedItem[]> {
169170
type: "page",
170171
title: "Blog",
171172
description: "Stay up-to-date with the latest news about react-md",
172-
objectID: `${BASE_URL}/blog`,
173-
url: `${BASE_URL}/blog`,
173+
objectID: `${baseUrl}/blog`,
174+
url: `${baseUrl}/blog`,
174175
pathname: "/blog",
175176
headings,
176177
keywords: ["blog", "release notes"],
@@ -181,37 +182,94 @@ async function indexBlogs(): Promise<readonly IndexedItem[]> {
181182
return items;
182183
}
183184

184-
async function run(): Promise<void> {
185-
config({ path: join(process.cwd(), ".env.development.local") });
186-
config({ path: join(process.cwd(), ".env.local") });
185+
interface RequiredEnvVars {
186+
appId: string;
187+
apiKey: string;
188+
baseUrl: string;
189+
indexName: string;
190+
}
187191

188-
const appId = process.env.NEXT_PUBLIC_ALGOLIA_APP_ID;
189-
const apiKey = process.env.ALGOLIA_WRITE_API_KEY;
190-
const indexName = process.env.NEXT_PUBLIC_ALGOLIA_INDEX_NAME;
191-
if (!appId || !apiKey || !indexName) {
192-
throw new Error(
193-
`The .env.local is missing the NEXT_PUBLIC_ALGOLIA_APP_ID and/or ALGOLIA_WRITE_API_KEY and/or NEXT_PUBLIC_ALGOLIA_INDEX_NAME`
194-
);
192+
async function getEnvVars(): Promise<RequiredEnvVars> {
193+
const appId = process.env.ALGOLIA_APP_ID?.trim() ?? "";
194+
const apiKey = process.env.ALGOLIA_WRITE_API_KEY?.trim() ?? "";
195+
const indexName = process.env.ALGOLIA_INDEX_NAME?.trim() ?? "";
196+
const baseUrl = process.env.BASE_URL?.trim() ?? "";
197+
const missing: string[] = [];
198+
if (!appId) {
199+
missing.push("ALGOLIA_APP_ID");
200+
}
201+
if (!apiKey) {
202+
missing.push("ALGOLIA_WRITE_API_KEY");
203+
}
204+
if (!indexName) {
205+
missing.push("ALGOLIA_INDEX_NAME");
206+
}
207+
if (!baseUrl) {
208+
missing.push("BASE_URL");
209+
}
210+
if (missing.length) {
211+
console.error(`The following environment variables are missing:`);
212+
console.error(missing.map((name) => `- ${name}`).join("\n"));
213+
console.error("Update the `.env.algolia` with the correct values");
214+
process.exit(1);
215+
}
216+
217+
if (
218+
!(await confirm({
219+
message: `Are the following variables correct?
220+
221+
- appId - ${appId}
222+
- apiKey - ${apiKey}
223+
- baseUrl - ${baseUrl}
224+
- indexName - ${indexName}
225+
226+
`,
227+
}))
228+
) {
229+
console.error(`Update the \`.env.algolia\` and run again.`);
230+
process.exit(1);
195231
}
196232

233+
return {
234+
appId,
235+
apiKey,
236+
indexName,
237+
baseUrl,
238+
};
239+
}
240+
241+
async function run({
242+
appId,
243+
apiKey,
244+
baseUrl,
245+
indexName,
246+
}: RequiredEnvVars): Promise<void> {
197247
const client = searchClient(appId, apiKey);
198248
const indexes: IndexedItem[] = [];
199249
indexes.push(
200-
...(await log(indexMdxPages(), "Indexing MDX pages", "MDX pages indexed")),
201250
...(await log(
202-
indexSassDocPages(),
251+
indexMdxPages(baseUrl),
252+
"Indexing MDX pages",
253+
"MDX pages indexed"
254+
)),
255+
...(await log(
256+
indexSassDocPages(baseUrl),
203257
"Indexing SassDoc pages",
204258
"SassDoc pages indexed"
205259
)),
206-
...(await log(indexBlogs(), "Indexing Blog pages", "Blog pages indexed"))
260+
...(await log(
261+
indexBlogs(baseUrl),
262+
"Indexing Blog pages",
263+
"Blog pages indexed"
264+
))
207265
);
208266
indexes.push({
209267
type: "page",
210268
title: "Material Icons and Symbols",
211269
description:
212270
"This page is used to help find icons available in react-md using Material Symbols or Material Icons svg components. Icons can be filtered by type, group, or name.",
213-
objectID: `${BASE_URL}/components/material-icons-and-symbols`,
214-
url: `${BASE_URL}/components/material-icons-and-symbols`,
271+
objectID: `${baseUrl}/components/material-icons-and-symbols`,
272+
url: `${baseUrl}/components/material-icons-and-symbols`,
215273
pathname: "/components/material-icons-and-symbols",
216274
headings: [],
217275
keywords: ["icon", "material", "component"],
@@ -229,8 +287,10 @@ async function run(): Promise<void> {
229287
});
230288
}
231289

290+
config({ path: join(process.cwd(), ".env.algolia") });
291+
const envVars = await getEnvVars();
232292
await log(
233-
run(),
293+
run(envVars),
234294
"Indexing the documentation site",
235295
"Documentation site indexed!"
236296
);

0 commit comments

Comments
 (0)