mirror of
https://github.com/sergeyyarkov/yarkov.tech.git
synced 2026-10-11 10:29:21 +03:00
refactor: refactored getTranslatedArticles method in ArticleService
This commit is contained in:
parent
a22e453a56
commit
49563e96a3
97
package-lock.json
generated
97
package-lock.json
generated
@ -1,16 +1,16 @@
|
||||
{
|
||||
"name": "yarkov.tech",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "yarkov.tech",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@astrojs/image": "^0.3.7",
|
||||
"@astrojs/mdx": "^0.10.3",
|
||||
"@astrojs/image": "^0.4.0",
|
||||
"@astrojs/mdx": "^0.11.0",
|
||||
"@astrojs/rss": "^1.0.0",
|
||||
"@astrojs/sitemap": "^1.0.0",
|
||||
"@astrojs/solid-js": "^1.1.0",
|
||||
@ -18,7 +18,7 @@
|
||||
"@types/mdx": "^2.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
||||
"@typescript-eslint/parser": "^5.36.1",
|
||||
"astro": "^1.1.2",
|
||||
"astro": "^1.1.3",
|
||||
"astro-compress": "^1.0.6",
|
||||
"astro-icon": "^0.7.3",
|
||||
"eslint": "^8.23.0",
|
||||
@ -34,7 +34,7 @@
|
||||
"rehype-slug": "^5.0.1",
|
||||
"remark-gfm": "^3.0.1",
|
||||
"remark-smartypants": "^2.0.0",
|
||||
"sass": "^1.54.7",
|
||||
"sass": "^1.54.8",
|
||||
"solid-js": "^1.5.3"
|
||||
}
|
||||
},
|
||||
@ -58,17 +58,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@astrojs/image": {
|
||||
"version": "0.3.7",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/image/-/image-0.3.7.tgz",
|
||||
"integrity": "sha512-A6029ed1ZfhcaZJLnII0XCl29VzeFJp07VqelOGi3yyi7VAXREHZa3bT23V5YQt/goPwKac8gZn2/pgnYPea6Q==",
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/image/-/image-0.4.0.tgz",
|
||||
"integrity": "sha512-DUXY9N91CvCskLJyKB9GQH4S8NoxVyQYHikmE8xx0haYbu8BgIX1fq/N5hN5+cNxkqLTKXKcHffY0lquwidSBg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"etag": "^1.8.1",
|
||||
"image-size": "^1.0.1",
|
||||
"mrmime": "^1.0.0",
|
||||
"sharp": "^0.30.6",
|
||||
"slash": "^4.0.0",
|
||||
"tiny-glob": "^0.2.9"
|
||||
"image-size": "^1.0.2",
|
||||
"magic-string": "^0.25.9",
|
||||
"mime": "^3.0.0",
|
||||
"sharp": "^0.30.6"
|
||||
}
|
||||
},
|
||||
"node_modules/@astrojs/language-server": {
|
||||
@ -137,9 +135,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@astrojs/mdx": {
|
||||
"version": "0.10.3",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/mdx/-/mdx-0.10.3.tgz",
|
||||
"integrity": "sha512-bZ74xKJ1eqH9shd4TcpgS4Rn2ycQvXwBXkgKSABvFHQhT16vufuBo0M9vmLDdHKnDupYphCj1aBilmN9VeLugA==",
|
||||
"version": "0.11.0",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/mdx/-/mdx-0.11.0.tgz",
|
||||
"integrity": "sha512-nAtPNGYpzMlLVjM3P7DBZqjNJ7mIDJct+G9vgTrHYdsCvk5T18HXQLgwMS6WlIyQ2AGy+jah2VpRxDwBffh0MA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@astrojs/prism": "^1.0.1",
|
||||
@ -149,6 +147,7 @@
|
||||
"es-module-lexer": "^0.10.5",
|
||||
"github-slugger": "^1.4.0",
|
||||
"gray-matter": "^4.0.3",
|
||||
"kleur": "^4.1.4",
|
||||
"rehype-raw": "^6.1.1",
|
||||
"remark-frontmatter": "^4.0.1",
|
||||
"remark-gfm": "^3.0.1",
|
||||
@ -1597,9 +1596,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/astro": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/astro/-/astro-1.1.2.tgz",
|
||||
"integrity": "sha512-btawnJ+MwkJbjLO0uSMrlYl71jsa6ljyXEXeeFitp9aqRo0sqH7JdTE0nh7MWsVMePXEvcJ88WOkEhW/FpRu/Q==",
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://registry.npmjs.org/astro/-/astro-1.1.3.tgz",
|
||||
"integrity": "sha512-bD12gtIQkvqkc7L2syqsFdJzhvDYF/4EP8+RyMnE/FycAYSCn7z6AtIBMZIhPVhD+3GFhNYrhG5Sjfx/TlR9tQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@astrojs/compiler": "^0.23.4",
|
||||
@ -3588,15 +3587,6 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/etag": {
|
||||
"version": "1.8.1",
|
||||
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
|
||||
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/execa": {
|
||||
"version": "6.1.0",
|
||||
"resolved": "https://registry.npmjs.org/execa/-/execa-6.1.0.tgz",
|
||||
@ -8005,9 +7995,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/sass": {
|
||||
"version": "1.54.7",
|
||||
"resolved": "https://registry.npmjs.org/sass/-/sass-1.54.7.tgz",
|
||||
"integrity": "sha512-3q7MQz7sCpVG6TLhUfZwGOcd2/sm2ghYN2JEdRjNiW04ILdvahdo9GuAs+bxsxZ3hDCKv+wUT5w0iFWGU0CxlA==",
|
||||
"version": "1.54.8",
|
||||
"resolved": "https://registry.npmjs.org/sass/-/sass-1.54.8.tgz",
|
||||
"integrity": "sha512-ib4JhLRRgbg6QVy6bsv5uJxnJMTS2soVcCp9Y88Extyy13A8vV0G1fAwujOzmNkFQbR3LvedudAMbtuNRPbQww==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"chokidar": ">=3.0.0 <4.0.0",
|
||||
@ -9570,17 +9560,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"@astrojs/image": {
|
||||
"version": "0.3.7",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/image/-/image-0.3.7.tgz",
|
||||
"integrity": "sha512-A6029ed1ZfhcaZJLnII0XCl29VzeFJp07VqelOGi3yyi7VAXREHZa3bT23V5YQt/goPwKac8gZn2/pgnYPea6Q==",
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/image/-/image-0.4.0.tgz",
|
||||
"integrity": "sha512-DUXY9N91CvCskLJyKB9GQH4S8NoxVyQYHikmE8xx0haYbu8BgIX1fq/N5hN5+cNxkqLTKXKcHffY0lquwidSBg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"etag": "^1.8.1",
|
||||
"image-size": "^1.0.1",
|
||||
"mrmime": "^1.0.0",
|
||||
"sharp": "^0.30.6",
|
||||
"slash": "^4.0.0",
|
||||
"tiny-glob": "^0.2.9"
|
||||
"image-size": "^1.0.2",
|
||||
"magic-string": "^0.25.9",
|
||||
"mime": "^3.0.0",
|
||||
"sharp": "^0.30.6"
|
||||
}
|
||||
},
|
||||
"@astrojs/language-server": {
|
||||
@ -9641,9 +9629,9 @@
|
||||
}
|
||||
},
|
||||
"@astrojs/mdx": {
|
||||
"version": "0.10.3",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/mdx/-/mdx-0.10.3.tgz",
|
||||
"integrity": "sha512-bZ74xKJ1eqH9shd4TcpgS4Rn2ycQvXwBXkgKSABvFHQhT16vufuBo0M9vmLDdHKnDupYphCj1aBilmN9VeLugA==",
|
||||
"version": "0.11.0",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/mdx/-/mdx-0.11.0.tgz",
|
||||
"integrity": "sha512-nAtPNGYpzMlLVjM3P7DBZqjNJ7mIDJct+G9vgTrHYdsCvk5T18HXQLgwMS6WlIyQ2AGy+jah2VpRxDwBffh0MA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@astrojs/prism": "^1.0.1",
|
||||
@ -9653,6 +9641,7 @@
|
||||
"es-module-lexer": "^0.10.5",
|
||||
"github-slugger": "^1.4.0",
|
||||
"gray-matter": "^4.0.3",
|
||||
"kleur": "^4.1.4",
|
||||
"rehype-raw": "^6.1.1",
|
||||
"remark-frontmatter": "^4.0.1",
|
||||
"remark-gfm": "^3.0.1",
|
||||
@ -10754,9 +10743,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"astro": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/astro/-/astro-1.1.2.tgz",
|
||||
"integrity": "sha512-btawnJ+MwkJbjLO0uSMrlYl71jsa6ljyXEXeeFitp9aqRo0sqH7JdTE0nh7MWsVMePXEvcJ88WOkEhW/FpRu/Q==",
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://registry.npmjs.org/astro/-/astro-1.1.3.tgz",
|
||||
"integrity": "sha512-bD12gtIQkvqkc7L2syqsFdJzhvDYF/4EP8+RyMnE/FycAYSCn7z6AtIBMZIhPVhD+3GFhNYrhG5Sjfx/TlR9tQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@astrojs/compiler": "^0.23.4",
|
||||
@ -12133,12 +12122,6 @@
|
||||
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
|
||||
"dev": true
|
||||
},
|
||||
"etag": {
|
||||
"version": "1.8.1",
|
||||
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
|
||||
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
|
||||
"dev": true
|
||||
},
|
||||
"execa": {
|
||||
"version": "6.1.0",
|
||||
"resolved": "https://registry.npmjs.org/execa/-/execa-6.1.0.tgz",
|
||||
@ -15261,9 +15244,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"sass": {
|
||||
"version": "1.54.7",
|
||||
"resolved": "https://registry.npmjs.org/sass/-/sass-1.54.7.tgz",
|
||||
"integrity": "sha512-3q7MQz7sCpVG6TLhUfZwGOcd2/sm2ghYN2JEdRjNiW04ILdvahdo9GuAs+bxsxZ3hDCKv+wUT5w0iFWGU0CxlA==",
|
||||
"version": "1.54.8",
|
||||
"resolved": "https://registry.npmjs.org/sass/-/sass-1.54.8.tgz",
|
||||
"integrity": "sha512-ib4JhLRRgbg6QVy6bsv5uJxnJMTS2soVcCp9Y88Extyy13A8vV0G1fAwujOzmNkFQbR3LvedudAMbtuNRPbQww==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"chokidar": ">=3.0.0 <4.0.0",
|
||||
|
||||
@ -19,8 +19,8 @@
|
||||
"lint": "eslint '**/*.{js,mjs,ts,tsx}'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@astrojs/image": "^0.3.7",
|
||||
"@astrojs/mdx": "^0.10.3",
|
||||
"@astrojs/image": "^0.4.0",
|
||||
"@astrojs/mdx": "^0.11.0",
|
||||
"@astrojs/rss": "^1.0.0",
|
||||
"@astrojs/sitemap": "^1.0.0",
|
||||
"@astrojs/solid-js": "^1.1.0",
|
||||
@ -28,7 +28,7 @@
|
||||
"@types/mdx": "^2.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
||||
"@typescript-eslint/parser": "^5.36.1",
|
||||
"astro": "^1.1.2",
|
||||
"astro": "^1.1.3",
|
||||
"astro-compress": "^1.0.6",
|
||||
"astro-icon": "^0.7.3",
|
||||
"eslint": "^8.23.0",
|
||||
@ -44,7 +44,7 @@
|
||||
"rehype-slug": "^5.0.1",
|
||||
"remark-gfm": "^3.0.1",
|
||||
"remark-smartypants": "^2.0.0",
|
||||
"sass": "^1.54.7",
|
||||
"sass": "^1.54.8",
|
||||
"solid-js": "^1.5.3"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
---
|
||||
import { Image, Picture } from "@astrojs/image/components";
|
||||
import { Picture } from "@astrojs/image/components";
|
||||
import { createDateFormatter } from "@root/utils";
|
||||
import { isURL } from "@utils/string";
|
||||
import ArticleComments from "@components/ArticleComments";
|
||||
import "./ArticlePost.scss";
|
||||
import TranslatedLink from "../TranslatedLink";
|
||||
import "./ArticlePost.scss";
|
||||
|
||||
export type Props = {
|
||||
data: ArticleType;
|
||||
@ -44,7 +45,7 @@ const { title, coverImage, tags, author, published_at } = data;
|
||||
<figure>
|
||||
<Picture
|
||||
itemprop="image"
|
||||
src={coverImage}
|
||||
src={isURL(coverImage) ? coverImage : `../../../content/assets/images/${coverImage}`}
|
||||
sizes="(max-width: 500px) 500px, 700px"
|
||||
widths={[500, 700]}
|
||||
formats={["webp"]}
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
---
|
||||
import { DEFAULT_LANGUAGE } from "@root/config";
|
||||
import { DEFAULT_LANGUAGE, SUPPORTED_LANGUAGES } from "@root/config";
|
||||
import { getLanguageFromURL } from "@root/utils";
|
||||
|
||||
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
|
||||
to: string;
|
||||
}
|
||||
|
||||
const lang = getLanguageFromURL(Astro.url.pathname);
|
||||
let lang = getLanguageFromURL(Astro.url.pathname);
|
||||
const { to, ...attrs } = Astro.props as Props;
|
||||
|
||||
if (!SUPPORTED_LANGUAGES.includes(lang)) lang = DEFAULT_LANGUAGE;
|
||||
|
||||
const href = lang === DEFAULT_LANGUAGE ? to : `/${lang}${to}`;
|
||||
---
|
||||
|
||||
|
||||
@ -1,32 +1,46 @@
|
||||
import type { AstroGlobal, MDXInstance } from "astro";
|
||||
import { formatArticlesByLangs } from "@i18n/utils";
|
||||
import { getLanguageFromURL } from "@utils/url";
|
||||
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 data = import.meta.glob<MDXInstance<ArticleType>>("../../content/articles/**/*.mdx", { eager: true });
|
||||
const modules = formatArticlesByLangs(data);
|
||||
const siteLang = getLanguageFromURL(Astro.url.pathname);
|
||||
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 key of Object.keys(modules)) {
|
||||
const langs = Object.keys(modules[key]) as LanguageKeys[];
|
||||
for (const dirName of Object.keys(modules)) {
|
||||
const article = modules[dirName];
|
||||
const langs = Object.keys(article) as LanguageKeys[];
|
||||
|
||||
for (const lang of langs) {
|
||||
const isNonDefaultLanguage = !langs.includes(siteLang) && langs.length === 1;
|
||||
const article = modules[key];
|
||||
if (lang !== siteLang) {
|
||||
if (langs.length === 1) {
|
||||
push(article[lang], lang);
|
||||
}
|
||||
|
||||
if (lang === siteLang) push(article[siteLang], siteLang);
|
||||
if (isNonDefaultLanguage) 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 articles.filter((article) => !article.frontmatter.draft);
|
||||
return utils.getPublishedArticles(articles);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +15,10 @@ export const sortArtcilesByDate = (articles: ArticleType[]): ArticleType[] => {
|
||||
return articles.sort((a, b) => new Date(b.published_at).valueOf() - new Date(a.published_at).valueOf());
|
||||
};
|
||||
|
||||
export const getPublishedArticles = (articles: MDXInstance<ArticleType>[]) => {
|
||||
return articles.filter((article) => !article.frontmatter.draft);
|
||||
};
|
||||
|
||||
/**
|
||||
* This function will distribute the array of articles by years.
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user