コード例 #1
0
        private static HtmlAttributes GetHtmlAttributes(LinkInline linkInline)
        {
            var htmlAttributes = new HtmlAttributes();
            var fromAttributes = linkInline.TryGetAttributes();

            if (fromAttributes != null)
            {
                fromAttributes.CopyTo(htmlAttributes, false, false);
            }

            return(htmlAttributes);
        }
コード例 #2
0
        private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline)
        {
            if (linkInline.IsImage && linkInline.Url != null)
            {
                Uri uri;
                // Only process absolute Uri
                if (Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out uri) && uri.IsAbsoluteUri)
                {
                    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($"<iframe src=\"{iFrameUrl}\"");
                        htmlAttributes.AddPropertyIfNotExist("width", Options.Width);
                        htmlAttributes.AddPropertyIfNotExist("height", Options.Height);
                        htmlAttributes.AddPropertyIfNotExist("frameborder", "0");
                        htmlAttributes.AddPropertyIfNotExist("allowfullscreen", null);
                        renderer.WriteAttributes(htmlAttributes);
                        renderer.Write("></iframe>");
                        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($"><source type=\"{mimeType}\" src=\"{linkInline.Url}\"></source></{tagType}>");
                            renderer.Write("</iframe>");

                            return(true);
                        }
                    }
                }
            }
            return(false);
        }