// TODO: Replace mediaCache with media url provider
        internal static string ParseInternalLinks(string text, UrlProvider urlProvider, IPublishedMediaCache mediaCache)
        {
            if (urlProvider == null)
            {
                throw new ArgumentNullException(nameof(urlProvider));
            }
            if (mediaCache == null)
            {
                throw new ArgumentNullException(nameof(mediaCache));
            }

            // Parse internal links
            var tags = LocalLinkPattern.Matches(text);

            foreach (Match tag in tags)
            {
                if (tag.Groups.Count > 0)
                {
                    var id = tag.Groups[1].Value; //.Remove(tag.Groups[1].Value.Length - 1, 1);

                    //The id could be an int or a UDI
                    if (Udi.TryParse(id, out var udi))
                    {
                        var guidUdi = udi as GuidUdi;
                        if (guidUdi != null)
                        {
                            var newLink = "#";
                            if (guidUdi.EntityType == Constants.UdiEntityType.Document)
                            {
                                newLink = urlProvider.GetUrl(guidUdi.Guid);
                            }
                            else if (guidUdi.EntityType == Constants.UdiEntityType.Media)
                            {
                                newLink = mediaCache.GetById(guidUdi.Guid)?.Url;
                            }

                            if (newLink == null)
                            {
                                newLink = "#";
                            }

                            text = text.Replace(tag.Value, "href=\"" + newLink);
                        }
                    }

                    if (int.TryParse(id, out var intId))
                    {
                        var newLink = urlProvider.GetUrl(intId);
                        text = text.Replace(tag.Value, "href=\"" + newLink);
                    }
                }
            }

            return(text);
        }
Пример #2
0
        /// <summary>
        /// Parses the string looking for the {localLink} syntax and updates them to their correct links.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="urlProvider"></param>
        /// <returns></returns>
        public static string ParseInternalLinks(string text, UrlProvider urlProvider)
        {
            if (urlProvider == null)
            {
                throw new ArgumentNullException("urlProvider");
            }

            // Parse internal links
            var tags = LocalLinkPattern.Matches(text);

            foreach (Match tag in tags)
            {
                if (tag.Groups.Count > 0)
                {
                    var id = tag.Groups[1].Value; //.Remove(tag.Groups[1].Value.Length - 1, 1);

                    //The id could be an int or a UDI
                    Udi udi;
                    if (Udi.TryParse(id, out udi))
                    {
                        var guidUdi = udi as GuidUdi;
                        if (guidUdi != null)
                        {
                            var newLink = urlProvider.GetUrl(guidUdi.Guid);
                            text = text.Replace(tag.Value, "href=\"" + newLink);
                        }
                    }
                    int intId;
                    if (int.TryParse(id, out intId))
                    {
                        var newLink = urlProvider.GetUrl(intId);
                        text = text.Replace(tag.Value, "href=\"" + newLink);
                    }
                }
            }

            return(text);
        }