react-notion-x
react-notion-x copied to clipboard
Support for hyperlinks attached to images
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!
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:
- The
imageHyperlinkcondition lacks proper URL validity checks, which could lead to serious issues; - The
getIsURLfunction 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!
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:
- The
imageHyperlinkcondition lacks proper URL validity checks, which could lead to serious issues;- The
getIsURLfunction 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.
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:
- The
imageHyperlinkcondition lacks proper URL validity checks, which could lead to serious issues;- The
getIsURLfunction 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!
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:
- The
imageHyperlinkcondition lacks proper URL validity checks, which could lead to serious issues;- The
getIsURLfunction 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.
I also want to request this feature. It's awesome!