Skip to content

Commit 1a1bbec

Browse files
authored
Merge pull request #786 from MartinZikmund/feature/youtube-short-support
Support for YouTube Shorts embedding
2 parents 8e22754 + 68bd307 commit 1a1bbec

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Markdig.Tests/TestMediaLinks.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null)
2525
[Test]
2626
[TestCase("![static mp4](https://sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"https://sample.com/video.mp4\"></source></video></p>\n")]
2727
[TestCase("![static mp4](//sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"//sample.com/video.mp4\"></source></video></p>\n")]
28+
[TestCase(@"![youtube short](https://www.youtube.com/shorts/6BUptHVuvyI?feature=share)", "<p><iframe src=\"https://www.youtube.com/embed/6BUptHVuvyI\" class=\"youtubeshort\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")]
2829
[TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "<p><iframe src=\"https://www.youtube.com/embed/mswPy5bt3TQ\" class=\"youtube\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")]
2930
[TestCase("![yandex.ru](https://music.yandex.ru/album/411845/track/4402274)", "<p><iframe src=\"https://music.yandex.ru/iframe/#track/4402274/411845/\" class=\"yandex\" width=\"500\" height=\"281\" frameborder=\"0\"></iframe></p>\n")]
3031
[TestCase("![vimeo](https://vimeo.com/8607834)", "<p><iframe src=\"https://player.vimeo.com/video/8607834\" class=\"vimeo\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")]
@@ -33,7 +34,7 @@ private MarkdownPipeline GetPipelineWithBootstrap(MediaOptions options = null)
3334
public void TestBuiltInHosts(string markdown, string expected)
3435
{
3536
string html = Markdown.ToHtml(markdown, GetPipeline());
36-
Assert.AreEqual(html, expected);
37+
Assert.AreEqual(expected, html);
3738
}
3839

3940
[TestCase("![static video relative path](./video.mp4)",
@@ -43,7 +44,7 @@ public void TestBuiltInHosts(string markdown, string expected)
4344
public void TestBuiltInHostsWithRelativePaths(string markdown, string expected)
4445
{
4546
string html = Markdown.ToHtml(markdown, GetPipeline());
46-
Assert.AreEqual(html, expected);
47+
Assert.AreEqual(expected, html);
4748
}
4849

4950
private class TestHostProvider : IHostProvider

src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static IHostProvider Create(string hostPrefix, Func<Uri, string?> handler
5858
internal static Dictionary<string, IHostProvider> KnownHosts { get; }
5959
= new Dictionary<string, IHostProvider>(StringComparer.OrdinalIgnoreCase)
6060
{
61+
["YouTubeShort"] = Create("www.youtube.com", YouTubeShort, iframeClass: "youtubeshort"),
6162
["YouTube"] = Create("www.youtube.com", YouTube, iframeClass: "youtube"),
6263
["YouTubeShortened"] = Create("youtu.be", YouTubeShortened, iframeClass: "youtube"),
6364
["Vimeo"] = Create("vimeo.com", Vimeo, iframeClass: "vimeo"),
@@ -92,6 +93,19 @@ private static string[] SplitQuery(Uri uri)
9293
);
9394
}
9495

96+
private static string? YouTubeShort(Uri uri)
97+
{
98+
string uriPath = uri.AbsolutePath;
99+
bool isYouTubeShort = uriPath.StartsWith("/shorts/", StringComparison.OrdinalIgnoreCase);
100+
if (!isYouTubeShort)
101+
{
102+
return null;
103+
}
104+
105+
var shortId = uriPath.Substring("/shorts/".Length).Split('?').FirstOrDefault(); // the format might be "/shorts/6BUptHVuvyI?feature=share"
106+
return BuildYouTubeIframeUrl(shortId, null);
107+
}
108+
95109
private static string? YouTubeShortened(Uri uri)
96110
{
97111
return BuildYouTubeIframeUrl(

0 commit comments

Comments
 (0)