feat: Add shorts embedding.

This commit is contained in:
Caspian
2023-12-20 13:12:49 +00:00
parent 7e48f03917
commit b51d9afc76
5 changed files with 191 additions and 1 deletions
@@ -0,0 +1,54 @@
import { match } from "path-to-regexp"
import type { Transformer } from "../rehype-embed"
import { isHostIncludes } from "./utils"
export const XLogShortsTransformer: Transformer = {
name: "XLogShorts",
shouldTransform(url) {
// There are two patterns for xlog shorts url
const { host, pathname, searchParams } = url
const hasShortsParam = searchParams.get("ct") === "shorts"
/**
* content type param is required
* @example ?ct=shorts
* */
if (!hasShortsParam) return false
// https://xlog.app/site/caspian-3030/AtOAglnMV_N6xslDbfhz4?ct=shorts
if (isHostIncludes("xlog.app", host) && pathname.startsWith("/site/")) {
return true
}
// https://caspian-3030.xlog.app/AtOAglnMV_N6xslDbfhz4?ct=shorts
return /^[a-z0-9-]+\.xlog\.app/.test(host)
},
getHTML(url) {
let slug = ""
let handle = ""
// import { match } from "path-to-regexp"
// https://caspian-3030.xlog.app/AtOAglnMV_N6xslDbfhz4?ct=shorts
let matched = match<{ slug: string }>("/:slug")(url.pathname)
if (matched) {
slug = matched.params.slug
handle = url.host.split(".")[0]
} else {
// https://xlog.app/site/caspian-3030/AtOAglnMV_N6xslDbfhz4?ct=shorts
matched = match<{ slug: string; handle: string }>("/site/:handle/:slug")(
url.pathname,
)
if (!matched) return
slug = matched?.params.slug || ""
// @ts-ignore
handle = matched?.params.handle || ""
}
return `<xlog-shorts slug="${slug}" handle="${handle}"/>`
},
}
+2
View File
@@ -4,12 +4,14 @@ import { CodeSandboxTransformer } from "./CodeSandBox"
import { GitHubRepoTransformer } from "./GitHub"
import { NetEaseMusicTransformer } from "./NetEaseMusic"
import { SpotifyTransformer } from "./Spotify"
import { XLogShortsTransformer } from "./XLogShorts"
import { YouTubeTransformer } from "./YouTube"
export const transformers: Transformer[] = [
CodeSandboxTransformer,
GitHubRepoTransformer,
YouTubeTransformer,
XLogShortsTransformer,
NetEaseMusicTransformer,
BilibiliTransformer,
SpotifyTransformer,
+2
View File
@@ -57,6 +57,7 @@ const Style = dynamic(() => import("~/components/common/Style"))
const Mention = dynamic(() => import("~/components/ui/Mention"))
const Mermaid = dynamic(() => import("~/components/ui/Mermaid"))
const GithubRepo = dynamic(() => import("~/components/ui/GithubRepo"))
const XLogShorts = dynamic(() => import("~/components/ui/XLogShorts"))
const APlayer = dynamic(() => import("~/components/ui/APlayer"))
const DPlayer = dynamic(() => import("~/components/ui/DPlayer"))
@@ -233,6 +234,7 @@ export const renderPageContent = (
audio: APlayer,
video: DPlayer,
"github-repo": GithubRepo,
"xlog-shorts": XLogShorts,
style: Style,
} as any,
})