add readme support
This commit is contained in:
parent
7b30305ac8
commit
30191a5b1f
6 changed files with 176 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { lanyardConfig } from "@config/environment";
|
||||
import { renderEjsTemplate } from "@helpers/ejs";
|
||||
import { getLanyardData } from "@helpers/lanyard";
|
||||
import { getLanyardData, handleReadMe } from "@helpers/lanyard";
|
||||
|
||||
const routeDef: RouteDef = {
|
||||
method: "GET",
|
||||
|
@ -14,7 +14,7 @@ async function handler(request: ExtendedRequest): Promise<Response> {
|
|||
|
||||
if (!data.success) {
|
||||
return await renderEjsTemplate("error", {
|
||||
message: "User not found or Lanyard data unavailable.",
|
||||
message: data.error.message,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -29,20 +29,22 @@ async function handler(request: ExtendedRequest): Promise<Response> {
|
|||
}
|
||||
|
||||
const presence: LanyardData = data.data;
|
||||
const readme: string | Promise<string> | null =
|
||||
await handleReadMe(presence);
|
||||
|
||||
const ejsTemplateData: EjsTemplateData = {
|
||||
title: "User Page",
|
||||
username: presence.discord_user.username,
|
||||
status: presence.discord_status,
|
||||
activities: presence.activities,
|
||||
user: presence.discord_user,
|
||||
|
||||
platform: {
|
||||
desktop: presence.active_on_discord_desktop,
|
||||
mobile: presence.active_on_discord_mobile,
|
||||
web: presence.active_on_discord_web,
|
||||
},
|
||||
|
||||
instance: instance,
|
||||
instance,
|
||||
readme,
|
||||
};
|
||||
|
||||
return await renderEjsTemplate("index", ejsTemplateData);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { lanyardConfig } from "@config/environment";
|
||||
import { renderEjsTemplate } from "@helpers/ejs";
|
||||
import { getLanyardData } from "@helpers/lanyard";
|
||||
import { getLanyardData, handleReadMe } from "@helpers/lanyard";
|
||||
|
||||
const routeDef: RouteDef = {
|
||||
method: "GET",
|
||||
|
@ -12,8 +12,8 @@ async function handler(): Promise<Response> {
|
|||
const data: LanyardResponse = await getLanyardData();
|
||||
|
||||
if (!data.success) {
|
||||
return Response.json(data.error, {
|
||||
status: 500,
|
||||
return await renderEjsTemplate("error", {
|
||||
message: data.error.message,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -28,20 +28,22 @@ async function handler(): Promise<Response> {
|
|||
}
|
||||
|
||||
const presence: LanyardData = data.data;
|
||||
const readme: string | Promise<string> | null =
|
||||
await handleReadMe(presence);
|
||||
|
||||
const ejsTemplateData: EjsTemplateData = {
|
||||
title: "User Page",
|
||||
username: presence.discord_user.username,
|
||||
status: presence.discord_status,
|
||||
activities: presence.activities,
|
||||
user: presence.discord_user,
|
||||
|
||||
platform: {
|
||||
desktop: presence.active_on_discord_desktop,
|
||||
mobile: presence.active_on_discord_mobile,
|
||||
web: presence.active_on_discord_web,
|
||||
},
|
||||
|
||||
instance: instance,
|
||||
instance,
|
||||
readme,
|
||||
};
|
||||
|
||||
return await renderEjsTemplate("index", ejsTemplateData);
|
||||
|
|
|
@ -3,6 +3,11 @@
|
|||
<head data-user-id="<%= user.id %>" data-instance-uri="<%= instance %>">
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<meta property="og:title" content="<%= username %>'s Presence">
|
||||
<meta property="og:image" content="https://cdn.discordapp.com/avatars/<%= user.id %>/<%= user.avatar %>">
|
||||
<meta property="og:description" content="<%= activities[0]?.state || 'Discord Presence' %>">
|
||||
|
||||
<title><%= title %></title>
|
||||
|
||||
<link rel="stylesheet" href="/public/css/index.css">
|
||||
|
@ -99,7 +104,7 @@
|
|||
|
||||
<% if (progress !== null) { %>
|
||||
<div class="progress-bar" data-start="<%= start %>" data-end="<%= end %>">
|
||||
<div class="progress-fill" style="width: <%= progress %>%"></div>
|
||||
<div class="progress-fill" <%= progress !== null ? `style="width: ${progress}%"` : '' %>></div>
|
||||
</div>
|
||||
|
||||
<% if (start && end) { %>
|
||||
|
@ -114,5 +119,11 @@
|
|||
<% }) %>
|
||||
</ul>
|
||||
<% } %>
|
||||
<% if (readme) { %>
|
||||
<h2>Readme</h2>
|
||||
<section class="readme">
|
||||
<div class="markdown-body"><%- readme %></div>
|
||||
</section>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue