From 3c203f3b74cba96e7676943483a312db4dd5b39b Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 10 Nov 2024 16:03:21 +0700 Subject: [PATCH] feat: twitter utils --- .../notion-utils/src/get-page-tweet-ids.ts | 23 +++++++++++++++++ .../notion-utils/src/get-page-tweet-urls.ts | 25 +++++++++++++++++++ packages/notion-utils/src/get-page-tweets.ts | 23 ----------------- packages/notion-utils/src/index.ts | 3 ++- 4 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 packages/notion-utils/src/get-page-tweet-ids.ts create mode 100644 packages/notion-utils/src/get-page-tweet-urls.ts delete mode 100644 packages/notion-utils/src/get-page-tweets.ts diff --git a/packages/notion-utils/src/get-page-tweet-ids.ts b/packages/notion-utils/src/get-page-tweet-ids.ts new file mode 100644 index 000000000..4ed26a08d --- /dev/null +++ b/packages/notion-utils/src/get-page-tweet-ids.ts @@ -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) +} diff --git a/packages/notion-utils/src/get-page-tweet-urls.ts b/packages/notion-utils/src/get-page-tweet-urls.ts new file mode 100644 index 000000000..926ab8f11 --- /dev/null +++ b/packages/notion-utils/src/get-page-tweet-urls.ts @@ -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)) +} diff --git a/packages/notion-utils/src/get-page-tweets.ts b/packages/notion-utils/src/get-page-tweets.ts deleted file mode 100644 index 54fa546c9..000000000 --- a/packages/notion-utils/src/get-page-tweets.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type * as types from 'notion-types' - -/** - * Gets the IDs of all tweets embedded on a page. - */ -export const getPageTweets = (recordMap: types.ExtendedRecordMap): string[] => { - const blockIds = Object.keys(recordMap.block) - const tweetIds: string[] = blockIds - .map((blockId) => { - const block = recordMap.block[blockId]?.value - - if (block?.type === 'tweet') { - const tweetId = block.properties?.source?.[0]?.[0] - - if (tweetId) { - return tweetId - } - } - }) - .filter(Boolean) - - return Array.from(new Set(tweetIds)) -} diff --git a/packages/notion-utils/src/index.ts b/packages/notion-utils/src/index.ts index 0fe518c20..af6ed267e 100644 --- a/packages/notion-utils/src/index.ts +++ b/packages/notion-utils/src/index.ts @@ -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'