Skip to content

Commit

Permalink
generate youtube embed links if needed (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 authored Oct 15, 2024
1 parent 6ddc625 commit 27ac366
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/app/(routes)/blog/[slug]/_components/article-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,32 @@ const markRenderers: RenderMark = {
[MARKS.SUBSCRIPT]: (text) => <sub>{text}</sub>,
};

function createMaybeYoutubeEmbedLink(url: URL) {
if (!url.hostname.includes("youtube.com")) return;

if (url.pathname.includes("embed")) {
// already an embed link
return url.href;
}
const videoId = url.searchParams.get("v");
if (!videoId) return;
return `https://youtube.com/embed/${videoId}`;
}

// add more cases for vimeo or other services as needed
function generateEmbedHref(url: URL) {
return createMaybeYoutubeEmbedLink(url) ?? url.href;
}

const nodeRenderers: RenderNode = {
[INLINES.HYPERLINK]: (node, children) => {
const href = node.data.uri as string;
const url = new URL(node.data.uri as string);

const href = generateEmbedHref(url);

if (
href.includes("youtube.com/embed") ||
href.includes("player.vimeo.com") ||
url.hostname.includes("youtube.com") ||
url.hostname.includes("player.vimeo.com") ||
children?.toString().toLowerCase().includes("iframe") // to handle uncommon cases, creator can set the text to "iframe"
) {
return (
Expand Down

0 comments on commit 27ac366

Please sign in to comment.