working with the content folder

This commit is contained in:
Al Murad Uzzaman
2024-05-18 17:08:18 +06:00
parent 8c1ab27945
commit 76f1b5b6c1
38 changed files with 90 additions and 25 deletions
+54 -3
View File
@@ -15,9 +15,35 @@ export const getSinglePage = async <C extends CollectionKey>(
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,
},
];
const language = languages.find(
(l) =>
l.languageCode === langCollection || l.languageCode === default_language
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
// Explicitly define the type of pages to CollectionEntry<C>[]
const pages: CollectionEntry<C>[] = (await getCollection(
langCollection || default_language,
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName) && !id.endsWith("-index.md");
}
@@ -33,10 +59,35 @@ export const getListPage = async <C extends CollectionKey>(
lang: keyof ContentEntryMap
): Promise<CollectionEntry<C>[]> => {
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,
},
];
const language = languages.find(
(l) => l.languageCode == lang || l.languageCode == default_language
);
if (!language) {
throw new Error("Language not found");
}
const { contentDir } = language;
// Fetch the collection based on the language
const pages: CollectionEntry<C>[] = (await getCollection(
langCollection || default_language,
contentDir as any,
({ id }: any) => {
return id.startsWith(collectionName);
}
+10 -14
View File
@@ -5,20 +5,17 @@ import fs from "fs";
import path from "path";
const { default_language } = config.settings;
const menusFolderPath = "./src/config";
const locales: { [key: string]: any } = {};
const locales = fs
.readdirSync(menusFolderPath)
.filter((file) => /^menu\.[a-z]{2}\.json$/.test(file))
.map((file) => {
const filePath = path.join(menusFolderPath, file);
const localeName = file.split(".")[1]; // Extract language code from file name
const localeData = JSON.parse(fs.readFileSync(filePath, "utf8"));
return { [localeName]: localeData };
})
.reduce((accumulator, locale) => {
return { ...accumulator, ...locale };
}, {});
// Load menu and dictionary dynamically
languagesJSON.forEach((language) => {
const { languageCode } = language;
import(`../../config/menu.${languageCode}.json`).then((menu) => {
import(`../../i18n/${languageCode}.json`).then((dictionary) => {
locales[languageCode] = { ...menu, ...dictionary };
});
});
});
// Extract all languages from the locales object
const languages = Object.keys(locales);
@@ -47,7 +44,6 @@ export const getTranslations = async (lang: string) => {
export const supportedLang = [""].concat(
languagesJSON.map((lang) => lang.languageCode),
);
// Function to construct the URL based on trailing_slash value
export const constructUrl = (
url: string,
+20 -4
View File
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import fs from "fs";
import path from "path";
import languages from "@/config/language.json";
export interface ChildNavigationLink {
name: string;
@@ -19,7 +20,22 @@ interface Menu {
}
export function loadMenu(lang: string): Menu {
const menuPath: string = path.join(process.cwd(), 'src', 'config', `menu.${lang}.json`);
const menuData: string = fs.readFileSync(menuPath, 'utf-8');
// Find the language object based on the language code
const language = languages.find((l) => l.languageCode === lang);
if (!language) {
throw new Error(
`Language '${lang}' not found in the list of supported languages.`,
);
}
// Load menu data based on the language code
const menuPath: string = path.join(
process.cwd(),
"src",
"config",
`menu.${language.languageCode}.json`,
);
const menuData: string = fs.readFileSync(menuPath, "utf-8");
return JSON.parse(menuData) as Menu;
}