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";
|
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 IS_SITE_UNDER_CONSTRUCTION = siteconfig.isSiteUnderConstruction;
|
||||||
export const SUPPORTED_LANGUAGES: SupportedLanguages = ["ru", "en"];
|
export const SUPPORTED_LANGUAGES: SupportedLanguages = ["ru", "en"];
|
||||||
export const URL_BLOG_PREFIX = "blog";
|
export const URL_BLOG_PREFIX = "blog";
|
||||||
|
|||||||
@ -8,9 +8,7 @@ export function useTranslation(Astro: Readonly<AstroGlobal>): (key: UIDictionary
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getLanguageKeys(): Array<string> {
|
export function getLanguageKeys(): Array<string> {
|
||||||
return Object.keys(languages).filter((lang) =>
|
return Object.keys(languages).filter((lang) => SUPPORTED_LANGUAGES.includes(lang as LanguageKeys));
|
||||||
SUPPORTED_LANGUAGES.includes(lang as LanguageKeys)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLanguageFromURL(pathname: string): LanguageKeys {
|
export function getLanguageFromURL(pathname: string): LanguageKeys {
|
||||||
@ -32,9 +30,7 @@ function transformExports<T>(modules: Record<string, { default: T }>) {
|
|||||||
return translations;
|
return translations;
|
||||||
}
|
}
|
||||||
|
|
||||||
const translations = transformExports<Record<string, UIDictionaryKeys>>(
|
const translations = transformExports<Record<string, UIDictionaryKeys>>(import.meta.glob("./*/ui.ts", { eager: true }));
|
||||||
import.meta.glob("./*/ui.ts", { eager: true })
|
|
||||||
);
|
|
||||||
|
|
||||||
export function translate(key: UIDictionaryKeys, lang: LanguageKeys): string {
|
export function translate(key: UIDictionaryKeys, lang: LanguageKeys): string {
|
||||||
const value = translations[lang]?.[key] || translations[DEFAULT_LANGUAGE][key];
|
const value = translations[lang]?.[key] || translations[DEFAULT_LANGUAGE][key];
|
||||||
|
|||||||
@ -1,46 +1,79 @@
|
|||||||
import type { APIContext } from "astro";
|
import type { APIContext } from "astro";
|
||||||
|
import { createRelativeArticleUrl } from "@root/utils";
|
||||||
import { getArticleList } from "../queries";
|
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) {
|
export async function GET(context: APIContext) {
|
||||||
const site = context.site?.origin || "https://yarkov.tech";
|
const site = context.site?.origin || "https://yarkov.tech";
|
||||||
const urls: Array<{ loc: string; changefreq?: string; priority?: number }> = [];
|
|
||||||
const articles = await getArticleList();
|
const articles = await getArticleList();
|
||||||
|
|
||||||
urls.push(
|
const baseUrls: Array<SitemapURL> = [
|
||||||
{ loc: `${site}/`, changefreq: "weekly", priority: 1.0 },
|
{ loc: `/`, changefreq: "weekly", priority: 1.0 },
|
||||||
{ loc: `${site}/articles/`, changefreq: "weekly", priority: 0.9 },
|
{ loc: `/articles/`, changefreq: "weekly", priority: 0.9 },
|
||||||
{ loc: `${site}/projects/`, changefreq: "monthly", priority: 0.8 },
|
{ loc: `/projects/`, changefreq: "monthly", priority: 0.8 },
|
||||||
{ loc: `${site}/about/`, changefreq: "monthly", priority: 0.7 }
|
{ loc: `/about/`, changefreq: "monthly", priority: 0.7 },
|
||||||
);
|
];
|
||||||
|
|
||||||
// articles.forEach((a) => {
|
const articleUrls: Array<SitemapURL> = [];
|
||||||
// if (a.translations) {
|
|
||||||
// a.translations.forEach((article) => {
|
articles.forEach((a) => {
|
||||||
// urls.push({ loc: `${site}` });
|
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"?>
|
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">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
||||||
${urls
|
${baseUrls
|
||||||
.map(
|
.map((url) => {
|
||||||
(url) => `<url>
|
return `<url>
|
||||||
<loc>${url.loc}</loc>${
|
<loc>${site}${url.loc}</loc>
|
||||||
url.changefreq
|
${url.changefreq && `<changefreq>${url.changefreq}</changefreq>`}
|
||||||
? `
|
${url.priority && `<priority>${url.priority}</priority>`}
|
||||||
<changefreq>${url.changefreq}</changefreq>`
|
${SUPPORTED_LANGUAGES.map(
|
||||||
: ""
|
(lang) =>
|
||||||
}${
|
`<xhtml:link rel="alternate" hreflang="${lang}" href="${site}${
|
||||||
url.priority !== undefined
|
lang !== DEFAULT_LANGUAGE ? `/${lang}` : ""
|
||||||
? `
|
}${url.loc}"/>`
|
||||||
<priority>${url.priority}</priority>`
|
).join("")}
|
||||||
: ""
|
</url>`;
|
||||||
}
|
})
|
||||||
</url>`
|
.join("")}
|
||||||
)
|
${articleUrls
|
||||||
.join("\n")}
|
.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>`;
|
</urlset>`;
|
||||||
|
|
||||||
return new Response(sitemap, {
|
return new Response(sitemap, {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user