mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-06-02 15:11:00 +00:00
feat: switch to static tweets
This commit is contained in:
@@ -2,6 +2,8 @@ import { NotionAPI } from 'notion-client'
|
||||
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
|
||||
import { getPreviewImages } from './get-preview-images'
|
||||
import { mapNotionImageUrl } from './map-image-url'
|
||||
import { fetchTweetAst, getTweetAst } from 'static-tweets'
|
||||
import pMap from 'p-map'
|
||||
|
||||
const notion = new NotionAPI({
|
||||
apiBaseUrl: process.env.NOTION_API_BASE_URL
|
||||
@@ -47,6 +49,50 @@ export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
|
||||
const previewImageMap = await getPreviewImages(urls)
|
||||
;(recordMap as any).preview_images = previewImageMap
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.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.filter(Boolean).reduce(
|
||||
(acc, t) => ({
|
||||
...acc,
|
||||
[t.tweetId]: t.tweetAst
|
||||
}),
|
||||
{}
|
||||
)
|
||||
|
||||
;(recordMap as any).tweetAstMap = tweetAstMap
|
||||
|
||||
return recordMap
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user