Skip to content

feat: Implement Twitter embed functionality #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": false,
"trailingComma": "es5",
"tabWidth": 2,
"semi": true
}
254 changes: 253 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@notionpresso/api-sdk",
"version": "0.0.1",
"description": "",
"main": "./package/index.js",
"module": "./package/index.js",
"types": "./package/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"sync": "tsc --build .",
Expand All @@ -23,7 +26,8 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@notionhq/client": "^2.2.3"
"@notionhq/client": "^2.2.3",
"cheerio": "^1.0.0"
},
"type": "module",
"exports": {
Expand Down
1 change: 1 addition & 0 deletions src/embed/tweet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./process.js";
22 changes: 22 additions & 0 deletions src/embed/tweet/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { EmbedBlockObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import { convertToNotionpressoTweet } from "./tweet";

export function processTwitterEmbeds(blocks: any[]): any[] {
return blocks.map((block) => {
if ("type" in block) {
if (block.type === "embed") {
const embedBlock = block as EmbedBlockObjectResponse;

if (
embedBlock.embed.url &&
(embedBlock.embed.url.includes("twitter") ||
embedBlock.embed.url.includes("x.com"))
) {
console.log("트위터/X 임베드 변환:", embedBlock.embed.url);
return convertToNotionpressoTweet(embedBlock);
}
}
}
return block;
});
}
27 changes: 27 additions & 0 deletions src/embed/tweet/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const themeScript = `
<script id="notionpresso-tweet-theme-script">
if (!window.notionpressoTweetThemeInitialized) {
window.notionpressoTweetThemeInitialized = true;

function updateTwitterEmbedThemes() {
const theme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.querySelectorAll('.notionpresso-tweet-iframe').forEach(iframe => {
let src = iframe.getAttribute('src');
if (src) {
if (src.includes('theme=')) {
src = src.replace(/theme=(dark|light)/, 'theme=' + theme);
} else {
src += '&theme=' + theme;
}
iframe.setAttribute('src', src);
iframe.parentElement?.setAttribute('data-theme', theme);
}
});
}

updateTwitterEmbedThemes();

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTwitterEmbedThemes);
}
</script>
`;
Loading