-
-
Notifications
You must be signed in to change notification settings - Fork 569
/
preview-images.ts
56 lines (48 loc) · 1.71 KB
/
preview-images.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import ky from 'ky'
import lqip from 'lqip-modern'
import {
type ExtendedRecordMap,
type PreviewImage,
type PreviewImageMap
} from 'notion-types'
import { getPageImageUrls } from 'notion-utils'
import pMap from 'p-map'
import pMemoize from 'p-memoize'
import { defaultMapImageUrl } from 'react-notion-x'
// NOTE: this is just an example of how to pre-compute preview images.
// Depending on how many images you're working with, this can potentially be
// very expensive to recompute, so in production we recommend that you cache
// the preview image results in a key-value database of your choosing.
// If you're not sure where to start, check out https://github.com/jaredwray/keyv
export async function getPreviewImageMap(
recordMap: ExtendedRecordMap
): Promise<PreviewImageMap> {
const urls: string[] = getPageImageUrls(recordMap, {
mapImageUrl: defaultMapImageUrl
})
const previewImagesMap = Object.fromEntries(
await pMap(urls, async (url) => [url, await getPreviewImage(url)], {
concurrency: 8
})
)
return previewImagesMap
}
async function createPreviewImage(url: string): Promise<PreviewImage | null> {
try {
const body = await ky(url).arrayBuffer()
const result = await lqip(body)
console.log('lqip', { originalUrl: url, ...result.metadata })
return {
originalWidth: result.metadata.originalWidth,
originalHeight: result.metadata.originalHeight,
dataURIBase64: result.metadata.dataURIBase64
}
} catch (err) {
if (err.message === 'Input buffer contains unsupported image format') {
return null
}
console.warn('failed to create preview image', url, err.message)
return null
}
}
export const getPreviewImage = pMemoize(createPreviewImage)