Files
astroplate/src/lib/taxonomyParser.astro
T

79 lines
2.4 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";
2024-05-18 22:20:40 +06:00
import languages from "@/config/language.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
2024-05-18 22:20:40 +06:00
export const getTaxonomy = async (collection: string, name: string) => {
2024-05-17 18:25:14 +06:00
const { default_language } = config.settings;
2024-05-18 22:20:40 +06:00
const language = languages.find((l) => l.languageCode === collection);
let actualCollection = default_language;
if (language) {
actualCollection = language.contentDir;
} else {
const defaultLanguageMatch = languages.find(
(l) => l.languageCode === default_language
2024-05-18 22:20:40 +06:00
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
2024-05-17 18:25:14 +06:00
2024-05-14 15:34:22 +06:00
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
2024-05-16 17:11:25 +06:00
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
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
2024-05-18 22:20:40 +06:00
export const getAllTaxonomy = async (collection: string, name: string) => {
2024-05-17 18:25:14 +06:00
const { default_language } = config.settings;
2024-05-18 22:20:40 +06:00
const language = languages.find((l) => l.languageCode === collection);
let actualCollection = default_language;
if (language) {
actualCollection = language.contentDir;
} else {
const defaultLanguageMatch = languages.find(
(l) => l.languageCode === default_language
2024-05-18 22:20:40 +06:00
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
2024-05-14 15:34:22 +06:00
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
2024-05-16 17:11:25 +06:00
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
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;
};
---