Skip to content

Commit a6d7b10

Browse files
committed
api tweaks
1 parent 35f3fc2 commit a6d7b10

7 files changed

Lines changed: 34 additions & 42 deletions

File tree

src/i18n/utils.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ export function createTInstance(locale: string): Promise<TFunction> {
9292
}
9393
export { getLocale };
9494

95-
export const stripLocaleFromId = (id: string): string => {
96-
const slashIdx = id.indexOf("/");
97-
return slashIdx === -1 ? id : id.slice(slashIdx + 1);
95+
export const splitLocaleId = (id: string): { locale: string; slug: string } => {
96+
const i = id.indexOf("/");
97+
return i === -1
98+
? { locale: id, slug: id }
99+
: { locale: id.slice(0, i), slug: id.slice(i + 1) };
98100
};
99101

100102
const collectionMapCache = new Map<string, Promise<Map<string, unknown>>>();
@@ -112,43 +114,37 @@ const getCollectionMap = <C extends CollectionKey>(
112114
return promise as Promise<Map<string, CollectionEntry<C>>>;
113115
};
114116

115-
export const getLocalizedCollection = async <C extends CollectionKey>(
117+
export const applyLocale = async <C extends CollectionKey>(
118+
entries: CollectionEntry<C>[],
116119
name: C,
117120
locale: string,
118121
): Promise<CollectionEntry<C>[]> => {
122+
if (locale === defaultLocale) return entries;
119123
const map = await getCollectionMap(name);
120-
const defaults = [...map.values()].filter((e) =>
121-
e.id.startsWith(`${defaultLocale}/`),
122-
);
123-
if (locale === defaultLocale) return defaults;
124-
return defaults.map(
124+
return entries.map(
125125
(e) =>
126-
(map.get(`${locale}/${stripLocaleFromId(e.id)}`) as
126+
(map.get(`${locale}/${splitLocaleId(e.id).slug}`) as
127127
| CollectionEntry<C>
128128
| undefined) ?? e,
129129
);
130130
};
131131

132-
export const getLocalizedEntry = async <C extends CollectionKey>(
132+
export const getLocalizedCollection = async <C extends CollectionKey>(
133133
name: C,
134134
locale: string,
135-
slug: string,
136-
): Promise<CollectionEntry<C> | undefined> => {
135+
): Promise<CollectionEntry<C>[]> => {
137136
const map = await getCollectionMap(name);
138-
return map.get(`${locale}/${slug}`) ?? map.get(`${defaultLocale}/${slug}`);
137+
const defaults = [...map.values()].filter((e) =>
138+
e.id.startsWith(`${defaultLocale}/`),
139+
);
140+
return applyLocale(defaults, name, locale);
139141
};
140142

141-
export const applyLocale = async <C extends CollectionKey>(
142-
entries: CollectionEntry<C>[],
143+
export const getLocalizedEntry = async <C extends CollectionKey>(
143144
name: C,
144145
locale: string,
145-
): Promise<CollectionEntry<C>[]> => {
146-
if (locale === defaultLocale) return entries;
146+
slug: string,
147+
): Promise<CollectionEntry<C> | undefined> => {
147148
const map = await getCollectionMap(name);
148-
return entries.map(
149-
(e) =>
150-
(map.get(`${locale}/${stripLocaleFromId(e.id)}`) as
151-
| CollectionEntry<C>
152-
| undefined) ?? e,
153-
);
149+
return map.get(`${locale}/${slug}`) ?? map.get(`${defaultLocale}/${slug}`);
154150
};

src/pages/blog/[...page].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export const prerender = true;
33
44
import { defaultLocale } from "@/i18n/config";
5-
import { applyLocale, stripLocaleFromId } from "@/i18n/utils";
5+
import { applyLocale, splitLocaleId } from "@/i18n/utils";
66
import { getLocalizedBlog } from "@/utils/blog";
77
88
import Layout from "@/layouts/Layout.astro";
@@ -77,7 +77,7 @@ const tabs = [
7777
{
7878
posts.map((post) => (
7979
<BlogCard
80-
id={stripLocaleFromId(post.id)}
80+
id={splitLocaleId(post.id).slug}
8181
title={post.data.title}
8282
author={post.data.author}
8383
tags={post.data.tags || []}

src/pages/blog/[...post].astro

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import BlogHeader from "@/components/pages/blog/BlogHeader.astro";
66
import Card from "@/components/ui/Card.astro";
77
import Layout from "@/layouts/Layout.astro";
88
import { defaultLocale } from "@/i18n/config";
9-
import {
10-
getDirection,
11-
getLocalizedEntry,
12-
stripLocaleFromId,
13-
} from "@/i18n/utils";
9+
import { getDirection, getLocalizedEntry, splitLocaleId } from "@/i18n/utils";
1410
import { getDateStringFromId, getLocalizedBlog } from "@/utils/blog";
1511
import type { GetStaticPaths } from "astro";
1612
import { Image } from "astro:assets";
@@ -19,7 +15,7 @@ import { render } from "astro:content";
1915
export const getStaticPaths: GetStaticPaths = async () => {
2016
const posts = await getLocalizedBlog(defaultLocale);
2117
return posts.map((post) => {
22-
const slug = stripLocaleFromId(post.id);
18+
const { slug } = splitLocaleId(post.id);
2319
return { params: { post: slug }, props: { slug } };
2420
});
2521
};
@@ -39,7 +35,7 @@ if (!post) {
3935
const { Content } = await render(post);
4036
const allPosts = await getLocalizedBlog(locale);
4137
const recentPosts = allPosts.filter((p) => p.id !== post.id).slice(0, 4);
42-
const contentLocale = post.id.split("/")[0];
38+
const { locale: contentLocale } = splitLocaleId(post.id);
4339
const contentDir = getDirection(contentLocale);
4440
---
4541

@@ -103,7 +99,7 @@ const contentDir = getDirection(contentLocale);
10399
<ul class="recent-posts">
104100
{
105101
recentPosts.map((recentPost) => {
106-
const recentSlug = stripLocaleFromId(recentPost.id);
102+
const { slug: recentSlug } = splitLocaleId(recentPost.id);
107103
const dateStr = getDateStringFromId(recentSlug);
108104
const formattedDate = localizeDateString(dateStr, {
109105
year: "numeric",

src/pages/blog/tags/[tag]/[...page].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { CollectionEntry } from "astro:content";
1414
1515
import MaskIcon from "@/components/ui/icons/MaskIcon.astro";
1616
import { defaultLocale } from "@/i18n/config";
17-
import { applyLocale, stripLocaleFromId } from "@/i18n/utils";
17+
import { applyLocale, splitLocaleId } from "@/i18n/utils";
1818
import { getLocalizedBlog } from "@/utils/blog";
1919
import { icons } from "@/utils/icons";
2020
@@ -109,7 +109,7 @@ const titleKey = isMainTab ? "blog:index.hero.title" : "blog:tags.hero.title";
109109
{
110110
posts.map((post) => (
111111
<BlogCard
112-
id={stripLocaleFromId(post.id)}
112+
id={splitLocaleId(post.id).slug}
113113
title={post.data.title}
114114
author={post.data.author}
115115
tags={post.data.tags || []}

src/pages/feed.xml.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import rss from "@astrojs/rss";
22
import { defaultLocale } from "@/i18n/config";
3-
import { stripLocaleFromId } from "@/i18n/utils";
3+
import { splitLocaleId } from "@/i18n/utils";
44
import { getDateStringFromId, getLocalizedBlog } from "@/utils/blog";
55
import { createSafeMarkdown } from "@/utils/safeMarkdown";
66

@@ -13,7 +13,7 @@ export async function GET(context) {
1313
description: "Monero Blog RSS Feed",
1414
site: context.site,
1515
items: blog.map((post) => {
16-
const slug = stripLocaleFromId(post.id);
16+
const { slug } = splitLocaleId(post.id);
1717
return {
1818
title: post.data.title,
1919
pubDate: getDateStringFromId(slug),

src/pages/open-graph/[...route].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { OGImageRoute } from "astro-og-canvas";
22

33
import { defaultLocale } from "@/i18n/config";
4-
import { stripLocaleFromId } from "@/i18n/utils";
4+
import { splitLocaleId } from "@/i18n/utils";
55
import { getLocalizedBlog } from "@/utils/blog";
66

77
export const prerender = true;
88

99
const posts = await getLocalizedBlog(defaultLocale);
1010
const pages: Record<string, { title: string; description: string }> = {};
1111
for (const post of posts) {
12-
const slug = stripLocaleFromId(post.id);
12+
const { slug } = splitLocaleId(post.id);
1313
// astro-og-canvas trims anything after the last dot, so its necessary to include a dummy .md extension, don't remove this!
1414
pages[`blog/${slug}.md`] = {
1515
title: post.data.title ?? "Blog post",

src/pages/resources/moneropedia/[...slug].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defaultLocale } from "@/i18n/config";
55
import {
66
getLocalizedCollection,
77
getLocalizedEntry,
8-
stripLocaleFromId,
8+
splitLocaleId,
99
} from "@/i18n/utils";
1010
import type { CollectionEntry } from "astro:content";
1111
import { render } from "astro:content";
@@ -16,7 +16,7 @@ import BackLink from "@/components/ui/BackLink.astro";
1616
export async function getStaticPaths() {
1717
const entries = await getLocalizedCollection("moneropedia", defaultLocale);
1818
return entries.map((entry) => ({
19-
params: { slug: stripLocaleFromId(entry.id) },
19+
params: { slug: splitLocaleId(entry.id).slug },
2020
}));
2121
}
2222

0 commit comments

Comments
 (0)