mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 23:16:47 +00:00
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { Block } from 'notion-types'
|
|
import { imageCDNHost } from './config'
|
|
|
|
export const mapNotionImageUrl = (url: string, block: Block) => {
|
|
if (!url) {
|
|
return null
|
|
}
|
|
|
|
if (url.startsWith('data:')) {
|
|
return url
|
|
}
|
|
|
|
if (imageCDNHost && url.startsWith(imageCDNHost)) {
|
|
return url
|
|
}
|
|
|
|
// const origUrl = url
|
|
|
|
if (url.startsWith('/images')) {
|
|
url = `https://www.notion.so${url}`
|
|
}
|
|
|
|
// more recent versions of notion don't proxy unsplash images
|
|
if (!url.startsWith('https://images.unsplash.com')) {
|
|
url = `https://www.notion.so${
|
|
url.startsWith('/image') ? url : `/image/${encodeURIComponent(url)}`
|
|
}`
|
|
|
|
const notionImageUrlV2 = new URL(url)
|
|
let table = block.parent_table === 'space' ? 'block' : block.parent_table
|
|
if (table === 'collection') {
|
|
table = 'block'
|
|
}
|
|
notionImageUrlV2.searchParams.set('table', table)
|
|
notionImageUrlV2.searchParams.set('id', block.id)
|
|
notionImageUrlV2.searchParams.set('cache', 'v2')
|
|
|
|
url = notionImageUrlV2.toString()
|
|
}
|
|
|
|
// console.log({ url, origUrl })
|
|
return mapImageUrl(url)
|
|
}
|
|
|
|
export const mapImageUrl = (imageUrl: string) => {
|
|
if (imageUrl.startsWith('data:')) {
|
|
return imageUrl
|
|
}
|
|
|
|
if (imageCDNHost) {
|
|
return `${imageCDNHost}/${encodeURIComponent(imageUrl)}`
|
|
} else {
|
|
return imageUrl
|
|
}
|
|
}
|