From e486903687fd52ae90598fef4d03c4a26251d958 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Mon, 1 Apr 2024 17:42:12 +0200 Subject: [PATCH 1/2] Test support for YouTube Shorts embedding --- src/Markdig.Tests/TestMediaLinks.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Markdig.Tests/TestMediaLinks.cs b/src/Markdig.Tests/TestMediaLinks.cs index 69fff8a5e..90d7f6a40 100644 --- a/src/Markdig.Tests/TestMediaLinks.cs +++ b/src/Markdig.Tests/TestMediaLinks.cs @@ -25,6 +25,7 @@ private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null) [Test] [TestCase("![static mp4](https://sample.com/video.mp4)", "

\n")] [TestCase("![static mp4](//sample.com/video.mp4)", "

\n")] + [TestCase(@"![youtube short](https://www.youtube.com/shorts/6BUptHVuvyI?feature=share)", "

\n")] [TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "

\n")] [TestCase("![yandex.ru](https://music.yandex.ru/album/411845/track/4402274)", "

\n")] [TestCase("![vimeo](https://vimeo.com/8607834)", "

\n")] @@ -33,7 +34,7 @@ private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null) public void TestBuiltInHosts(string markdown, string expected) { string html = Markdown.ToHtml(markdown, GetPipeline()); - Assert.AreEqual(html, expected); + Assert.AreEqual(expected, html); } [TestCase("![static video relative path](./video.mp4)", @@ -43,7 +44,7 @@ public void TestBuiltInHosts(string markdown, string expected) public void TestBuiltInHostsWithRelativePaths(string markdown, string expected) { string html = Markdown.ToHtml(markdown, GetPipeline()); - Assert.AreEqual(html, expected); + Assert.AreEqual(expected, html); } private class TestHostProvider : IHostProvider From 68bd3074b2763611d07f5a806b9e72f986d4ea0c Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Mon, 1 Apr 2024 17:42:46 +0200 Subject: [PATCH 2/2] Add support for YouTube Shorts embedding --- .../Extensions/MediaLinks/HostProviderBuilder.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs b/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs index 419f38d22..38377ac8c 100644 --- a/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs +++ b/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs @@ -58,6 +58,7 @@ public static IHostProvider Create(string hostPrefix, Func handler internal static Dictionary KnownHosts { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase) { + ["YouTubeShort"] = Create("www.youtube.com", YouTubeShort, iframeClass: "youtubeshort"), ["YouTube"] = Create("www.youtube.com", YouTube, iframeClass: "youtube"), ["YouTubeShortened"] = Create("youtu.be", YouTubeShortened, iframeClass: "youtube"), ["Vimeo"] = Create("vimeo.com", Vimeo, iframeClass: "vimeo"), @@ -92,6 +93,19 @@ private static string[] SplitQuery(Uri uri) ); } + private static string? YouTubeShort(Uri uri) + { + string uriPath = uri.AbsolutePath; + bool isYouTubeShort = uriPath.StartsWith("/shorts/", StringComparison.OrdinalIgnoreCase); + if (!isYouTubeShort) + { + return null; + } + + var shortId = uriPath.Substring("/shorts/".Length).Split('?').FirstOrDefault(); // the format might be "/shorts/6BUptHVuvyI?feature=share" + return BuildYouTubeIframeUrl(shortId, null); + } + private static string? YouTubeShortened(Uri uri) { return BuildYouTubeIframeUrl(