Add export-story script and draft for Tiny Accident

This commit is contained in:
Bad Manners 2024-03-24 22:48:07 -03:00
parent 7f7a62a391
commit 808f565e59
16 changed files with 678 additions and 15 deletions

10
src/pages/healthcheck.ts Normal file
View file

@ -0,0 +1,10 @@
import type { APIRoute } from "astro";
export const GET: APIRoute = () => {
if (import.meta.env.PROD) {
return new Response(null, { status: 404 });
}
return new Response(JSON.stringify({ isAlive: true }), {
headers: { "Content-Type": "application/json; charset=utf-8" },
});
};

View file

@ -1,8 +1,8 @@
import type { APIRoute, GetStaticPaths } from "astro";
import { type APIRoute, type GetStaticPaths } from "astro";
import { getCollection, getEntry, type CollectionEntry, getEntries } from "astro:content";
import { marked, type RendererApi } from "marked";
import he from "he";
import { type Website } from "../../../../content/config";
import { type Website } from "../../../../../content/config";
const WEBSITE_LIST = ["eka", "furaffinity", "inkbunny", "sofurry", "weasyl"] as const satisfies Website[];

View file

@ -0,0 +1,72 @@
import { type APIRoute, type GetStaticPaths } from "astro";
import { getCollection, getEntry, type CollectionEntry, getEntries } from "astro:content";
import { type Lang } from "../../../../content/config";
type Props = {
story: CollectionEntry<"stories">;
};
type Params = {
slug: CollectionEntry<"stories">["slug"];
};
export const getStaticPaths: GetStaticPaths = async () => {
if (import.meta.env.PROD) {
return [];
}
return (await getCollection("stories")).map((story) => ({
params: { slug: story.slug } satisfies Params,
props: { story } satisfies Props,
}));
};
function getNameForUser(user: CollectionEntry<"users">, anonymousUser: CollectionEntry<"users">, lang: Lang): string {
if (user.data.isAnonymous) {
return anonymousUser.data.nameLang[lang] || anonymousUser.data.name;
}
return user.data.nameLang[lang] || user.data.name;
}
export const GET: APIRoute<Props, Params> = async ({ props: { story } }) => {
const { lang } = story.data;
const anonymousUser = await getEntry("users", "anonymous");
const authorsNames = (await getEntries([story.data.authors].flat())).map((author) =>
getNameForUser(author, anonymousUser, lang),
);
const commissioner = story.data.commissioner && (await getEntry(story.data.commissioner));
const requester = story.data.requester && (await getEntry(story.data.requester));
let storyHeader = `${story.data.title}\n`;
if (lang === "eng") {
let authorsString = `by ${authorsNames[0]}`;
if (authorsNames.length > 2) {
authorsString += `, ${authorsNames.slice(1, authorsNames.length - 1).join(", ")}, and ${authorsNames[authorsNames.length - 1]}`;
} else if (authorsNames.length == 2) {
authorsString += ` and ${authorsNames[1]}`;
}
storyHeader +=
`${authorsString}\n` +
(commissioner ? `Commissioned by ${getNameForUser(commissioner, anonymousUser, lang)}\n` : "") +
(requester ? `Requested by ${getNameForUser(requester, anonymousUser, lang)}\n` : "");
} else if (lang === "tok") {
let authorsString = "lipu ni li tan ";
if (authorsNames.length > 1) {
authorsString += `jan ni: ${authorsNames.join(" en ")}`;
} else {
authorsString += authorsNames[0];
}
if (commissioner) {
throw new Error(`No "commissioner" handler for language "tok"`);
}
if (requester) {
throw new Error(`No "requester" handler for language "tok"`);
}
storyHeader += `${authorsString}\n`;
} else {
throw new Error(`Unknown language "${lang}"`);
}
const storyText = `${storyHeader}\n===\n\n${story.body.replaceAll("\\*", "*").replaceAll("\\=", "=")}`;
const headers = { "Content-Type": "text/plain; charset=utf-8" };
return new Response(`${storyText.replaceAll(/\n\n\n+/g, "\n\n").trim()}\n`, { headers });
};

View file

@ -0,0 +1,27 @@
import { type APIRoute, type GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
type Props = {
story: CollectionEntry<"stories">;
};
type Params = {
slug: CollectionEntry<"stories">["slug"];
};
export const getStaticPaths: GetStaticPaths = async () => {
if (import.meta.env.PROD) {
return [];
}
return (await getCollection("stories")).map((story) => ({
params: { slug: story.slug } satisfies Params,
props: { story } satisfies Props,
}));
};
export const GET: APIRoute<Props, Params> = async ({ props: { story }, redirect }) => {
if (!story.data.thumbnail) {
return new Response(null, { status: 404 });
}
return redirect(story.data.thumbnail.src);
};

View file

@ -0,0 +1,10 @@
---
import GalleryLayout from "../../layouts/GalleryLayout.astro";
const tag = "Digestion";
---
<GalleryLayout pageTitle={`Works tagged "${tag}"`}>
<h1 class="m-2 text-2xl font-semibold text-stone-800 dark:text-stone-100">Works tagged "{tag}"</h1>
<p class="my-4">No.</p>
</GalleryLayout>