Files
astroplate/src/lib/taxonomyParser.astro
T
2024-05-23 15:39:53 +06:00

79 lines
2.4 KiB
Plaintext

---
import config from "@/config/config.json";
import languages from "@/config/language.json";
import { slugify } from "@/lib/utils/textConverter";
import type { ContentEntryMap } from "astro:content";
import { getCollection } from "astro:content";
// get taxonomy from frontmatter
export const getTaxonomy = async (collection: string, name: string) => {
const { default_language } = config.settings;
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
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
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;
};
// get all taxonomies from frontmatter
export const getAllTaxonomy = async (collection: string, name: string) => {
const { default_language } = config.settings;
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
);
if (defaultLanguageMatch) {
actualCollection = defaultLanguageMatch.contentDir;
}
}
const singlePages = await getCollection(
"blog" as keyof ContentEntryMap,
({ id }: any) => {
return id.startsWith(actualCollection) && !id.endsWith("-index.md");
}
);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
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;
};
---