project setup

This commit is contained in:
Al Murad Uzzaman
2024-05-14 15:34:22 +06:00
parent d34a3f5489
commit ac6fa96c9c
86 changed files with 1966 additions and 293 deletions
+43 -3
View File
@@ -1,16 +1,56 @@
---
// import {
// getCollection,
// type CollectionEntry,
// type CollectionKey,
// } from "astro:content";
// export const getSinglePage = async <C extends CollectionKey>(
// collectionName: C,
// ): Promise<CollectionEntry<C>[]> => {
// const allPages = await getCollection(collectionName);
// const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/));
// const removeDrafts = removeIndex.filter((data) => !data.data.draft);
// return removeDrafts;
// };
import {
getCollection,
type CollectionEntry,
type CollectionKey,
type ContentEntryMap,
} from "astro:content";
import config from "@/config/config.json";
export const getSinglePage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap
): Promise<CollectionEntry<C>[]> => {
const allPages = await getCollection(collectionName);
const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/));
const removeDrafts = removeIndex.filter((data) => !data.data.draft);
const { defaultLang } = config.language;
const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap;
// Explicitly define the type of pages to CollectionEntry<C>[]
const pages: CollectionEntry<C>[] = await getCollection(langCollection || defaultLang, ({ id }) => {
return id.startsWith(collectionName) && !id.endsWith("-index.md");
}) as CollectionEntry<C>[];
const removeDrafts = pages.filter((data) => !data.data.draft);
return removeDrafts;
};
export const getListPage = async <C extends CollectionKey>(
collectionName: C,
lang: keyof ContentEntryMap
): Promise<CollectionEntry<C>[]> => {
const { defaultLang } = config.language;
const langCollection: keyof ContentEntryMap = lang as keyof ContentEntryMap;
// Fetch the collection based on the language
const pages: CollectionEntry<C>[] = await getCollection(langCollection || defaultLang, ({ id }) => {
return id.startsWith(collectionName);
}) as CollectionEntry<C>[];
return pages;
};
---
+17 -2
View File
@@ -1,10 +1,19 @@
---
import { getSinglePage } from "@/lib/contentParser.astro";
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) => {
const singlePages = await getSinglePage(collection);
// const singlePages = await getSinglePage(collection);
const singlePages = await getCollection(
collection as keyof ContentEntryMap,
({ id }) => {
return id.startsWith("blog") && !id.endsWith("-index.md");
}
);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
for (let i = 0; i < taxonomyPages.length; i++) {
@@ -19,7 +28,13 @@ export const getTaxonomy = async (collection: any, name: string) => {
// get all taxonomies from frontmatter
export const getAllTaxonomy = async (collection: any, name: string) => {
const singlePages = await getSinglePage(collection);
// const singlePages = await getSinglePage(collection);
const singlePages = await getCollection(
collection as keyof ContentEntryMap,
({ id }) => {
return id.startsWith("blog") && !id.endsWith("-index.md");
}
);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
for (let i = 0; i < taxonomyPages.length; i++) {
+62
View File
@@ -0,0 +1,62 @@
import fs from 'fs';
import path from 'path';
const menusFolderPath = './src/config';
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 };
}, {});
// Extract all languages from the locales object
const languages = Object.keys(locales);
// Export the locales and languages
export { locales, languages };
export function getLangFromUrl(url: URL): string {
const [, lang] = url.pathname.split('/');
if (locales.hasOwnProperty(lang)) {
return lang;
}
return 'en';
}
// import { ui } from '@/i18n/ui';
// import config from "@/config/config.json";
// const {defaultLang}: {defaultLang : keyof typeof ui} = config.language as any;
// export function useTranslations(lang: keyof typeof ui) {
// return function t(key: keyof typeof ui[typeof defaultLang]) {
// return ui[lang][key] || ui[defaultLang][key];
// }
// }
import { ui } from '@/i18n/ui';
import config from "@/config/config.json";
const { defaultLang }: { defaultLang: keyof typeof ui } = config.language as any;
export function useTranslations(lang?: keyof typeof ui) {
const activeLang = lang || defaultLang;
return function t(key: keyof typeof ui[typeof defaultLang]): string {
const translation = ui[activeLang][key];
// If not found, fall back to the default language
if (translation) {
return translation;
} else {
return ui[defaultLang][key];
}
};
}
+25
View File
@@ -0,0 +1,25 @@
import fs from 'fs';
import path from 'path';
export interface ChildNavigationLink {
name: string;
url: string;
}
export interface NavigationLink {
name: string;
url: string;
hasChildren?: boolean;
children?: ChildNavigationLink[];
}
interface Menu {
main: NavigationLink[];
footer: NavigationLink[];
}
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');
return JSON.parse(menuData) as Menu;
}