diff --git a/src/content/french/blog/post-1.md b/src/content/french/blog/post-1.md index 51e72f7..8d1a370 100755 --- a/src/content/french/blog/post-1.md +++ b/src/content/french/blog/post-1.md @@ -4,9 +4,9 @@ meta_title: "" description: "Ceci est une méta-description" date: 2022-04-04T05:00:00Z image: "/images/image-placeholder.png" -categories: ["Application", "Data"] +categories: ["french","Application", "Data"] author: "John Doe" -tags: ["nextjs", "tailwind"] +tags: ["nextjs", "tailwind", "react"] draft: false --- diff --git a/src/lib/contentParser.astro b/src/lib/contentParser.astro index 47d2494..d7e852a 100644 --- a/src/lib/contentParser.astro +++ b/src/lib/contentParser.astro @@ -7,32 +7,19 @@ import { } from "astro:content"; import config from "@/config/config.json"; +import languages from "@/config/language.json"; export const getSinglePage = async ( collectionName: C, - lang: keyof ContentEntryMap + lang: keyof ContentEntryMap | undefined, ): Promise[]> => { const { default_language } = config.settings; - const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap; - const languages = [ - { - languageName: "En", - languageCode: "en", - contentDir: "english", - weight: 1, - }, - { - languageName: "Fr", - languageCode: "fr", - contentDir: "french", - weight: 2, - }, - ]; + // If lang is undefined, use the default language + const selectedLanguageCode = lang || default_language; const language = languages.find( - (l) => - l.languageCode === langCollection || l.languageCode === default_language + (l: any) => l.languageCode === selectedLanguageCode, ); if (!language) { @@ -46,37 +33,26 @@ export const getSinglePage = async ( contentDir as any, ({ id }: any) => { return id.startsWith(collectionName) && !id.endsWith("-index.md"); - } + }, )) as CollectionEntry[]; - //@ts-ignore + // @ts-ignore const removeDrafts = pages.filter((data) => !data.data.draft); + return removeDrafts; }; export const getListPage = async ( collectionName: C, - lang: keyof ContentEntryMap + lang: keyof ContentEntryMap | undefined, ): Promise[]> => { const { default_language } = config.settings; - const languages = [ - { - languageName: "En", - languageCode: "en", - contentDir: "english", - weight: 1, - }, - { - languageName: "Fr", - languageCode: "fr", - contentDir: "french", - weight: 2, - }, - ]; + // If lang is undefined, use the default language + const selectedLanguageCode = lang || default_language; const language = languages.find( - (l) => l.languageCode == lang || l.languageCode == default_language + (l: any) => l.languageCode == selectedLanguageCode, ); if (!language) { @@ -90,7 +66,7 @@ export const getListPage = async ( contentDir as any, ({ id }: any) => { return id.startsWith(collectionName); - } + }, )) as CollectionEntry[]; return pages; diff --git a/src/lib/taxonomyParser.astro b/src/lib/taxonomyParser.astro index 3e14140..69c0c1c 100644 --- a/src/lib/taxonomyParser.astro +++ b/src/lib/taxonomyParser.astro @@ -1,20 +1,33 @@ --- 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: any, name: string) => { +export const getTaxonomy = async (collection: string, name: string) => { const { default_language } = config.settings; - const actualCollection = - collection !== ("" || undefined) ? collection : default_language; + + 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( actualCollection as keyof ContentEntryMap, ({ id }: any) => { return id.startsWith("blog") && !id.endsWith("-index.md"); - } + }, ); const taxonomyPages = singlePages.map((page: any) => page.data[name]); let taxonomies: string[] = []; @@ -29,15 +42,28 @@ export const getTaxonomy = async (collection: any, name: string) => { }; // get all taxonomies from frontmatter -export const getAllTaxonomy = async (collection: any, name: string) => { +export const getAllTaxonomy = async (collection: string, name: string) => { const { default_language } = config.settings; - const actualCollection = - collection !== ("" || undefined) ? collection : default_language; + + 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( actualCollection as keyof ContentEntryMap, ({ id }: any) => { return id.startsWith("blog") && !id.endsWith("-index.md"); - } + }, ); const taxonomyPages = singlePages.map((page: any) => page.data[name]); let taxonomies: string[] = []; diff --git a/src/pages/[...lang]/about.astro b/src/pages/[...lang]/about.astro index 88c810d..1419fdc 100644 --- a/src/pages/[...lang]/about.astro +++ b/src/pages/[...lang]/about.astro @@ -4,8 +4,7 @@ import Base from "@/layouts/Base.astro"; import { getListPage } from "@/lib/contentParser.astro"; import { supportedLang } from "@/lib/utils/i18nUtils"; import { markdownify } from "@/lib/utils/textConverter"; -import type { ContentEntryMap } from "astro:content"; -import type { ContentCollectionKey } from "astro:content"; +import type { ContentCollectionKey, ContentEntryMap } from "astro:content"; export function getStaticPaths() { const paths = supportedLang.map((lang) => ({ @@ -17,11 +16,9 @@ export function getStaticPaths() { const { lang } = Astro.params; const about: any = await getListPage( "about" as ContentCollectionKey, - lang as keyof ContentEntryMap + lang as keyof ContentEntryMap, ); -console.log(about); - const { Content } = await about[0].render(); const { title, description, meta_title, image } = about[0].data; --- diff --git a/src/pages/[...lang]/blog/page/[slug].astro b/src/pages/[...lang]/blog/page/[slug].astro index f1cde7f..8a3c25d 100755 --- a/src/pages/[...lang]/blog/page/[slug].astro +++ b/src/pages/[...lang]/blog/page/[slug].astro @@ -3,6 +3,7 @@ import BlogCard from "@/components/BlogCard.astro"; import Pagination from "@/components/Pagination.astro"; import config from "@/config/config.json"; import Base from "@/layouts/Base.astro"; +import { getListPage, getSinglePage } from "@/lib/contentParser.astro"; import { getAllTaxonomy, getTaxonomy } from "@/lib/taxonomyParser.astro"; import { supportedLang } from "@/lib/utils/i18nUtils"; import { sortByDate } from "@/lib/utils/sortFunctions"; @@ -14,24 +15,20 @@ const BLOG_FOLDER = "blog"; const { slug, lang } = Astro.params; -const postIndex: any = await getCollection( +const postIndex: any = await getListPage( + BLOG_FOLDER, lang as keyof ContentEntryMap, - ({ id }: any) => { - return id.startsWith(BLOG_FOLDER); - }, ); - -const posts = await getCollection( +const posts = await getSinglePage(BLOG_FOLDER, lang as keyof ContentEntryMap); +const allCategories = await getAllTaxonomy( lang as keyof ContentEntryMap, - ({ id }: any) => { - return id.startsWith(BLOG_FOLDER) && !id.endsWith("-index.md"); - }, + "categories", ); - -const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap; -const allCategories = await getAllTaxonomy(langCollection, "categories"); -const categories = await getTaxonomy(langCollection, "categories"); -const tags = await getTaxonomy(langCollection, "tags"); +const categories = await getTaxonomy( + lang as keyof ContentEntryMap, + "categories", +); +const tags = await getTaxonomy(lang as keyof ContentEntryMap, "tags"); const sortedPosts = sortByDate(posts); const totalPages = Math.ceil(posts.length / config.settings.pagination); const currentPage = slug && !isNaN(Number(slug)) ? Number(slug) : 1; diff --git a/src/pages/[...lang]/categories/[category].astro b/src/pages/[...lang]/categories/[category].astro index 32c02a1..879dec1 100755 --- a/src/pages/[...lang]/categories/[category].astro +++ b/src/pages/[...lang]/categories/[category].astro @@ -16,7 +16,10 @@ export async function getStaticPaths() { const paths = await Promise.all( supportedLang.map(async (lang) => { - const categories = await getTaxonomy(lang, "categories"); + const categories = await getTaxonomy( + lang as keyof ContentEntryMap, + "categories", + ); return categories.map((category) => ({ params: { @@ -27,11 +30,14 @@ export async function getStaticPaths() { category, }, })); - }) + }), ); // Handle default path (no lang) - const defaultCategories = await getTaxonomy(default_language, "categories"); + const defaultCategories = await getTaxonomy( + default_language as keyof ContentEntryMap, + "categories", + ); const defaultPaths = defaultCategories.map((category) => ({ params: { lang: undefined, diff --git a/src/pages/[...lang]/index.astro b/src/pages/[...lang]/index.astro index cc21300..93c201a 100755 --- a/src/pages/[...lang]/index.astro +++ b/src/pages/[...lang]/index.astro @@ -2,14 +2,13 @@ import ImageMod from "@/components/ImageMod.astro"; import config from "@/config/config.json"; import Base from "@/layouts/Base.astro"; -import { getListPage } from "@/lib/contentParser.astro"; +import { getListPage, getSinglePage } from "@/lib/contentParser.astro"; import { supportedLang } from "@/lib/utils/i18nUtils"; import { markdownify } from "@/lib/utils/textConverter"; import CallToAction from "@/partials/CallToAction.astro"; import Testimonial from "@/partials/Testimonial.astro"; import type { Button, Feature } from "@/types"; import type { ContentCollectionKey, ContentEntryMap } from "astro:content"; -import { getCollection } from "astro:content"; import { FaCheck } from "react-icons/fa"; const { default_language } = config.settings; @@ -37,19 +36,14 @@ const homepage: any = await getListPage( ); const { banner, features } = homepage[0].data; -const testimonial = await getCollection( - (lang as keyof ContentEntryMap) || default_language, - ({ id }: any) => { - return id.startsWith("sections/testimonial") && !id.endsWith("-index.md"); - }, +const testimonial = await getSinglePage( + "sections/testimonial" as ContentCollectionKey, + lang as keyof ContentEntryMap, ); -const call_to_action = await getCollection( - (lang as keyof ContentEntryMap) || default_language, - ({ id }: any) => { - return ( - id.startsWith("sections/call-to-action") && !id.endsWith("-index.md") - ); - }, + +const call_to_action = await getSinglePage( + "sections/call-to-action" as ContentCollectionKey, + lang as keyof ContentEntryMap, ); --- diff --git a/src/pages/[...lang]/tags/[tag].astro b/src/pages/[...lang]/tags/[tag].astro index 0b1286c..88904f9 100644 --- a/src/pages/[...lang]/tags/[tag].astro +++ b/src/pages/[...lang]/tags/[tag].astro @@ -16,7 +16,7 @@ export async function getStaticPaths() { const paths = await Promise.all( supportedLang.map(async (lang) => { - const tags = await getTaxonomy(lang, "tags"); + const tags = await getTaxonomy(lang as keyof ContentEntryMap, "tags"); return tags.map((tag) => ({ params: { @@ -27,11 +27,14 @@ export async function getStaticPaths() { tag, }, })); - }) + }), ); // Handle default path (no lang) - const defaultCategories = await getTaxonomy(default_language, "tags"); + const defaultCategories = await getTaxonomy( + default_language as keyof ContentEntryMap, + "tags", + ); const defaultPaths = defaultCategories.map((tag) => ({ params: { lang: undefined, diff --git a/src/types/index.d.ts b/src/types/index.d.ts index acb3d63..fb8ccc0 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,3 +1,5 @@ +import type { ContentEntryMap } from "astro:content"; + export type Feature = { button: button; image: string;