-
Notifications
You must be signed in to change notification settings - Fork 21
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
Notion: import YouTube videos and blockquotes #116
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
We recently updated our CMS export, could you please update the implementation to match the newest formats?
e7f8446
to
28adf80
Compare
28adf80
to
f1f200a
Compare
plugins/notion/src/blocksToHTML.ts
Outdated
const url = block.video.external.url | ||
if (url && (url.includes("youtube.com") || url.includes("youtu.be"))) { | ||
try { | ||
const urlObj = new URL(url) | ||
let videoId = "" | ||
|
||
if (urlObj.hostname === "youtube.com" || urlObj.hostname === "www.youtube.com") { | ||
videoId = urlObj.searchParams.get("v") || "" | ||
} else if (urlObj.hostname === "youtu.be") { | ||
videoId = urlObj.pathname.slice(1) | ||
} | ||
|
||
if (videoId && /^[a-zA-Z0-9_-]{11}$/.test(videoId)) { | ||
const embedUrl = `https://www.youtube.com/embed/${encodeURIComponent( | ||
videoId | ||
)}?controls=0&autoplay=0&loop=0&mute=1` | ||
htmlContent += `<iframe src="${embedUrl}"></iframe>` | ||
} | ||
} catch (error) { | ||
console.warn("Invalid YouTube URL:", url) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially rely on RegEx to simplify this logic:
const VIMEO_ID_REGEX = /vimeo\.com\/(?<videoId>[^?]+)/u
const YOUTUBE_ID_REGEX = /(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/))(?<videoId>[^?&]+)/u
I'll let you adjust the embed src for Vimeo, or you can use Framer's default by not passing any query variables.
const url = block.video.external.url | |
if (url && (url.includes("youtube.com") || url.includes("youtu.be"))) { | |
try { | |
const urlObj = new URL(url) | |
let videoId = "" | |
if (urlObj.hostname === "youtube.com" || urlObj.hostname === "www.youtube.com") { | |
videoId = urlObj.searchParams.get("v") || "" | |
} else if (urlObj.hostname === "youtu.be") { | |
videoId = urlObj.pathname.slice(1) | |
} | |
if (videoId && /^[a-zA-Z0-9_-]{11}$/.test(videoId)) { | |
const embedUrl = `https://www.youtube.com/embed/${encodeURIComponent( | |
videoId | |
)}?controls=0&autoplay=0&loop=0&mute=1` | |
htmlContent += `<iframe src="${embedUrl}"></iframe>` | |
} | |
} catch (error) { | |
console.warn("Invalid YouTube URL:", url) | |
} | |
const url = block.video.external.url | |
const vimeoId = url.match(VIMEO_ID_REGEX)?.groups?.videoId | |
const youtubeId = url.match(YOUTUBE_ID_REGEX)?.groups?.videoId | |
if (vimeoId) { | |
htmlContent += `<iframe src="https://player.vimeo.com/video/${vimeoId}"></iframe>` | |
break | |
} else if (youtubeId) { | |
htmlContent += `<iframe src="https://www.youtube.com/embed/${youtubeId}"></iframe>` | |
break | |
} | |
console.warn("Unsupported video URL:", block.video.external.url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, this is much simpler. I updated it to use your code for YouTube videos, but left Vimeo videos out because it shows this in the formatted text when importing videos from Vimeo:
Since Vimeo videos aren't supported in the formatted text editor yet, we can leave it out for now and add it later when it becomes officially supported.
Description
This pull request adds support for importing YouTube videos and blockquotes from Notion in formatted text.
Blockquotes are added with the
<blockquote>
HTML element.YouTube videos are added with the
<template>
element and the YouTube component's identifier, which is the same way the CMS Export plugin exports YouTube videos to HTML. Having a component identifier hardcoded in the plugin may not be ideal, but as far as I can tell it works without any issues.Other Notion video types (Vimeo and file upload) are not imported because Framer does not support those in formatted text yet.
Testing