mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-16 14:18:29 +00:00
Map categories count
This commit is contained in:
@@ -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<Category> {
|
||||
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<Category[]> {
|
||||
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)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}))
|
||||
})
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
import { useCategoryView } from '../../composables/useCategoryView'
|
||||
|
||||
interface Header {
|
||||
level: number
|
||||
title: string
|
||||
slug: string
|
||||
iconCount: number
|
||||
link: string
|
||||
children: Header[]
|
||||
}
|
||||
@@ -13,7 +15,7 @@ type MenuItem = Omit<Header, 'slug' | 'children'> & {
|
||||
children?: MenuItem[]
|
||||
}
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
headers: MenuItem[]
|
||||
root?: boolean
|
||||
}>()
|
||||
@@ -33,14 +35,19 @@ function onClick(event: Event) {
|
||||
|
||||
<template>
|
||||
<ul :class="root ? 'root' : 'nested'">
|
||||
<li v-for="{ children, link, title } in headers">
|
||||
<li v-for="{ children, link, title, iconCount } in headers">
|
||||
<a
|
||||
class="outline-link"
|
||||
:href="link"
|
||||
@click="onClick"
|
||||
:title="title"
|
||||
>
|
||||
{{ title }}
|
||||
<span>
|
||||
{{ title }}
|
||||
</span>
|
||||
<span class="icon-count" :aria-label="`Count of icons in ${title}`">
|
||||
{{ iconCount }}
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -57,7 +64,7 @@ function onClick(event: Event) {
|
||||
}
|
||||
|
||||
.outline-link {
|
||||
display: block;
|
||||
display: flex;
|
||||
line-height: 28px;
|
||||
color: var(--vp-c-text-2);
|
||||
white-space: nowrap;
|
||||
@@ -76,4 +83,9 @@ function onClick(event: Event) {
|
||||
.outline-link.nested {
|
||||
padding-left: 13px;
|
||||
}
|
||||
|
||||
.icon-count {
|
||||
opacity: 0.5;
|
||||
margin-left: auto;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user