Example #1
0
        public static async Task <string> RenderPostForRSS(this IHtmlHelper helper, PostWithRelatedPostStubs post, string scheme, HostString host, IRetrievePostSlugs postSlugRetriever, ICache cache)
        {
            if (helper == null)
            {
                throw new ArgumentNullException(nameof(helper));
            }
            if (post == null)
            {
                throw new ArgumentNullException(nameof(post));
            }
            if (string.IsNullOrWhiteSpace(scheme))
            {
                throw new ArgumentException("Null/blank/whitespace-only scheme");
            }
            if (!host.HasValue)
            {
                throw new ArgumentException("No host value");
            }
            if (postSlugRetriever == null)
            {
                throw new ArgumentNullException(nameof(postSlugRetriever));
            }
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            var cacheKey   = "PostHelper-RenderPostForRSS-" + post.Id;
            var cachedData = cache[cacheKey];

            if (cachedData != null)
            {
                if ((cachedData is CachablePostContent cachedPostContent) && (cachedPostContent.LastModified >= post.LastModified))
                {
                    return(cachedPostContent.RenderableContent);
                }
                cache.Remove(cacheKey);
            }

            var content = await GetRenderableContent(helper, post, previousPostIfAny : null, nextPostIfAny : null, includeTitle : false, includePostedDate : false, includeTags : false, postSlugRetriever);

            var doc = new HtmlDocument();

            doc.LoadHtml(content);
            MakeUrlsAbsolute(doc, "a", "href", scheme, host);
            MakeUrlsAbsolute(doc, "img", "src", scheme, host);
            using (var writer = new StringWriter())
            {
                doc.Save(writer);
                content = writer.ToString();
            }

            cache[cacheKey] = new CachablePostContent(content, post.LastModified);
            return(content);
        }
Example #2
0
        public static async Task <HtmlString> RenderPost(
            this IHtmlHelper helper,
            PostWithRelatedPostStubs post,
            Post previousPostIfAny,
            Post nextPostIfAny,
            IRetrievePostSlugs postSlugRetriever,
            ICache cache)
        {
            if (helper == null)
            {
                throw new ArgumentNullException(nameof(helper));
            }
            if (post == null)
            {
                throw new ArgumentNullException(nameof(post));
            }
            if (postSlugRetriever == null)
            {
                throw new ArgumentNullException(nameof(postSlugRetriever));
            }
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            var cacheKey = string.Format(
                "PostHelper-RenderPost-{0}-{1}-{2}",
                post.Id,
                (previousPostIfAny == null) ? 0 : previousPostIfAny.Id,
                (nextPostIfAny == null) ? 0 : nextPostIfAny.Id
                );
            var cachedData = cache[cacheKey];

            if (cachedData != null)
            {
                if ((cachedData is CachablePostContent cachedPostContent) && (cachedPostContent.LastModified >= post.LastModified))
                {
                    return(new HtmlString(cachedPostContent.RenderableContent));
                }
                cache.Remove(cacheKey);
            }

            var content = await GetRenderableContent(helper, post, previousPostIfAny, nextPostIfAny, includeTitle : true, includePostedDate : true, includeTags : true, postSlugRetriever);

            cache[cacheKey] = new CachablePostContent(content, post.LastModified);
            return(new HtmlString(content));
        }