Skip to content

Support for hyperlinks attached to images #637

Open
@raymondanythings

Description

@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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions