Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
100 changes: 99 additions & 1 deletion plugins/notion/src/blocksToHTML.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BlockObjectResponse, RichTextItemResponse } from "@notionhq/client/build/src/api-endpoints"
import { assert } from "./utils"

const YOUTUBE_ID_REGEX = /(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/))(?<videoId>[^?&]+)/u

export function richTextToHTML(texts: RichTextItemResponse[]) {
return texts
.map(({ plain_text, annotations, href }) => {
Expand Down Expand Up @@ -92,7 +94,28 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {
break
}
case "code":
htmlContent += `<pre><code class="language-${block.code.language.replace(" ", "-")}">${richTextToHTML(block.code.rich_text)}</code></pre>`
const language = CODE_LANGUAGE_MAPPING[block.code.language]

if (language) {
htmlContent += `<pre data-language="${language}"><code>${richTextToHTML(
block.code.rich_text
)}</code></pre>`
} else {
htmlContent += `<pre><code>${richTextToHTML(block.code.rich_text)}</code></pre>`
}
break
case "quote":
htmlContent += `<blockquote>${richTextToHTML(block.quote.rich_text)}</blockquote>`
break
case "video":
if (block.video.type === "external") {
const url = block.video.external.url
const youtubeId = url.match(YOUTUBE_ID_REGEX)?.groups?.videoId
if (youtubeId) {
htmlContent += `<iframe src="https://www.youtube.com/embed/${youtubeId}"></iframe>`
break
}
}
break
default:
// TODO: More block types can be added here!
Expand All @@ -102,3 +125,78 @@ export function blocksToHtml(blocks: BlockObjectResponse[]) {

return htmlContent
}

const CODE_LANGUAGE_MAPPING: Record<string, string | null> = {
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,
}