mirror of
https://github.com/revoltchat/revite.git
synced 2024-11-22 15:10:57 -05:00
feat: add markdown formatting for messages containing timestamps (#482)
This commit is contained in:
parent
69b430d886
commit
5735020013
1 changed files with 31 additions and 0 deletions
|
@ -26,6 +26,8 @@ import { emojiDictionary } from "../../assets/emojis";
|
||||||
import { MarkdownProps } from "./Markdown";
|
import { MarkdownProps } from "./Markdown";
|
||||||
import Prism from "./prism";
|
import Prism from "./prism";
|
||||||
|
|
||||||
|
import { dayjs } from "../../context/Locale";
|
||||||
|
|
||||||
// TODO: global.d.ts file for defining globals
|
// TODO: global.d.ts file for defining globals
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
|
@ -121,6 +123,8 @@ const RE_TWEMOJI = /:(\w+):/g;
|
||||||
// ! FIXME: Move to library
|
// ! FIXME: Move to library
|
||||||
const RE_CHANNELS = /<#([A-z0-9]{26})>/g;
|
const RE_CHANNELS = /<#([A-z0-9]{26})>/g;
|
||||||
|
|
||||||
|
const RE_TIME = /<t:([0-9]+):(\w)>/g;
|
||||||
|
|
||||||
export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) {
|
export default function Renderer({ content, disallowBigEmoji }: MarkdownProps) {
|
||||||
const client = useContext(AppContext);
|
const client = useContext(AppContext);
|
||||||
const { openLink } = useIntermediate();
|
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 replace the message with the mention at the time of render.
|
||||||
// We don't care if the mention changes.
|
// We don't care if the mention changes.
|
||||||
const newContent = content
|
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[]) => {
|
.replace(RE_MENTIONS, (sub: string, ...args: unknown[]) => {
|
||||||
const id = args[0] as string,
|
const id = args[0] as string,
|
||||||
user = client.users.get(id);
|
user = client.users.get(id);
|
||||||
|
|
Loading…
Reference in a new issue