Skip to content

Commit

Permalink
Add support for YouTube Shorts embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Apr 1, 2024
1 parent e486903 commit 68bd307
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static IHostProvider Create(string hostPrefix, Func<Uri, string?> handler
internal static Dictionary<string, IHostProvider> KnownHosts { get; }
= new Dictionary<string, IHostProvider>(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"),
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 68bd307

Please sign in to comment.