Add static checking to i18n and improve types
This commit is contained in:
parent
f8ac450ab5
commit
579e5879e1
16 changed files with 126 additions and 82 deletions
|
|
@ -4,6 +4,7 @@ import type { Lang, Website } from "../../../content/config";
|
|||
import { t } from "../../../i18n";
|
||||
import { formatCopyrightedCharacters } from "../../../utils/format_copyrighted_characters";
|
||||
import { markdownToBbcode } from "../../../utils/markdown_to_bbcode";
|
||||
import { getUsernameForLang } from "../../../utils/get_username_for_lang";
|
||||
|
||||
interface ExportWebsiteInfo {
|
||||
website: Website;
|
||||
|
|
@ -156,9 +157,9 @@ function getLinkForUser(user: CollectionEntry<"users">, website: ExportWebsiteNa
|
|||
|
||||
function getNameForUser(user: CollectionEntry<"users">, anonymousUser: CollectionEntry<"users">, lang: Lang): string {
|
||||
if (user.data.isAnonymous) {
|
||||
return t(lang, anonymousUser.data.nameLang as any) || anonymousUser.data.name;
|
||||
return getUsernameForLang(anonymousUser, lang);
|
||||
}
|
||||
return t(lang, user.data.nameLang as any) || user.data.name;
|
||||
return getUsernameForLang(user, lang);
|
||||
}
|
||||
|
||||
type Props = {
|
||||
|
|
@ -179,14 +180,15 @@ export const getStaticPaths: GetStaticPaths = async () => {
|
|||
}));
|
||||
};
|
||||
|
||||
const ANONYMOUS_USER = await getEntry("users", "anonymous");
|
||||
|
||||
export const GET: APIRoute<Props, Params> = async ({ props: { story }, site }) => {
|
||||
const { lang } = story.data;
|
||||
const copyrightedCharacters = await formatCopyrightedCharacters(story.data.copyrightedCharacters);
|
||||
const authorsList = await getEntries([story.data.authors].flat());
|
||||
const commissioner = story.data.commissioner && (await getEntry(story.data.commissioner));
|
||||
const requester = story.data.requester && (await getEntry(story.data.requester));
|
||||
const anonymousUser = await getEntry("users", "anonymous");
|
||||
const anonymousFallback = getNameForUser(anonymousUser, anonymousUser, lang);
|
||||
const anonymousFallback = getNameForUser(ANONYMOUS_USER, ANONYMOUS_USER, lang);
|
||||
|
||||
const description = Object.fromEntries(
|
||||
WEBSITE_LIST.map<[ExportWebsiteName, string]>(({ website, exportFormat }) => {
|
||||
|
|
@ -234,10 +236,10 @@ export const GET: APIRoute<Props, Params> = async ({ props: { story }, site }) =
|
|||
`${t(
|
||||
lang,
|
||||
"story/authors",
|
||||
authorsList.map((author) => getNameForUser(author, anonymousUser, lang)),
|
||||
authorsList.map((author) => getNameForUser(author, ANONYMOUS_USER, lang)),
|
||||
)}\n` +
|
||||
(commissioner ? `${t(lang, "story/commissioned_by", getNameForUser(commissioner, anonymousUser, lang))}\n` : "") +
|
||||
(requester ? `${t(lang, "story/requested_by", getNameForUser(requester, anonymousUser, lang))}\n` : "");
|
||||
(commissioner ? `${t(lang, "story/commissioned_by", getNameForUser(commissioner, ANONYMOUS_USER, lang))}\n` : "") +
|
||||
(requester ? `${t(lang, "story/requested_by", getNameForUser(requester, ANONYMOUS_USER, lang))}\n` : "");
|
||||
|
||||
const storyText = `${storyHeader}\n===\n\n${story.body.replaceAll(/\\([=*])/g, "$1")}`
|
||||
.replaceAll(/\n\n\n+/g, "\n\n")
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import sanitizeHtml from "sanitize-html";
|
|||
import { t } from "../i18n";
|
||||
import type { Lang } from "../content/config";
|
||||
import { markdownToPlaintext } from "../utils/markdown_to_plaintext";
|
||||
import { getUsernameForLang } from "../utils/get_username_for_lang";
|
||||
|
||||
type FeedItem = RSSFeedItem & {
|
||||
pubDate: Date;
|
||||
|
|
@ -20,7 +21,7 @@ function toNoonUTCDate(date: Date) {
|
|||
}
|
||||
|
||||
const getLinkForUser = (user: CollectionEntry<"users">, lang: Lang) => {
|
||||
const userName = user.data.nameLang[lang] || user.data.name;
|
||||
const userName = getUsernameForLang(user, lang);
|
||||
if (user.data.preferredLink) {
|
||||
const link = user.data.links[user.data.preferredLink]!;
|
||||
return `<a href="${typeof link === "string" ? link : link[0]}">${userName}</a>`;
|
||||
|
|
@ -40,7 +41,7 @@ export const GET: APIRoute = async ({ site }) => {
|
|||
return rss({
|
||||
title: "Gallery | Bad Manners",
|
||||
description: "Stories, games, and (possibly) more by Bad Manners",
|
||||
site: site as URL,
|
||||
site: site!,
|
||||
items: [
|
||||
await Promise.all(
|
||||
stories.map<Promise<FeedItem>>(async ({ data, slug, body }) => ({
|
||||
|
|
@ -48,7 +49,7 @@ export const GET: APIRoute = async ({ site }) => {
|
|||
pubDate: toNoonUTCDate(data.pubDate!),
|
||||
link: `/stories/${slug}`,
|
||||
description:
|
||||
`${t(data.lang, "story/warnings", data.wordCount, data.contentWarning.trim())}\n\n${markdownToPlaintext(data.description)}`
|
||||
`${t(data.lang, "story/warnings", data.wordCount || "???", data.contentWarning.trim())}\n\n${markdownToPlaintext(data.description)}`
|
||||
.replaceAll(/[\n ]+/g, " ")
|
||||
.trim(),
|
||||
categories: ["story"],
|
||||
|
|
@ -72,7 +73,7 @@ export const GET: APIRoute = async ({ site }) => {
|
|||
(data.commissioner
|
||||
? `<p>${t(data.lang, "export_story/commissioned_by", getLinkForUser(users.find((user) => user.id === data.commissioner!.id)!, data.lang))}</p>`
|
||||
: "") +
|
||||
`<hr><p><em>${t(data.lang, "story/warnings", data.wordCount, data.contentWarning.trim())}</em></p>` +
|
||||
`<hr><p><em>${t(data.lang, "story/warnings", data.wordCount || "???", data.contentWarning.trim())}</em></p>` +
|
||||
`<hr>${await markdown(body)}` +
|
||||
`<hr>${await markdown(data.description)}`,
|
||||
),
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ const latestItems: LatestItemsEntry[] = [
|
|||
thumbnail: story.data.thumbnail,
|
||||
href: `/stories/${story.slug}`,
|
||||
title: story.data.title,
|
||||
altText: t(story.data.lang, "story/warnings", story.data.wordCount, story.data.contentWarning.trim()),
|
||||
altText: t(story.data.lang, "story/warnings", story.data.wordCount || "???", story.data.contentWarning.trim()),
|
||||
pubDate: story.data.pubDate!,
|
||||
})),
|
||||
games.map<LatestItemsEntry>((game) => ({
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ const totalPages = Math.ceil(page.total / page.size);
|
|||
<p class="text-center font-light text-stone-950 dark:text-white">
|
||||
{
|
||||
page.start == page.end
|
||||
? `Displaying story ${page.start + 1}`
|
||||
: `Displaying stories ${page.start + 1} - ${page.end + 1}`
|
||||
? `Displaying story #${page.start + 1}`
|
||||
: `Displaying stories #${page.start + 1}–${page.end + 1}`
|
||||
} / {page.total}
|
||||
</p>
|
||||
<div class="mx-auto mb-6 mt-2 flex w-fit rounded-lg border border-stone-400 dark:border-stone-500">
|
||||
|
|
@ -71,7 +71,12 @@ const totalPages = Math.ceil(page.total / page.size);
|
|||
<a
|
||||
class="text-link hover:underline focus:underline"
|
||||
href={`/stories/${story.slug}`}
|
||||
title={t(story.data.lang, "story/warnings", story.data.wordCount, story.data.contentWarning.trim())}
|
||||
title={t(
|
||||
story.data.lang,
|
||||
"story/warnings",
|
||||
story.data.wordCount || "???",
|
||||
story.data.contentWarning.trim(),
|
||||
)}
|
||||
>
|
||||
{story.data.thumbnail ? (
|
||||
<div class="flex aspect-square max-w-[192px] justify-center">
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ export const getStaticPaths: GetStaticPaths = async () => {
|
|||
|
||||
const { tag, description, stories, games, related } = Astro.props;
|
||||
if (!description) {
|
||||
console.warn(`Tag "${tag}" has no description!`);
|
||||
console.log(`Tag "${tag}" has no description`);
|
||||
}
|
||||
const count = stories.length + games.length;
|
||||
let totalWorksWithTag: string = "";
|
||||
|
|
@ -132,7 +132,12 @@ if (count == 1) {
|
|||
<a
|
||||
class="text-link hover:underline focus:underline"
|
||||
href={`/stories/${story.slug}`}
|
||||
title={t(story.data.lang, "story/warnings", story.data.wordCount, story.data.contentWarning.trim())}
|
||||
title={t(
|
||||
story.data.lang,
|
||||
"story/warnings",
|
||||
story.data.wordCount || "???",
|
||||
story.data.contentWarning.trim(),
|
||||
)}
|
||||
>
|
||||
{story.data.thumbnail ? (
|
||||
<div class="flex aspect-square max-w-[192px] justify-center">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue