- realtime.ts: the page's one Centrifuge client, setRealtimeAuth, onUserEvent for user:{me} events the chat does not handle
- chatEvents, chatFormat, chatText, chatUnread, format, media, tabs ported; copy passed in localized
- useChat over useState: channels with join and leave, DMs, member list, unread and mention counts, read marks, typing, presence, polling fallback
- ChatPanel, ChatHeader, ChatMessage, ChatDmList, ChatMemberPicker, ChatMentionPopup, ChatAvatar, ChatIcon, ChatNotices
- EN and PL strings
33 lines
1.4 KiB
TypeScript
33 lines
1.4 KiB
TypeScript
// HTML helpers of the chat text renderer (chatText.ts), ported from a
|
|
// Golem15 SPA's markdown.ts: escaping, the link-target allow-list and
|
|
// bold/italic on escaped text.
|
|
//
|
|
// Links must be http(s), mailto, a site path or an anchor; anything else
|
|
// (javascript:, data:, ...) is refused.
|
|
|
|
const ESC: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
|
|
|
|
export function escapeHtml(s: string): string {
|
|
return s.replace(/[&<>"']/g, (c) => ESC[c])
|
|
}
|
|
|
|
/** Returns the URL when it is allowed as a link target, else null. */
|
|
export function safeUrl(raw: string): string | null {
|
|
// Strip whitespace and control characters browsers ignore inside schemes.
|
|
// eslint-disable-next-line no-control-regex
|
|
const url = raw.trim().replace(/[\u0000-\u001f\u007f\s]+/g, '')
|
|
if (/^https?:\/\/[^/]/i.test(url) || /^mailto:[^\s]+$/i.test(url)) return url
|
|
// A site path: a "/" not followed by another "/" or a backslash (browsers
|
|
// read "/\host" as "//host", an external link).
|
|
if (/^\/(?![/\\])/.test(url) || url.startsWith('#')) return url
|
|
return null
|
|
}
|
|
|
|
/** Bold and italic on already-escaped text (shared with the chat renderer). */
|
|
export function formatSpans(escaped: string): string {
|
|
return escaped
|
|
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
.replace(/(^|[\s(])\*([^*\n]+)\*(?=$|[\s).,;:!?])/g, '$1<em>$2</em>')
|
|
.replace(/(^|[\s(])_([^_\n]+)_(?=$|[\s).,;:!?])/g, '$1<em>$2</em>')
|
|
}
|