diff --git a/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs b/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs index e575dd03..cc24a87f 100644 --- a/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs +++ b/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs @@ -1,8 +1,10 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// Copyright (c) Alexandre Mutel. All rights reserved. // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using System; +using System.Collections.Generic; +using System.Linq; using Markdig.Renderers; using Markdig.Renderers.Html; using Markdig.Renderers.Html.Inlines; @@ -47,88 +49,125 @@ namespace Markdig.Extensions.MediaLinks private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline) { - if (linkInline.IsImage && linkInline.Url != null) + if (!linkInline.IsImage || linkInline.Url == null) { - Uri uri; - // Only process absolute Uri - if (Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out uri) && uri.IsAbsoluteUri) + return false; + } + + Uri uri; + // Only process absolute Uri + if (!Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out uri) || !uri.IsAbsoluteUri) + { + return false; + } + + if (TryRenderIframeFromKnownProviders(uri, renderer, linkInline)) + { + return true; + } + + if (TryGuessAudioVideoFile(uri, renderer, linkInline)) + { + return true; + } + + return false; + } + + private static HtmlAttributes GetHtmlAttributes(LinkInline linkInline) + { + var htmlAttributes = new HtmlAttributes(); + var fromAttributes = linkInline.TryGetAttributes(); + if (fromAttributes != null) + { + fromAttributes.CopyTo(htmlAttributes, false, false); + } + + return htmlAttributes; + } + + private bool TryGuessAudioVideoFile(Uri uri, HtmlRenderer renderer, LinkInline linkInline) + { + var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); + // Otherwise try to detect if we have an audio/video from the file extension + string mimeType; + var lastDot = path.LastIndexOf('.'); + if (lastDot >= 0 && + Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out mimeType)) + { + var htmlAttributes = GetHtmlAttributes(linkInline); + var isAudio = mimeType.StartsWith("audio"); + var tagType = isAudio ? "audio" : "video"; + + renderer.Write($"<{tagType}"); + htmlAttributes.AddPropertyIfNotExist("width", Options.Width); + if (!isAudio) { - var htmlAttributes = new HtmlAttributes(); - var fromAttributes = linkInline.TryGetAttributes(); - if (fromAttributes != null) - { - fromAttributes.CopyTo(htmlAttributes, false, false); - } - - // TODO: this code is not pluggable, so for now, we handle only the following web providers: - // - youtube - // - vimeo - var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); - - string iFrameUrl = null; - if (uri.Host.StartsWith("www.youtube.com", StringComparison.OrdinalIgnoreCase)) - { - var query = SplitQuery(uri); - if (query.Length > 0 && query[0].StartsWith("v=")) - { - iFrameUrl = $"https://www.youtube.com/embed/{query[0].Substring(2)}"; - } - } - else if (uri.Host.StartsWith("vimeo.com", StringComparison.OrdinalIgnoreCase)) - { - var items = path.Split('/'); - if (items.Length > 0) - { - iFrameUrl = $"https://player.vimeo.com/video/{items[items.Length - 1]}"; - } - } - - if (iFrameUrl != null) - { - renderer.Write($""); - return true; - } - else - { - // Otherwise try to detect if we have an audio/video from the file extension - string mimeType; - var lastDot = path.LastIndexOf('.'); - if (lastDot >= 0 && - Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out mimeType)) - { - var isAudio = mimeType.StartsWith("audio"); - var tagType = isAudio ? "audio" : "video"; - - renderer.Write($"<{tagType}"); - htmlAttributes.AddPropertyIfNotExist("width", Options.Width); - if (!isAudio) - { - htmlAttributes.AddPropertyIfNotExist("height", Options.Height); - } - htmlAttributes.AddPropertyIfNotExist("controls", null); - renderer.WriteAttributes(htmlAttributes); - - renderer.Write($">"); - - return true; - } - } + htmlAttributes.AddPropertyIfNotExist("height", Options.Height); } + htmlAttributes.AddPropertyIfNotExist("controls", null); + renderer.WriteAttributes(htmlAttributes); + + renderer.Write($">"); + + return true; } return false; } + #region Known providers + private static readonly Dictionary> KnownHosts = new Dictionary>() + { + {"www.youtube.com", YouTube}, + {"vimeo.com", Vimeo }, + }; + + + private bool TryRenderIframeFromKnownProviders(Uri uri, HtmlRenderer renderer, LinkInline linkInline) + { + var iFrameUrl = + KnownHosts + .Where(pair => uri.Host.StartsWith(pair.Key, StringComparison.OrdinalIgnoreCase)) // when host is match + .Select(pair => pair.Value(uri)) // try to call delegate to get iframeUrl + .FirstOrDefault(iframeSrc => iframeSrc != null); // use first success + + if (iFrameUrl == null) + { + return false; + } + + var htmlAttributes = GetHtmlAttributes(linkInline); + renderer.Write($""); + + return true; + } + private static readonly string[] SplitAnd = {"&"}; private static string[] SplitQuery(Uri uri) { var query = uri.Query.Substring(uri.Query.IndexOf('?') + 1); return query.Split(SplitAnd, StringSplitOptions.RemoveEmptyEntries); } + + private static string YouTube(Uri uri) + { + var query = SplitQuery(uri); + return query.Length > 0 && query[0].StartsWith("v=") + ? $"https://www.youtube.com/embed/{query[0].Substring(2)}" + : null; + } + + private static string Vimeo(Uri uri) + { + var items = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped).Split('/'); + return items.Length > 0 ? $"https://player.vimeo.com/video/{items[items.Length - 1]}" : null; + } + #endregion } } \ No newline at end of file