react-notion-x icon indicating copy to clipboard operation
react-notion-x copied to clipboard

Support for hyperlinks attached to images

Open raymondanythings opened this issue 5 months ago • 2 comments
trafficstars

While using the library, I thought it would be great to have native support for hyperlinks applied directly to images. After reviewing this issue, it seems that links can be supported via image captions. However, using captions to display hyperlinks feels a bit awkward, as it exposes the link directly within the Notion page.

I believe we could add support for actual image hyperlinks by modifying the asset-wrapper file directly. Here’s a snippet showing how this might be implemented:

const caption = block.properties?.caption?.[0]?.[0]
const imageHyperlink = (block as ImageBlock).format?.image_hyperlink

const isURL = getIsURL(block, imageHyperlink, caption)

function getIsURL(block: BaseContentBlock, imageHyperlink?: string, caption?: string): boolean {
  if (block.type !== 'image') {
    return false
  }

  if (imageHyperlink) {
    const id = parsePageId(imageHyperlink, { uuid: true })
    return imageHyperlink.charAt(0) === '/' && !!id
  }

  if (caption) {
    const id = parsePageId(caption, { uuid: true })
    const isPage = caption.charAt(0) === '/' && id
    return !!(isPage || isValidURL(caption))
  }

  return false
}

This is my first time submitting an issue, so please let me know if there's anything I should improve. Thank you!

raymondanythings avatar May 30 '25 08:05 raymondanythings