Skip to content

Commit

Permalink
[remark-wiki-link][xs]: add support youtu.be links
Browse files Browse the repository at this point in the history
  • Loading branch information
olayway committed Sep 26, 2023
1 parent 06d3977 commit 71716ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-terms-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@portaljs/remark-embed': patch
---

Add support for shortened yt links (https://youtu.be).
21 changes: 18 additions & 3 deletions packages/remark-embed/src/lib/remark-embed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { visit } from "unist-util-visit";

// https://youtu.be/vDuh7vDgGIg

// TODO write tests!
function transformer(tree) {
visit(tree, "paragraph", (node) => {
visit(node, "text", (textNode) => {
if (
textNode.value.includes("https://www.youtube.com") &&
(textNode.value.includes("https://www.youtube.com") ||
textNode.value.includes("https://youtu.be")
) &&
!textNode.value.includes("\n")
) {
const urlSplit = textNode.value.split(/[=&]+/);
const iframeUrl = `https://www.youtube.com/embed/${urlSplit[1]}`;
let videoId: string;

if (textNode.value.includes("https://youtu.be")) {
// Extract video ID for short YT URLs
videoId = textNode.value.split("/").pop();
} else {
// Extract video ID for full YT URLs
const urlSplit = textNode.value.split(/[=&]+/);
videoId = urlSplit[1];
}

const iframeUrl = `https://www.youtube.com/embed/${videoId}`;
Object.assign(node, {
...node,
type: "element",
Expand Down

0 comments on commit 71716ab

Please sign in to comment.