feat: spotify embed (#586)
This commit is contained in:
@@ -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",
|
||||
})
|
||||
},
|
||||
}
|
||||
@@ -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,
|
||||
]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user