示例#1
0
        private string Convert(object source, bool preview)
        {
            if (source == null)
            {
                return(null);
            }

            var sourceString = source.ToString();

            // ensures string is parsed for {localLink} and URLs and media are resolved correctly
            sourceString = _linkParser.EnsureInternalLinks(sourceString, preview);
            sourceString = _urlParser.EnsureUrls(sourceString);
            sourceString = _imageSourceParser.EnsureImageSources(sourceString);

            // ensure string is parsed for macros and macros are executed correctly
            sourceString = RenderRteMacros(sourceString, preview);

            // find and remove the rel attributes used in the Umbraco UI from img tags
            var doc = new HtmlDocument();

            doc.LoadHtml(sourceString);

            if (doc.ParseErrors.Any() == false && doc.DocumentNode != null)
            {
                // Find all images with rel attribute
                var imgNodes = doc.DocumentNode.SelectNodes("//img[@rel]");

                var modified = false;
                if (imgNodes != null)
                {
                    foreach (var img in imgNodes)
                    {
                        var nodeId = img.GetAttributeValue("rel", string.Empty);
                        if (int.TryParse(nodeId, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
                        {
                            img.Attributes.Remove("rel");
                            modified = true;
                        }
                    }
                }

                // Find all a and img tags with a data-udi attribute
                var dataUdiNodes = doc.DocumentNode.SelectNodes("(//a|//img)[@data-udi]");
                if (dataUdiNodes != null)
                {
                    foreach (var node in dataUdiNodes)
                    {
                        node.Attributes.Remove("data-udi");
                        modified = true;
                    }
                }

                if (modified)
                {
                    return(doc.DocumentNode.OuterHtml);
                }
            }

            return(sourceString);
        }
        public override object?ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object?source, bool preview)
        {
            if (source == null)
            {
                return(null);
            }
            var sourceString = source.ToString();

            // ensures string is parsed for {localLink} and URLs are resolved correctly
            sourceString = _linkParser.EnsureInternalLinks(sourceString !, preview);
            sourceString = _urlParser.EnsureUrls(sourceString);

            return(sourceString);
        }