Skip to content

implement comment.linkHtmlTagName #757

@plebe1us

Description

@plebe1us

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)
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

Status

Low Priority

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions