Skip to content

Commit

Permalink
feat: twitter utils
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Nov 10, 2024
1 parent e0d4742 commit 3c203f3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
23 changes: 23 additions & 0 deletions packages/notion-utils/src/get-page-tweet-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type * as types from 'notion-types'

import { getPageTweetUrls } from './get-page-tweet-urls'

/**
* Gets the IDs of all tweets embedded on a page.
*/
export const getPageTweetIds = (
recordMap: types.ExtendedRecordMap
): string[] => {
const tweetUrls = getPageTweetUrls(recordMap)
return tweetUrls
.map((url) => {
try {
const u = new URL(url)
const parts = u.pathname.split('/')
return parts.at(-1)
} catch {
return
}
})
.filter(Boolean)
}
25 changes: 25 additions & 0 deletions packages/notion-utils/src/get-page-tweet-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type * as types from 'notion-types'

/**
* Gets the URLs of all tweets embedded on a page.
*/
export const getPageTweetUrls = (
recordMap: types.ExtendedRecordMap
): string[] => {
const blockIds = Object.keys(recordMap.block)
const tweetUrls: string[] = blockIds
.map((blockId) => {
const block = recordMap.block[blockId]?.value

if (block?.type === 'tweet') {
const tweetUrl = block.properties?.source?.[0]?.[0]

if (tweetUrl) {
return tweetUrl
}
}
})
.filter(Boolean)

return Array.from(new Set(tweetUrls))
}
23 changes: 0 additions & 23 deletions packages/notion-utils/src/get-page-tweets.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/notion-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export * from './get-page-image-urls'
export * from './get-page-property'
export * from './get-page-table-of-contents'
export * from './get-page-title'
export * from './get-page-tweets'
export * from './get-page-tweet-ids'
export * from './get-page-tweet-urls'
export * from './get-text-content'
export * from './id-to-uuid'
export * from './is-url'
Expand Down

0 comments on commit 3c203f3

Please sign in to comment.