[remark-wiki-link][xs]: add support youtu.be links
This commit is contained in:
5
.changeset/spicy-terms-worry.md
Normal file
5
.changeset/spicy-terms-worry.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@portaljs/remark-embed': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add support for shortened yt links (https://youtu.be).
|
||||||
@@ -1,14 +1,29 @@
|
|||||||
import { visit } from "unist-util-visit";
|
import { visit } from "unist-util-visit";
|
||||||
|
|
||||||
|
// https://youtu.be/vDuh7vDgGIg
|
||||||
|
|
||||||
|
// TODO write tests!
|
||||||
function transformer(tree) {
|
function transformer(tree) {
|
||||||
visit(tree, "paragraph", (node) => {
|
visit(tree, "paragraph", (node) => {
|
||||||
visit(node, "text", (textNode) => {
|
visit(node, "text", (textNode) => {
|
||||||
if (
|
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")
|
!textNode.value.includes("\n")
|
||||||
) {
|
) {
|
||||||
const urlSplit = textNode.value.split(/[=&]+/);
|
let videoId: string;
|
||||||
const iframeUrl = `https://www.youtube.com/embed/${urlSplit[1]}`;
|
|
||||||
|
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, {
|
Object.assign(node, {
|
||||||
...node,
|
...node,
|
||||||
type: "element",
|
type: "element",
|
||||||
|
|||||||
Reference in New Issue
Block a user