Skip to content
Closed
Changes from 1 commit
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
108 changes: 107 additions & 1 deletion plugins/notion/src/blocksToHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,38 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {
break
}
case "code":
htmlContent += `<pre><code class="language-${block.code.language.replace(" ", "-")}">${richTextToHTML(block.code.rich_text)}</code></pre>`
htmlContent += `<pre data-language="${
CODE_LANGUAGE_MAPPING[block.code.language] || "JavaScript"
}"><code>${richTextToHTML(block.code.rich_text)}</code></pre>`
Comment thread
madebyisaacr marked this conversation as resolved.
Outdated
break
Comment thread
madebyisaacr marked this conversation as resolved.
case "quote":
htmlContent += `<blockquote>${richTextToHTML(block.quote.rich_text)}</blockquote>`
break
case "video":
if (block.video.type === "external") {
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)
}

Copy link
Copy Markdown
Member

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.

Suggested change
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)

Copy link
Copy Markdown
Collaborator Author

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:

image

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.

}
}
break
Comment thread
madebyisaacr marked this conversation as resolved.
default:
// TODO: More block types can be added here!
Expand All @@ -102,3 +133,78 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {

return htmlContent
}

const CODE_LANGUAGE_MAPPING = {
abap: null,
arduino: null,
bash: "Shell",
basic: null,
c: "C",
clojure: null,
coffeescript: null,
"c++": "C++",
"c#": "C#",
css: "CSS",
dart: null,
diff: null,
docker: null,
elixir: null,
elm: null,
erlang: null,
flow: null,
fortran: null,
"f#": null,
gherkin: null,
glsl: null,
go: "Go",
graphql: null,
groovy: null,
haskell: "Haskell",
html: "HTML",
java: "Java",
javascript: "JavaScript",
json: null,
julia: "Julia",
kotlin: "Kotlin",
latex: null,
less: "Less",
lisp: null,
livescript: null,
lua: "Lua",
makefile: null,
markdown: "Markdown",
markup: null,
matlab: "MATLAB",
mermaid: null,
nix: null,
"objective-c": "Objective-C",
ocaml: null,
pascal: null,
perl: "Perl",
php: "PHP",
"plain text": null,
powershell: null,
prolog: null,
protobuf: null,
python: "Python",
r: null,
reason: null,
ruby: "Ruby",
rust: "Rust",
sass: null,
scala: "Scala",
scheme: null,
scss: "SCSS",
shell: "Shell",
sql: "SQL",
swift: "Swift",
typescript: "TypeScript",
"vb.net": null,
verilog: null,
vhdl: null,
"visual basic": null,
webassembly: null,
xml: null,
yaml: "YAML",
"java/c/c++/c#": null,
}