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 6 months ago • 5 comments

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

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!

I think your suggestion is excellent! I've reviewed the code you shared and fully understand its purpose. I believe there are a couple of areas that could be improved:

  1. The imageHyperlink condition lacks proper URL validity checks, which could lead to serious issues;
  2. The getIsURL function only returns a boolean, but in practice, we often need the actual URL as well—this forces external code to repeat similar logic.

If you're open to it, I'd be happy to submit a PR with these improvements and include you as a co-author!

LooseLi avatar May 30 '25 10:05 LooseLi

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!

I think your suggestion is excellent! I've reviewed the code you shared and fully understand its purpose. I believe there are a couple of areas that could be improved:

  1. The imageHyperlink condition lacks proper URL validity checks, which could lead to serious issues;
  2. The getIsURL function only returns a boolean, but in practice, we often need the actual URL as well—this forces external code to repeat similar logic.

If you're open to it, I'd be happy to submit a PR with these improvements and include you as a co-author!

Thank you for your response! I'll go ahead and submit a PR that includes the improvements you mentioned.

raymondanythings avatar Jun 01 '25 10:06 raymondanythings

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!

I think your suggestion is excellent! I've reviewed the code you shared and fully understand its purpose. I believe there are a couple of areas that could be improved:

  1. The imageHyperlink condition lacks proper URL validity checks, which could lead to serious issues;
  2. The getIsURL function only returns a boolean, but in practice, we often need the actual URL as well—this forces external code to repeat similar logic.

If you're open to it, I'd be happy to submit a PR with these improvements and include you as a co-author!

Thank you for your response! I'll go ahead and submit a PR that includes the improvements you mentioned.

Hi @LooseLi !

I've submitted the PR incorporating the improvements you suggested.

Please review when you have a moment!

raymondanythings avatar Jun 30 '25 08:06 raymondanythings

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!

I think your suggestion is excellent! I've reviewed the code you shared and fully understand its purpose. I believe there are a couple of areas that could be improved:

  1. The imageHyperlink condition lacks proper URL validity checks, which could lead to serious issues;
  2. The getIsURL function only returns a boolean, but in practice, we often need the actual URL as well—this forces external code to repeat similar logic.

If you're open to it, I'd be happy to submit a PR with these improvements and include you as a co-author!

Thank you for your response! I'll go ahead and submit a PR that includes the improvements you mentioned.

Hi @LooseLi !

I've submitted the PR incorporating the improvements you suggested.

Please review when you have a moment!

sorry, I'm just a code contributor to this repository and don't have the permission to review or merge code.

LooseLi avatar Jul 10 '25 02:07 LooseLi

I also want to request this feature. It's awesome!

mommocmoc avatar Oct 13 '25 18:10 mommocmoc