diff --git a/docs/.vitepress/lib/categories.ts b/docs/.vitepress/lib/categories.ts index 55b95104f..3a84bf930 100644 --- a/docs/.vitepress/lib/categories.ts +++ b/docs/.vitepress/lib/categories.ts @@ -1,11 +1,10 @@ import fs from "fs"; import path from "path"; import {Category, IconEntity} from "../theme/types"; -import {getAllData} from "./icons"; const directory = path.join(process.cwd(), "../categories"); -export function getAllCategoryFiles() { +export function getAllCategoryFiles(): Category[] { const fileNames = fs.readdirSync(directory).filter((file) => path.extname(file) === '.json'); return fileNames.map((fileName) => { @@ -19,21 +18,9 @@ export function getAllCategoryFiles() { }); } -export async function getData(name: string, icons: IconEntity[]): Promise { - const jsonPath = path.join(directory, `${name}.json`); - const jsonContent = fs.readFileSync(jsonPath, "utf8"); - const categoryJson = JSON.parse(jsonContent); - - return { - ...categoryJson, - name, - iconCount: icons.reduce((acc, curr) => (curr.categories.includes(name) ? ++acc : acc), 0) - }; -} - -export async function getAllCategories(): Promise { - const names = getAllCategoryFiles(); - const icons = await getAllData(); - - return Promise.all(names.map((name) => getData(name, icons))); +export function mapCategoryIconCount(categories: Category[], icons: IconEntity[]) { + return categories.map((category) => ({ + ...category, + iconCount: icons.reduce((acc, curr) => (curr.categories.includes(category.name) ? ++acc : acc), 0) + })) } diff --git a/docs/.vitepress/theme/components/icons/CategoryList.data.ts b/docs/.vitepress/theme/components/icons/CategoryList.data.ts index f73277fc1..896534684 100644 --- a/docs/.vitepress/theme/components/icons/CategoryList.data.ts +++ b/docs/.vitepress/theme/components/icons/CategoryList.data.ts @@ -1,35 +1,16 @@ -import fs from 'fs' -import path from 'path' import { getAllData } from '../../../lib/icons'; +import { getAllCategoryFiles, mapCategoryIconCount } from '../../../lib/categories'; -export function getAllCategoryFiles() { - const fileNames = fs.readdirSync(path.join('../categories')).filter((file) => path.extname(file) === '.json'); - - return fileNames - .map((fileName) => path.basename(fileName, '.json')) - .map((fileName) => [fileName, fs.readFileSync(path.join('../categories', fileName + '.json'), 'utf8')]) - .map(([name, file]) => ({ - name, - ...JSON.parse(file) - })); -} export default { async load() { const icons = await getAllData() - const categories = getAllCategoryFiles() + let categories = getAllCategoryFiles() - const categoryIconCounts = Object.fromEntries( - categories.map(({ name }) => { - const categoryIcons = icons.filter(({ categories }) => categories.includes(name)) - - return [name, categoryIcons.length]; - }) - ) + categories = mapCategoryIconCount(categories, icons) return { categories, - categoryIconCounts, } } } diff --git a/docs/.vitepress/theme/components/icons/CategoryList.vue b/docs/.vitepress/theme/components/icons/CategoryList.vue index fdfdfba84..d514a8056 100644 --- a/docs/.vitepress/theme/components/icons/CategoryList.vue +++ b/docs/.vitepress/theme/components/icons/CategoryList.vue @@ -21,10 +21,11 @@ const headers = computed(() => { const linkPrefix = page.value.relativePath.startsWith('icons/categories') ? '' : '/icons/categories' - return data.categories.map(({ name, title }) => ({ + return data.categories.map(({ name, title, iconCount }) => ({ level: 2, link: `${linkPrefix}#${name}`, title, + iconCount })) }) diff --git a/docs/.vitepress/theme/components/icons/CategoryListItem.vue b/docs/.vitepress/theme/components/icons/CategoryListItem.vue index 57452d695..5f200574d 100644 --- a/docs/.vitepress/theme/components/icons/CategoryListItem.vue +++ b/docs/.vitepress/theme/components/icons/CategoryListItem.vue @@ -1,10 +1,12 @@