Skip to content

Commit 3c203f3

Browse files
feat: twitter utils
1 parent e0d4742 commit 3c203f3

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type * as types from 'notion-types'
2+
3+
import { getPageTweetUrls } from './get-page-tweet-urls'
4+
5+
/**
6+
* Gets the IDs of all tweets embedded on a page.
7+
*/
8+
export const getPageTweetIds = (
9+
recordMap: types.ExtendedRecordMap
10+
): string[] => {
11+
const tweetUrls = getPageTweetUrls(recordMap)
12+
return tweetUrls
13+
.map((url) => {
14+
try {
15+
const u = new URL(url)
16+
const parts = u.pathname.split('/')
17+
return parts.at(-1)
18+
} catch {
19+
return
20+
}
21+
})
22+
.filter(Boolean)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type * as types from 'notion-types'
2+
3+
/**
4+
* Gets the URLs of all tweets embedded on a page.
5+
*/
6+
export const getPageTweetUrls = (
7+
recordMap: types.ExtendedRecordMap
8+
): string[] => {
9+
const blockIds = Object.keys(recordMap.block)
10+
const tweetUrls: string[] = blockIds
11+
.map((blockId) => {
12+
const block = recordMap.block[blockId]?.value
13+
14+
if (block?.type === 'tweet') {
15+
const tweetUrl = block.properties?.source?.[0]?.[0]
16+
17+
if (tweetUrl) {
18+
return tweetUrl
19+
}
20+
}
21+
})
22+
.filter(Boolean)
23+
24+
return Array.from(new Set(tweetUrls))
25+
}

packages/notion-utils/src/get-page-tweets.ts

-23
This file was deleted.

packages/notion-utils/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export * from './get-page-image-urls'
1414
export * from './get-page-property'
1515
export * from './get-page-table-of-contents'
1616
export * from './get-page-title'
17-
export * from './get-page-tweets'
17+
export * from './get-page-tweet-ids'
18+
export * from './get-page-tweet-urls'
1819
export * from './get-text-content'
1920
export * from './id-to-uuid'
2021
export * from './is-url'

0 commit comments

Comments
 (0)