import type { NeutralMessage } from "../../types";
function esc(s: string): string {
return s
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function inlineUrl(url?: string, text?: string): string {
const label = esc(text ?? url ?? "");
if (!url) return label;
return `${label}`;
}
export function renderNeutralMessage(message: NeutralMessage): string {
const parts: string[] = [];
const title = inlineUrl(message.url, message.title);
parts.push(`${title}`);
if (message.author) {
const author = message.author.url
? `${esc(message.author.name)}`
: esc(message.author.name);
parts.push(`👤 ${author}`);
}
if (message.description) {
parts.push(esc(message.description));
}
for (const field of message.fields ?? []) {
parts.push(`${esc(field.name)}: ${esc(field.value)}`);
}
const meta: string[] = [];
if (message.footer) meta.push(esc(message.footer));
if (message.timestamp) meta.push(esc(message.timestamp));
if (meta.length > 0) {
parts.push(`${meta.join(" · ")}`);
}
return parts.join("\n");
}