mirror of
https://github.com/sergeyyarkov/yarkov.tech.git
synced 2026-10-11 02:19:08 +03:00
refactor: api & astro.config
This commit is contained in:
parent
49563e96a3
commit
c67fdb982b
@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
44
src/lib/api.ts
Normal file
44
src/lib/api.ts
Normal file
@ -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<AstroGlobal>): MDXInstance<ArticleType>[] {
|
||||
const data = import.meta.glob<MDXInstance<ArticleType>>("../../content/articles/**/*.mdx", { eager: true });
|
||||
const modules = formatArticlesByLangs(data);
|
||||
const siteLang = utils.getLanguageFromURL(Astro.url.pathname);
|
||||
const articles: MDXInstance<ArticleType>[] = [];
|
||||
const push = (article: MDXInstance<ArticleType>, 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);
|
||||
}
|
||||
@ -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<AstroGlobal>): MDXInstance<ArticleType>[] {
|
||||
const data = import.meta.glob<MDXInstance<ArticleType>>("../../content/articles/**/*.mdx", { eager: true });
|
||||
const modules = formatArticlesByLangs(data);
|
||||
const siteLang = utils.getLanguageFromURL(Astro.url.pathname);
|
||||
const articles: MDXInstance<ArticleType>[] = [];
|
||||
const push = (article: MDXInstance<ArticleType>, 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();
|
||||
@ -1 +0,0 @@
|
||||
export { default as ArticleService } from "./ArticleService";
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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"]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user