mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
import { ExtendedRecordMap } from 'notion-types'
|
|
import { fetchTweetAst } from 'static-tweets'
|
|
import pMap from 'p-map'
|
|
|
|
export async function getTweetAstMap(recordMap: ExtendedRecordMap) {
|
|
const blockIds = Object.keys(recordMap.block)
|
|
const tweetIds: string[] = blockIds
|
|
.map((blockId) => {
|
|
const block = recordMap.block[blockId]?.value
|
|
|
|
if (block) {
|
|
if (block.type === 'tweet') {
|
|
const src = block.properties?.source?.[0]?.[0]
|
|
|
|
if (src) {
|
|
const id = src.split('?')[0].split('/').pop()
|
|
if (id) return id
|
|
}
|
|
}
|
|
}
|
|
|
|
return null
|
|
})
|
|
.filter(Boolean)
|
|
|
|
const tweetAsts = await pMap(
|
|
tweetIds,
|
|
async (tweetId) => {
|
|
try {
|
|
return {
|
|
tweetId,
|
|
tweetAst: await fetchTweetAst(tweetId)
|
|
}
|
|
} catch (err) {
|
|
console.error('error fetching tweet info', tweetId, err)
|
|
}
|
|
},
|
|
{
|
|
concurrency: 4
|
|
}
|
|
)
|
|
|
|
const tweetAstMap = tweetAsts.reduce((acc, { tweetId, tweetAst }) => {
|
|
if (tweetAst) {
|
|
return {
|
|
...acc,
|
|
[tweetId]: tweetAst
|
|
}
|
|
} else {
|
|
return acc
|
|
}
|
|
}, {})
|
|
|
|
return tweetAstMap
|
|
}
|