feat: spotify embed (#586)

This commit is contained in:
Birdgg
2023-05-25 21:15:01 +01:00
committed by GitHub
parent a892d4d418
commit ddd6d1909c
3 changed files with 53 additions and 2 deletions
@@ -0,0 +1,47 @@
import type { Transformer } from "../rehype-embed"
import { generateIframeHTML, includesSomeOfArray } from "./utils"
const getSpotifyIFrameSrc = (url: URL) => {
const { pathname } = url
const urlString = url.toString()
const type = pathname.split("/")[1].toLowerCase()
const podcastTypes = ["episode", "show"]
if (podcastTypes.includes(type)) {
return urlString.replace(type, `embed-podcast/${type}`)
}
return urlString.replace(type, `embed/${type}`)
}
export const SpotifyTransformer: Transformer = {
name: "Spotify",
shouldTransform(url) {
const { host, pathname } = url
return (
host === "open.spotify.com" &&
!includesSomeOfArray(pathname, ["embed", "embed-podcast"]) &&
includesSomeOfArray(pathname, [
"/album/",
"/artist/",
"/episode/",
"/show/",
"/track/",
"/playlist/",
])
)
},
getHTML(url) {
const iframe = getSpotifyIFrameSrc(url)
return generateIframeHTML({
name: "spotify",
src: iframe,
height: "152px",
width: "100%",
style: "border-radius: 12px;",
allow:
"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
})
},
}
+2
View File
@@ -3,6 +3,7 @@ import { BilibiliTransformer } from "./Bilibili"
import { CodeSandboxTransformer } from "./CodeSandBox"
import { GitHubRepoTransformer } from "./GitHub"
import { NetEaseMusicTransformer } from "./NetEaseMusic"
import { SpotifyTransformer } from "./Spotify"
import { TweetTransformer } from "./Tweet"
import { YouTubeTransformer } from "./YouTube"
@@ -13,4 +14,5 @@ export const transformers: Transformer[] = [
YouTubeTransformer,
NetEaseMusicTransformer,
BilibiliTransformer,
SpotifyTransformer,
]
+4 -2
View File
@@ -1,8 +1,11 @@
import { IframeHTMLAttributes } from "react"
interface IframeProps extends IframeHTMLAttributes<HTMLIFrameElement> {
interface IframeProps
extends Omit<IframeHTMLAttributes<HTMLIFrameElement>, "style"> {
name: string
ratio?: string
/** inline style string */
style?: string
}
export const isHostIncludes = (domain: string, host: string) => {
@@ -18,7 +21,6 @@ export const generateIframeHTML = ({
height,
width,
ratio,
style,
...attributes
}: IframeProps) => {
const iframeAttributes = Object.entries(attributes)