import { useTranslation } from "../utils/i18n"; import { useNavigate } from "react-router"; import { Comment } from "../network/models"; import { network } from "../network/network"; import Badge from "./badge"; import Markdown from "react-markdown"; export function CommentTile({ comment, elevation, }: { comment: Comment; onUpdated?: () => void; elevation?: "normal" | "high"; }) { const navigate = useNavigate(); const { t } = useTranslation(); const link = `/comments/${comment.id}`; const userLink = `/user/${encodeURIComponent(comment.user.username)}`; // @ts-ignore return ( { e.preventDefault(); navigate(link); }} >

{ e.stopPropagation(); e.preventDefault(); navigate(userLink); }} > {"avatar"} {comment.user.username}

{new Date(comment.created_at).toLocaleDateString()}
{comment.content_truncated ? (
{t("Click to view more")}
) : (
)} ); } function CommentReplies({ comment }: { comment: Comment }) { const { t } = useTranslation(); if (!comment.replies) { return null; } return (
{comment.replies.map((e) => { return (

{e.user.username}: {CommentToPlainText(e.content)}

); })} {comment.reply_count > comment.replies.length ? (

{t("View {count} more replies").replace( "{count}", (comment.reply_count - comment.replies.length).toString(), )}

) : null}
); } function CommentToPlainText(content: string) { // Remove Markdown syntax to convert to plain text return content .replace(/!\[.*?]\(.*?\)/g, "") // Remove images .replace(/\[([^\]]+)]\((.*?)\)/g, "$1") // Convert links to just the text .replace(/[#>*_`~-]/g, "") // Remove other Markdown characters .replace(/\n+/g, " ") // Replace newlines with spaces .trim(); } export function CommentContent({ content }: { content: string }) { const lines = content.split("\n"); for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (!line.endsWith(" ")) { // Ensure that each line ends with two spaces for Markdown to recognize it as a line break lines[i] = line + " "; } } content = lines.join("\n"); return {content}; }