From 7aef7acd25963e98df26ec9d00d9843a62d15c86 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 19 May 2023 17:45:19 +0100 Subject: [PATCH] feat: embedding ratio and bilibili embedding --- src/markdown/embed-transformers/Bilibili.ts | 47 +++++++++++++++++++ .../embed-transformers/CodeSandBox.ts | 3 +- src/markdown/embed-transformers/YouTube.ts | 3 +- src/markdown/embed-transformers/index.ts | 2 + src/markdown/embed-transformers/utils.ts | 10 ++-- 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/markdown/embed-transformers/Bilibili.ts diff --git a/src/markdown/embed-transformers/Bilibili.ts b/src/markdown/embed-transformers/Bilibili.ts new file mode 100644 index 00000000..f9d8183f --- /dev/null +++ b/src/markdown/embed-transformers/Bilibili.ts @@ -0,0 +1,47 @@ +import type { Transformer } from "../rehype-embed" +import { generateIframeHTML } from "./utils" + +const getTimeValueInSeconds = (timeValue: string) => { + if (Number(timeValue).toString() === timeValue) { + return timeValue + } + + const { + 2: hours = "0", + 4: minutes = "0", + 6: seconds = "0", + } = timeValue.match(/((\d*)h)?((\d*)m)?((\d*)s)?/)! + + return String((Number(hours) * 60 + Number(minutes)) * 60 + Number(seconds)) +} + +const getBilibiliIFrameSrc = (url: URL) => { + const match = url.pathname.match(/\/video\/([A-Za-z0-9]+)\//) + if (!match || match.length < 1) return + const bvid = match[1] + + const embedUrl = new URL( + `https://player.bilibili.com/player.html?bvid=${bvid}&autoplay=false`, + ) + + return embedUrl.toString() +} + +export const BilibiliTransformer: Transformer = { + name: "Bilibili", + shouldTransform(url) { + const { host, pathname } = url + + return host === "www.bilibili.com" && pathname.includes("/video/BV") + }, + getHTML(url) { + return generateIframeHTML({ + name: "bilibili", + src: getBilibiliIFrameSrc(url), + ratio: "16 / 9", + allow: + "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture", + allowFullScreen: true, + }) + }, +} diff --git a/src/markdown/embed-transformers/CodeSandBox.ts b/src/markdown/embed-transformers/CodeSandBox.ts index e01d1551..3973f682 100644 --- a/src/markdown/embed-transformers/CodeSandBox.ts +++ b/src/markdown/embed-transformers/CodeSandBox.ts @@ -12,8 +12,7 @@ export const CodeSandboxTransformer: Transformer = { return generateIframeHTML({ name: "codesandbox", src: url.toString().replace("/s/", "/embed/"), - height: "500px", - width: "100%", + ratio: "16 / 9", allow: "accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking", sandbox: diff --git a/src/markdown/embed-transformers/YouTube.ts b/src/markdown/embed-transformers/YouTube.ts index 94987af0..e17c0340 100644 --- a/src/markdown/embed-transformers/YouTube.ts +++ b/src/markdown/embed-transformers/YouTube.ts @@ -52,8 +52,7 @@ export const YouTubeTransformer: Transformer = { return generateIframeHTML({ name: "youtube", src: getYouTubeIFrameSrc(url), - width: "100%", - height: "410px", + ratio: "16 / 9", allow: "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture", allowFullScreen: true, diff --git a/src/markdown/embed-transformers/index.ts b/src/markdown/embed-transformers/index.ts index 81a9ab1e..14d2673b 100644 --- a/src/markdown/embed-transformers/index.ts +++ b/src/markdown/embed-transformers/index.ts @@ -1,4 +1,5 @@ import type { Transformer } from "../rehype-embed" +import { BilibiliTransformer } from "./Bilibili" import { CodeSandboxTransformer } from "./CodeSandBox" import { GitHubRepoTransformer } from "./GitHub" import { NetEaseMusicTransformer } from "./NetEaseMusic" @@ -11,4 +12,5 @@ export const transformers: Transformer[] = [ GitHubRepoTransformer, YouTubeTransformer, NetEaseMusicTransformer, + BilibiliTransformer, ] diff --git a/src/markdown/embed-transformers/utils.ts b/src/markdown/embed-transformers/utils.ts index b9ee5dc4..472a0e48 100644 --- a/src/markdown/embed-transformers/utils.ts +++ b/src/markdown/embed-transformers/utils.ts @@ -2,6 +2,7 @@ import { IframeHTMLAttributes } from "react" interface IframeProps extends IframeHTMLAttributes { name: string + ratio?: string } export const isHostIncludes = (domain: string, host: string) => { @@ -16,11 +17,10 @@ export const generateIframeHTML = ({ src, height, width, + ratio, style, ...attributes }: IframeProps) => { - const iframeStyle = - 'style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"' const iframeAttributes = Object.entries(attributes) .map(([key, value]) => { if (key === "allowFullScreen") return key.toLowerCase() @@ -28,7 +28,9 @@ export const generateIframeHTML = ({ }) .join(" ") - return `
- + return `
+
` }