import { NO_EXCERPT_WORDING } from '../constants' ; import { formatRelativeTime } from './formatRelativeTime' ; export function getPostShortUrl ( content ) { const shortUrlRegex = /\((upload:\/\/\S+(?:jpe?g|png|gif|heic|heif))\)/g ; const result = content . match ( shortUrlRegex ) ?? undefined ; return result ?. map ( ( item ) => item . replace ( shortUrlRegex , '$1' ) ) ; } function formatTime ( match , s1 ) { return formatRelativeTime ( s1 , true , false ) ; } export function handleSpecialMarkdown ( content ? ) { if ( ! content ) { return '' ; } const message = ( item ) => t ( '> {item} are not supported at this time. To View, please access it on the website.' , { item } ) ; const quoteRegex = /\[(quote)([\s\S]*?)(\[\/\1)\]/g ; let result = content . replace ( quoteRegex , message ( 'Quotes' ) ) ; const toogleRegex = /\[(details)([\s\S]*?)(\[\/\1)\]/g ; result = result . replace ( toogleRegex , message ( 'Toogles' ) ) ; const pollRegex = /\[(poll)([\s\S]*?)(\[\/\1)\]/g ; result = result . replace ( pollRegex , message ( 'Polls' ) ) ; const dateRegex = /\[(?:date=)([\d/-]*) (?:timezone=")(.*?)"\]/g ; result = result . replace ( dateRegex , formatTime ) ; return result ; } type LookUpUrl = { shortUrl : string ; url : string ; } ; export function sortImageUrl ( shortUrls , lookUpUrls ) { let idx ; return shortUrls . map ( ( url ) => { idx = lookUpUrls . findIndex ( ( { shortUrl } ) => shortUrl === url ) ; return idx !== - 1 ? lookUpUrls [ idx ] . url : '' ; } ) ; } type MarkdownWithImage = { content : string ; imageUrl ? : string ; mentionedUsers ? : Array < string > ; } ; export function anchorToMarkdown ( rawContent ) { const imgRegex = /<a[^>]*? href="([^>]+(?:jpe?g|png|gif|heic|heif))".*?>(.*?)<\/a>/g ; let content = rawContent . replace ( imgRegex , '' ) ; const sqBracketImgRegex = /\[\w*\-?\w*\.?(jpe?g|png|gif|heic|heif)?\|\d*\sx\s\d*\]/g ; content = content . replace ( sqBracketImgRegex , '' ) ; const mentionRegex = /<a class=\"mention\".*?>@(.*?)<\/a>/g ; let keywords = content . match ( mentionRegex ) ?? [ ] ; let mentionedUsers = keywords ?. map ( ( item ) => item . replace ( mentionRegex , '$1' ) ) ; content = content . replace ( mentionRegex , '@$1' ) ; const linkRegex = /<a href="(.*?)".*?>(.*?)<\/a>/g ; content = content . replace ( linkRegex , '[$2]($1)' ) ; const emojiRegex = /<img.*?class="emoji.*? alt="(.*?)">/g ; content = content . replace ( emojiRegex , '$1' ) ; const imgRegex2 = /<img.*? src="(\S+(?:jpe?g|png|gif|heic|heif))".*? alt="(.*?)".*?>/g ; content = content . replace ( imgRegex2 , '' ) ; const anchorRegex = /<a.*>([\s\S]*?)<\/a>/g ; content = content . replace ( anchorRegex , '$1' ) ; content = content === '' ? NO_EXCERPT_WORDING : content ; let imagesUrl = rawContent . match ( imgRegex ) ?? [ ] ; let imageUrl = imagesUrl [ 0 ] ? imagesUrl [ 0 ] . replace ( imgRegex , '$1' ) : undefined ; return { content , imageUrl , mentionedUsers } ; }