diff --git a/src/Markdig.Tests/Specs/MediaSpecs.md b/src/Markdig.Tests/Specs/MediaSpecs.md index 7b166641..e9bd4910 100644 --- a/src/Markdig.Tests/Specs/MediaSpecs.md +++ b/src/Markdig.Tests/Specs/MediaSpecs.md @@ -12,8 +12,14 @@ Allows to embed audio/video links to popular website:   + + + + .
+ + ```````````````````````````````` \ No newline at end of file diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index 720a2f98..4c71b227 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -19729,14 +19729,20 @@ namespace Markdig.Tests //  // //  + // + //  + // + //  // // Should be rendered as: // // // + // + // Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, "Extensions Media links"); - TestParser.TestSpec("\n\n\n\n", "\n\n", "medialinks|advanced+medialinks"); + TestParser.TestSpec("\n\n\n\n\n\n\n\n", "\n\n\n\n", "medialinks|advanced+medialinks"); } } // # Extensions diff --git a/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs b/src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs index e575dd03..9090fc8a 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,170 @@ 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($">