Files
astroplate/src/lib/taxonomyParser.astro
T

53 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-04-17 12:50:29 +06:00
---
2024-05-17 18:25:14 +06:00
import config from "@/config/config.json";
2023-12-09 11:14:43 +06:00
import { slugify } from "@/lib/utils/textConverter";
2024-05-14 15:34:22 +06:00
import type { ContentEntryMap } from "astro:content";
import { getCollection } from "astro:content";
2023-12-03 15:28:12 +06:00
// get taxonomy from frontmatter
2023-12-09 11:14:43 +06:00
export const getTaxonomy = async (collection: any, name: string) => {
2024-05-17 18:25:14 +06:00
const { default_language } = config.settings;
const actualCollection =
collection !== ("" || undefined) ? collection : default_language;
2024-05-14 15:34:22 +06:00
const singlePages = await getCollection(
2024-05-17 18:25:14 +06:00
actualCollection as keyof ContentEntryMap,
2024-05-16 17:11:25 +06:00
({ id }: any) => {
2024-05-14 15:34:22 +06:00
return id.startsWith("blog") && !id.endsWith("-index.md");
2024-05-18 09:49:56 +06:00
}
2024-05-14 15:34:22 +06:00
);
2023-08-19 09:05:26 +06:00
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
2023-05-21 11:42:20 +06:00
let taxonomies: string[] = [];
2023-04-17 12:50:29 +06:00
for (let i = 0; i < taxonomyPages.length; i++) {
const categoryArray = taxonomyPages[i];
for (let j = 0; j < categoryArray.length; j++) {
taxonomies.push(slugify(categoryArray[j]));
}
}
const taxonomy = [...new Set(taxonomies)];
return taxonomy;
};
2023-12-03 15:28:12 +06:00
// get all taxonomies from frontmatter
2023-12-09 11:14:43 +06:00
export const getAllTaxonomy = async (collection: any, name: string) => {
2024-05-17 18:25:14 +06:00
const { default_language } = config.settings;
const actualCollection =
collection !== ("" || undefined) ? collection : default_language;
2024-05-14 15:34:22 +06:00
const singlePages = await getCollection(
2024-05-17 18:25:14 +06:00
actualCollection as keyof ContentEntryMap,
2024-05-16 17:11:25 +06:00
({ id }: any) => {
2024-05-14 15:34:22 +06:00
return id.startsWith("blog") && !id.endsWith("-index.md");
2024-05-18 09:49:56 +06:00
}
2024-05-14 15:34:22 +06:00
);
2023-08-19 09:05:26 +06:00
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
2023-05-21 11:42:20 +06:00
let taxonomies: string[] = [];
2023-04-17 12:50:29 +06:00
for (let i = 0; i < taxonomyPages.length; i++) {
const categoryArray = taxonomyPages[i];
for (let j = 0; j < categoryArray.length; j++) {
taxonomies.push(slugify(categoryArray[j]));
}
}
return taxonomies;
};
---