seedit icon indicating copy to clipboard operation
seedit copied to clipboard

implement comment.linkHtmlTagName

Open plebe1us opened this issue 6 months ago • 0 comments

we do need to fallback to it, but if the user has defined linkHtmlTagName we dont need to run it

but when it's undefined, we should run it, because it's an optional prop, it's not necessarily defined even if the user posted an image.

this is how I implemented the linkHtmlTagName thing

// cache media type because it takes on average 5ms
const getCommentLinkMediaTypeNoCache = (link) => {
  if (!link) return
  let mime
  try {
    mime = extName(new URL(link).pathname.toLowerCase().replace('/', ''))[0]?.mime
  } catch (e) {
    return
  }
  if (mime?.startsWith('image')) return 'image'
  if (mime?.startsWith('video')) return 'video'
  if (mime?.startsWith('audio')) return 'audio'
}
const getCommentLinkMediaType = memoize(getCommentLinkMediaTypeNoCache, {max: 1000})

export const getCommentMediaType = (comment) => {
  if (!comment?.link) return
  if (comment.linkHtmlTagName === 'img') return 'image'
  if (comment.linkHtmlTagName === 'video') return 'video'
  if (comment.linkHtmlTagName === 'audio') return 'audio'
  // never cache getCommentMediaType, only cache getCommentLinkMediaType
  // which uses extName because it's slow
  return getCommentLinkMediaType(comment.link)
}

plebe1us avatar Aug 12 '25 05:08 plebe1us