add readme support

This commit is contained in:
creations 2025-04-05 09:18:54 -04:00
parent 7b30305ac8
commit 30191a5b1f
Signed by: creations
GPG key ID: 8F553AA4320FC711
6 changed files with 176 additions and 14 deletions

View file

@ -1,5 +1,6 @@
import { lanyardConfig } from "@config/environment";
import { fetch } from "bun";
import { marked } from "marked";
export async function getLanyardData(id?: string): Promise<LanyardResponse> {
let instance: string = lanyardConfig.instance;
@ -44,3 +45,49 @@ export async function getLanyardData(id?: string): Promise<LanyardResponse> {
return data;
}
export async function handleReadMe(data: LanyardData): Promise<string | null> {
const userReadMe: string | null = data.kv?.readme;
console.log("userReadMe", userReadMe);
if (
!userReadMe ||
!userReadMe.toLowerCase().endsWith("readme.md") ||
!userReadMe.startsWith("http")
) {
return null;
}
try {
const res: Response = await fetch(userReadMe, {
headers: {
Accept: "text/markdown",
},
});
const contentType: string = res.headers.get("content-type") || "";
if (
!res.ok ||
!(
contentType.includes("text/markdown") ||
contentType.includes("text/plain")
)
)
return null;
if (res.headers.has("content-length")) {
const size: number = parseInt(
res.headers.get("content-length") || "0",
10,
);
if (size > 1024 * 100) return null;
}
const text: string = await res.text();
if (!text || text.length < 10) return null;
return marked.parse(text);
} catch {
return null;
}
}