feat: embedding ratio and bilibili embedding
This commit is contained in:
@@ -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,
|
||||
})
|
||||
},
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
|
||||
@@ -2,6 +2,7 @@ import { IframeHTMLAttributes } from "react"
|
||||
|
||||
interface IframeProps extends IframeHTMLAttributes<HTMLIFrameElement> {
|
||||
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 `<div class="xlog-post-content-${name}" style="position: relative; width: ${width}; height: ${height}; margin: 1rem auto;">
|
||||
<iframe src="${src}" border="0" ${iframeStyle} ${iframeAttributes}></iframe>
|
||||
return `<div class="xlog-post-content-${name} relative my-4 mx-auto" style="${
|
||||
ratio ? `aspect-ratio: ${ratio};` : `width: ${width}; height: ${height};`
|
||||
}">
|
||||
<iframe class="absolute left-0 top-0 w-full h-full" src="${src}" border="0" ${iframeAttributes}></iframe>
|
||||
</div>`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user