Skip to content

Support for hyperlinks attached to images #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
raymondanythings opened this issue May 30, 2025 · 2 comments · May be fixed by #640
Open

Support for hyperlinks attached to images #637

raymondanythings opened this issue May 30, 2025 · 2 comments · May be fixed by #640

Comments

@raymondanythings
Copy link

raymondanythings commented May 30, 2025

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!

@LooseLi
Copy link
Contributor

LooseLi commented May 30, 2025

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!

@raymondanythings
Copy link
Author

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 raymondanythings linked a pull request Jun 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants