mirror of
https://github.com/sergeyyarkov/yarkov.tech.git
synced 2026-10-11 02:19:08 +03:00
sitemap
This commit is contained in:
parent
b1b6b5eb15
commit
80236b839a
@ -1,6 +1,6 @@
|
||||
import siteconfig from "../siteconfig.json";
|
||||
|
||||
export const DEFAULT_LANGUAGE: LanguageKeys = "en";
|
||||
export const DEFAULT_LANGUAGE: LanguageKeys = "ru";
|
||||
export const IS_SITE_UNDER_CONSTRUCTION = siteconfig.isSiteUnderConstruction;
|
||||
export const SUPPORTED_LANGUAGES: SupportedLanguages = ["ru", "en"];
|
||||
export const URL_BLOG_PREFIX = "blog";
|
||||
|
||||
@ -8,9 +8,7 @@ export function useTranslation(Astro: Readonly<AstroGlobal>): (key: UIDictionary
|
||||
}
|
||||
|
||||
export function getLanguageKeys(): Array<string> {
|
||||
return Object.keys(languages).filter((lang) =>
|
||||
SUPPORTED_LANGUAGES.includes(lang as LanguageKeys)
|
||||
);
|
||||
return Object.keys(languages).filter((lang) => SUPPORTED_LANGUAGES.includes(lang as LanguageKeys));
|
||||
}
|
||||
|
||||
export function getLanguageFromURL(pathname: string): LanguageKeys {
|
||||
@ -32,9 +30,7 @@ function transformExports<T>(modules: Record<string, { default: T }>) {
|
||||
return translations;
|
||||
}
|
||||
|
||||
const translations = transformExports<Record<string, UIDictionaryKeys>>(
|
||||
import.meta.glob("./*/ui.ts", { eager: true })
|
||||
);
|
||||
const translations = transformExports<Record<string, UIDictionaryKeys>>(import.meta.glob("./*/ui.ts", { eager: true }));
|
||||
|
||||
export function translate(key: UIDictionaryKeys, lang: LanguageKeys): string {
|
||||
const value = translations[lang]?.[key] || translations[DEFAULT_LANGUAGE][key];
|
||||
|
||||
@ -1,46 +1,79 @@
|
||||
import type { APIContext } from "astro";
|
||||
import { createRelativeArticleUrl } from "@root/utils";
|
||||
import { getArticleList } from "../queries";
|
||||
// import { getCollection } from 'astro:content';
|
||||
import { DEFAULT_LANGUAGE, SUPPORTED_LANGUAGES } from "../constants";
|
||||
|
||||
type SitemapURL = { loc: string; changefreq?: string; priority?: number; langs?: { lang: string; href: string }[] };
|
||||
|
||||
export async function GET(context: APIContext) {
|
||||
const site = context.site?.origin || "https://yarkov.tech";
|
||||
const urls: Array<{ loc: string; changefreq?: string; priority?: number }> = [];
|
||||
const articles = await getArticleList();
|
||||
|
||||
urls.push(
|
||||
{ loc: `${site}/`, changefreq: "weekly", priority: 1.0 },
|
||||
{ loc: `${site}/articles/`, changefreq: "weekly", priority: 0.9 },
|
||||
{ loc: `${site}/projects/`, changefreq: "monthly", priority: 0.8 },
|
||||
{ loc: `${site}/about/`, changefreq: "monthly", priority: 0.7 }
|
||||
);
|
||||
const baseUrls: Array<SitemapURL> = [
|
||||
{ loc: `/`, changefreq: "weekly", priority: 1.0 },
|
||||
{ loc: `/articles/`, changefreq: "weekly", priority: 0.9 },
|
||||
{ loc: `/projects/`, changefreq: "monthly", priority: 0.8 },
|
||||
{ loc: `/about/`, changefreq: "monthly", priority: 0.7 },
|
||||
];
|
||||
|
||||
// articles.forEach((a) => {
|
||||
// if (a.translations) {
|
||||
// a.translations.forEach((article) => {
|
||||
// urls.push({ loc: `${site}` });
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
const articleUrls: Array<SitemapURL> = [];
|
||||
|
||||
articles.forEach((a) => {
|
||||
if (a.translations && a.translations.length >= 1) {
|
||||
const articlePageUrlLoc = a.translations[0];
|
||||
|
||||
if (articlePageUrlLoc) {
|
||||
const articleLang = articlePageUrlLoc.languages_code?.code.split("-")[0] || DEFAULT_LANGUAGE;
|
||||
const loc = createRelativeArticleUrl({
|
||||
articleLang,
|
||||
pubDate: articlePageUrlLoc.pub_date,
|
||||
slug: articlePageUrlLoc.slug,
|
||||
});
|
||||
|
||||
articleUrls.push({
|
||||
loc: `${site}${loc}`,
|
||||
langs: a.translations
|
||||
.filter((a) => !!a)
|
||||
.map((a) => ({
|
||||
lang: a.languages_code?.code.split("-")[0] || "",
|
||||
href: `${site}${createRelativeArticleUrl({
|
||||
articleLang: a.languages_code?.code.split("-")[0] || DEFAULT_LANGUAGE,
|
||||
pubDate: a.pub_date,
|
||||
slug: a.slug,
|
||||
})}/`,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
||||
${urls
|
||||
.map(
|
||||
(url) => `<url>
|
||||
<loc>${url.loc}</loc>${
|
||||
url.changefreq
|
||||
? `
|
||||
<changefreq>${url.changefreq}</changefreq>`
|
||||
: ""
|
||||
}${
|
||||
url.priority !== undefined
|
||||
? `
|
||||
<priority>${url.priority}</priority>`
|
||||
: ""
|
||||
}
|
||||
</url>`
|
||||
)
|
||||
.join("\n")}
|
||||
${baseUrls
|
||||
.map((url) => {
|
||||
return `<url>
|
||||
<loc>${site}${url.loc}</loc>
|
||||
${url.changefreq && `<changefreq>${url.changefreq}</changefreq>`}
|
||||
${url.priority && `<priority>${url.priority}</priority>`}
|
||||
${SUPPORTED_LANGUAGES.map(
|
||||
(lang) =>
|
||||
`<xhtml:link rel="alternate" hreflang="${lang}" href="${site}${
|
||||
lang !== DEFAULT_LANGUAGE ? `/${lang}` : ""
|
||||
}${url.loc}"/>`
|
||||
).join("")}
|
||||
</url>`;
|
||||
})
|
||||
.join("")}
|
||||
${articleUrls
|
||||
.map((url) => {
|
||||
return `
|
||||
<url>
|
||||
<loc>${url.loc}</loc>
|
||||
${url.langs?.map(({ lang, href }) => `<xhtml:link rel="alternate" hreflang="${lang}" href="${href}"/>`).join("")}
|
||||
</url>
|
||||
`;
|
||||
})
|
||||
.join("")}
|
||||
</urlset>`;
|
||||
|
||||
return new Response(sitemap, {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user