nextui/apps/docs/scripts/update-search-meta.ts
Junior Garcia 9b6566f089
v2.7.9 (#5342)
* fix(date-picker): error state (#5317)

* fix(date-range-picker): fixed the error state in preset

* Update giant-sloths-shop.md

* Removed if statement

* chore(date-picker): prettier

---------

Co-authored-by: WK Wong <wingkwong.code@gmail.com>

* fix(theme): clear button in mobile (#5252)

* fix(toast): fixed close button hover position

* fix(input): fixed the clear button rendering on smaller devices

* Delete .changeset/soft-spoons-march.md

* Update input.ts

* Undo unrelated toast changes

* fix(toast): icons (#5246)

* feat(shared-icons): add loading icon

* fix(toast): icons

* chore(toast): revise types for icons

* chore(changeset): add changeset

* refactor: migrate eslint to v9 (#5267)

* refactor: migrate eslint to v9

* chore: lint

* chore: update eslint command

* chore: fix lint warnings

* chore: separate lint and lint:fix

* chore: exclude contentlayer generated code

* fix(scripts): add missing await

* fix(autocomplete): persist last selected item position (#5286)

* refactor(select): remove unnecessary code

* fix(autocomplete): persist last selected item position

* chore(changeset): add changeset

* chore(deps): bump framer-motion version (#5287)

* chore(deps): bump framer-motion version

* fix: typing issues

* chore(changeset): add changeset

---------

Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>

* chore(docs): supplement onAction & selectionBehavior (#5289)

* fix(autocomplete): ensure focused item matches selected item after filter, selection (#5290)

* fix(autocomplete): ensure focused item matches selected item after filter, selection

* chore: apply type and default value

* chore: add perpose coment in updated code

* test: add focuskey management testcode

* docs: add changeset

* docs: update changeset

* chore: remove comment

* fix: broken components in stories (#5291)

* chore(switch): remove xl size

* chore(docs): remove xl size

* chore(system-rsc): remove xl size

* chore(circular-progress): remove xl size

* chore: undo

* chore(deps): bump RA versions (#5310)

* chore(deps): ra version bump

* chore(changeset): add changeset

* fix(scripts): incorrect docs path

---------

Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>

* chore(docs): update meta data (#5311)

* docs(layout.tsx): added text-foreground (#5316)

* feat(tabs): add click handling for tab items in tests and implementation (#3917)

Co-authored-by: WK Wong <wingkwong.code@gmail.com>

* fix issues in tabs examples (#2405)

Co-authored-by: WK Wong <wingkwong.code@gmail.com>

* chore(docs): add missing onValueChange in CheckboxGroup (#5332)

* ci(changesets): version packages (#5323)

Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>

---------

Co-authored-by: Vishv Salvi <82429084+Vishvsalvi@users.noreply.github.com>
Co-authored-by: WK Wong <wingkwong.code@gmail.com>
Co-authored-by: KumJungMin <37934668+KumJungMin@users.noreply.github.com>
Co-authored-by: liaoyinglong <vigossliao@gmail.com>
Co-authored-by: zhengjitf <zhengjitf@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-06-01 16:22:36 -03:00

161 lines
3.7 KiB
TypeScript

/* eslint-disable no-console */
import path from "path";
import fs from 'fs'
import {v4 as uuid} from "uuid"
import shell from "shelljs";
import dotenv from "dotenv";
import algoliasearch from "algoliasearch";
// @ts-ignore
import prettier from 'prettier'
// @ts-ignore
import toc from "markdown-toc";
import {parseMarkdownFile, fileToPath, removePrefix} from "@docusaurus/utils";
const docsRootFolder = "content/docs";
const configFolder = "config";
interface ResultType {
content: string
objectID: string
url: string
type: 'lvl1' | 'lvl2' | 'lvl3'
hierarchy: {
lvl1: string | null
lvl2?: string | null
lvl3?: string | null
}
}
interface TOCResultItem {
content: string
slug: string
lvl: 1 | 2 | 3
i: number
seen: number
}
const getUrl = (slug: string) => {
const url = removePrefix(slug, "/")
return `/docs${url}`
}
async function getMDXMeta(file: string) {
const {content, frontMatter: _frontMatter} = await parseMarkdownFile(file);
const frontMatter = _frontMatter as Record<string, any>
const tableOfContent = toc(content);
const json = tableOfContent.json as TOCResultItem[]
let slug = fileToPath(file)
.replace(`/${docsRootFolder}`, "")
.replace(process.cwd(), "");
const result:ResultType[] = [];
const title = frontMatter.title || "";
result.push({
content: title,
objectID: uuid(),
type: "lvl1",
url: getUrl(slug),
hierarchy: {
lvl1: title,
},
});
json.forEach((item, index) => {
item.content !== title && result.push({
content: item.content,
objectID: uuid(),
type: `lvl${item.lvl}`,
url: getUrl(slug) + `#${item.slug}`,
hierarchy: {
lvl1: title,
lvl2: item.lvl === 2 ? item.content : json[index - 1]?.content ?? null,
lvl3: item.lvl === 3 ? item.content : null,
},
});
});
return result;
}
async function getSearchMeta(saveMode: "algolia" | "local" = "local") {
dotenv.config();
try {
let json: any = [];
const files = shell
.ls("-R", docsRootFolder)
.map((file: any) => path.join(process.cwd(), docsRootFolder, file))
.filter((file: any) => file.endsWith(".mdx"));
for (const file of files) {
let result: ResultType[] = [];
try {
result = await getMDXMeta(file);
json.push(...result);
} catch {}
}
if (saveMode === "local") {
// Uncomment this to see save json into a file
json = prettier.format(JSON.stringify(json), { parser: 'json' });
// create a folder if it doesn't exist
if (!fs.existsSync(`${configFolder}`)) {
fs.mkdirSync(`${configFolder}`);
}
const outPath = path.join(
process.cwd(),
`${configFolder}`,
'search-meta.json'
);
fs.writeFileSync(outPath, json);
console.log("[HeroUI] Search meta is ready ✅");
return;
}
// Initialize Algolia client
const client = algoliasearch(
process.env.ALGOLIA_APP_ID || "",
process.env.ALGOLIA_ADMIN_API_KEY || "",
);
const tmpIndex = await client.initIndex("prod_docs_tmp");
const mainIndex = await client.initIndex("prod_docs");
// Get settings of main index and set them to the temp index
const indexSettings = await mainIndex.getSettings();
await tmpIndex.setSettings(indexSettings);
console.log("[Items count 🚀]: " + json.length);
console.log("[Saving on Algolia ⏰...]");
await mainIndex.replaceAllObjects(json, {
autoGenerateObjectIDIfNotExist: true,
safe: true,
});
console.log("[HeroUI] Search meta is ready ✅");
} catch (error) {
console.error(`[ERROR 🔥]:`, error);
}
}
getSearchMeta();