diff --git a/astro.config.ts b/astro.config.ts index 9c4b874..60125a3 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -26,6 +26,7 @@ import { defineConfig } from "astro/config"; import { h } from "hastscript"; import { toString } from "hast-util-to-string"; import { escape } from "html-escaper"; +import { DEFAULT_LANGUAGE } from "./src/config"; import languages from "./src/i18n/languages"; const createAnchorLabel = (heading: string) => { @@ -41,7 +42,7 @@ export default defineConfig({ solid(), sitemap({ i18n: { - defaultLocale: "ru", + defaultLocale: DEFAULT_LANGUAGE, locales: Object.fromEntries(Object.keys(languages).map((key) => [key, key])), }, }), @@ -111,7 +112,4 @@ export default defineConfig({ }, }, }, - experimental: { - integrations: true, - }, }); diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 0000000..6c06f29 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,44 @@ +import type { AstroGlobal, MDXInstance } from "astro"; + +import { formatArticlesByLangs } from "@i18n/utils"; +import * as utils from "@utils/index"; + +export function fetchTranslatedArticles(Astro: Readonly): MDXInstance[] { + const data = import.meta.glob>("../../content/articles/**/*.mdx", { eager: true }); + const modules = formatArticlesByLangs(data); + const siteLang = utils.getLanguageFromURL(Astro.url.pathname); + const articles: MDXInstance[] = []; + const push = (article: MDXInstance, lang: LanguageKeys) => { + articles.push(article); + article.frontmatter.lang = lang; + }; + + for (const dirName of Object.keys(modules)) { + const article = modules[dirName]; + const langs = Object.keys(article) as LanguageKeys[]; + + for (const lang of langs) { + if (lang !== siteLang) { + if (langs.length === 1) { + push(article[lang], lang); + } + + if (!langs.includes(siteLang) && langs.length >= 2) { + const fallbackLang = langs[0]; + const isPushed = articles.find(({ frontmatter }) => { + return frontmatter.title === article[fallbackLang].frontmatter.title; + }); + + if (!isPushed) { + push(article[fallbackLang], langs[fallbackLang]); + } + } + continue; + } + + push(article[lang], lang); + } + } + + return utils.getPublishedArticles(articles); +} diff --git a/src/services/ArticleService.ts b/src/services/ArticleService.ts deleted file mode 100644 index bcf056f..0000000 --- a/src/services/ArticleService.ts +++ /dev/null @@ -1,47 +0,0 @@ -import type { AstroGlobal, MDXInstance } from "astro"; -import { formatArticlesByLangs } from "@i18n/utils"; -import * as utils from "@utils/index"; - -class ArticleService { - public getTranslatedArticles(Astro: Readonly): MDXInstance[] { - const data = import.meta.glob>("../../content/articles/**/*.mdx", { eager: true }); - const modules = formatArticlesByLangs(data); - const siteLang = utils.getLanguageFromURL(Astro.url.pathname); - const articles: MDXInstance[] = []; - const push = (article: MDXInstance, lang: LanguageKeys) => { - articles.push(article); - article.frontmatter.lang = lang; - }; - - for (const dirName of Object.keys(modules)) { - const article = modules[dirName]; - const langs = Object.keys(article) as LanguageKeys[]; - - for (const lang of langs) { - if (lang !== siteLang) { - if (langs.length === 1) { - push(article[lang], lang); - } - - if (!langs.includes(siteLang) && langs.length >= 2) { - const fallbackLang = langs[0]; - const isPushed = articles.find(({ frontmatter }) => { - return frontmatter.title === article[fallbackLang].frontmatter.title; - }); - - if (!isPushed) { - push(article[fallbackLang], langs[fallbackLang]); - } - } - continue; - } - - push(article[lang], lang); - } - } - - return utils.getPublishedArticles(articles); - } -} - -export default new ArticleService(); diff --git a/src/services/index.ts b/src/services/index.ts deleted file mode 100644 index 1d9fc80..0000000 --- a/src/services/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { default as ArticleService } from "./ArticleService"; diff --git a/src/templates/Articles.astro b/src/templates/Articles.astro index 0778c86..40cd937 100644 --- a/src/templates/Articles.astro +++ b/src/templates/Articles.astro @@ -5,10 +5,10 @@ import ArticlesList from "@components/ArticlesList"; import { transformArticles, formatToArticleBlocks } from "@utils/articles"; import { getLanguageFromURL } from "@root/utils"; import { useTranslation } from "@i18n/utils"; -import { ArticleService } from "@services/index"; +import * as API from "@root/lib/api"; const t = useTranslation(Astro); -const data = ArticleService.getTranslatedArticles(Astro); +const data = API.fetchTranslatedArticles(Astro); const tags = new Set(data.map((article) => article.frontmatter.tags).flat()); const lang = getLanguageFromURL(Astro.url.pathname); const articles = transformArticles(data); diff --git a/src/templates/Home.astro b/src/templates/Home.astro index a093826..62b40d3 100644 --- a/src/templates/Home.astro +++ b/src/templates/Home.astro @@ -5,10 +5,10 @@ import RecentArticles from "@components/RecentArticles"; import { Splash, SplashHeading, SplashLinks, Link } from "@components/Splash"; import { getLanguageFromURL } from "@root/utils"; import { useTranslation } from "@i18n/utils"; -import { ArticleService } from "@services/index"; import UIString from "@components/UIString"; +import * as API from "@root/lib/api"; -const data = ArticleService.getTranslatedArticles(Astro); +const data = API.fetchTranslatedArticles(Astro); const t = useTranslation(Astro); const lang = getLanguageFromURL(Astro.url.pathname); const articles = utils.transformArticles(data); diff --git a/tsconfig.json b/tsconfig.json index 237b3e9..9127811 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,8 +19,7 @@ "@stores/*": ["./src/stores/*"], "@hooks/*": ["./src/hooks/*"], "@i18n/*": ["./src/i18n/*"], - "@images/*": ["./src/images/*"], - "@services/*": ["./src/services/*"] + "@images/*": ["./src/images/*"] }, "outDir": "./dist", "typeRoots": ["./node_modules/@types", "./src/types"]