From 5735020013a73307fdea116f5fd1b30a3677535c Mon Sep 17 00:00:00 2001 From: Ryan Alexander <38785445+ryanalexander@users.noreply.github.com> Date: Tue, 4 Jan 2022 20:53:12 +1000 Subject: [PATCH] feat: add markdown formatting for messages containing timestamps (#482) --- src/components/markdown/Renderer.tsx | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/components/markdown/Renderer.tsx b/src/components/markdown/Renderer.tsx index 2549407b..7b5aec6c 100644 --- a/src/components/markdown/Renderer.tsx +++ b/src/components/markdown/Renderer.tsx @@ -26,6 +26,8 @@ import { emojiDictionary } from "../../assets/emojis"; import { MarkdownProps } from "./Markdown"; import Prism from "./prism"; +import { dayjs } from "../../context/Locale"; + // TODO: global.d.ts file for defining globals declare global { interface Window { @@ -121,6 +123,8 @@ const RE_TWEMOJI = /:(\w+):/g; // ! FIXME: Move to library const RE_CHANNELS = /<#([A-z0-9]{26})>/g; +const RE_TIME = //g; + export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) { const client = useContext(AppContext); const { openLink } = useIntermediate(); @@ -131,6 +135,33 @@ export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) { // We replace the message with the mention at the time of render. // We don't care if the mention changes. const newContent = content + .replace(RE_TIME, (sub: string, ...args: unknown[]) => { + if (isNaN(args[0] as string)) return sub; + const date = dayjs.unix(args[0] as number); + const format = args[1] as string; + let final = ""; + switch (format) { + case "t": + final = date.format("hh:mm"); + break; + case "T": + final = date.format("hh:mm:ss"); + break; + case "R": + final = date.fromNow(); + break; + case "D": + final = date.format("DD MMMM YYYY"); + break; + case "F": + final = date.format("dddd, DD MMMM YYYY hh:mm"); + break; + default: + final = date.format("DD MMMM YYYY hh:mm"); + break; + } + return `\`${final}\``; + }) .replace(RE_MENTIONS, (sub: string, ...args: unknown[]) => { const id = args[0] as string, user = client.users.get(id);