public static IHtmlString RawButWithMappedUrls(this HtmlHelper helper, string value)
        {
            var formattedString = value;

            var linkFragments = new HtmlStreamReader(value).OfType <ElementFragment>().Where(f => f.NameEquals("a")).ToList();

            if (linkFragments.Any())
            {
                var linkMapper = ServiceLocator.Current.GetInstance <IPermanentLinkMapper>();
                foreach (var linkFragment in linkFragments)
                {
                    if (linkFragment.HasAttributes)
                    {
                        var attribute = linkFragment.Attributes["href"];
                        if (PermanentLinkUtility.IsMappableUrl(new UrlBuilder(attribute.UnquotedValue)))
                        {
                            var url       = new UrlFragment(attribute.UnquotedValue, linkMapper);
                            var mappedUrl = MapUrlFromRoute(helper.ViewContext.RequestContext, helper.RouteCollection, url.GetViewFormat());
                            if (!string.IsNullOrEmpty(mappedUrl))
                            {
                                formattedString = value.Replace(attribute.UnquotedValue, mappedUrl);
                            }
                        }
                    }
                }
            }
            return(new HtmlString(formattedString));
        }
        private bool TryConvertUrlFragment(UrlFragment fragment, out UrlFragment outFragment)
        {
            if ((fragment?.ReferencedPermanentLinkIds).IsNullOrEmpty())
            {
                // Not an internal EPiServer URL.
                outFragment = null;
                return(false);
            }

            // We need to keep EPiServer's Context Mode while converting URLs (View mode, Edit mode, Preview mode).
            var mode        = _httpContextBase?.Request?.RequestContext?.GetContextMode() ?? ContextMode.Default;
            var internalUrl = new UrlBuilder(fragment.InternalFormat);
            var friendlyUrl = _urlResolver.GetUrl(internalUrl, mode);

            outFragment = new UrlFragment(friendlyUrl);

            return(true);
        }