Esempio n. 1
0
        public void GetPostPermalink_returns_relative_url_starts_with_slash()
        {
            var postId    = 1;
            var permalink = BlogRoutes.GetPostPermalink(postId);

            Assert.Equal("/post/1", permalink);
        }
Esempio n. 2
0
        public async Task <BlogPostVM> GetBlogPostVMAsync(BlogPost blogPost)
        {
            var coreSettings = await settingService.GetSettingsAsync <CoreSettings>();

            var blogSettings = await settingService.GetSettingsAsync <BlogSettings>();

            var permalinkPart    = BlogRoutes.GetPostPermalink(blogPost.Id);
            var relativeLink     = BlogRoutes.GetPostRelativeLink(blogPost.CreatedOn, blogPost.Slug);
            var requestHostShort = request.Host.ToString().StartsWith("www.") ?
                                   request.Host.ToString().Remove(0, 4) :
                                   request.Host.ToString();
            var permalinkShort = $"{request.Scheme}://{requestHostShort}{permalinkPart}";

            var hash = "";

            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i < blogPost.Tags.Count - 1)
                    {
                        sb.Append(",");
                    }
                }
                hash = sb.ToString();
            }

            return(new BlogPostVM
            {
                Title = blogPost.Title,
                Body = blogPost.Body,
                Excerpt = blogPost.Excerpt,
                Author = blogPost.User.DisplayName,
                CreatedOn = blogPost.CreatedOn,
                CreatedOnDisplay = blogPost.CreatedOn.ToDisplayString(coreSettings.TimeZoneId),
                Tags = blogPost.Tags.OrderBy(t => t.Title).ToList(),
                Category = blogPost.Category,
                ViewCount = blogPost.ViewCount,

                RelativeLink = relativeLink,
                Permalink = $"{request.Scheme}://{request.Host}{permalinkPart}",
                CanonicalUrl = $"{request.Scheme}://{request.Host}{relativeLink}",
                EditLink = BlogRoutes.GetPostEditLink(blogPost.Id),

                DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}",
                ShowDisqus = blogSettings.AllowComments && blogSettings.CommentProvider == ECommentProvider.Disqus && !blogSettings.DisqusShortname.IsNullOrEmpty(),
                DisqusShortname = blogSettings.DisqusShortname,

                TwitterShareLink = hash.IsNullOrEmpty() ?
                                   $"https://twitter.com/intent/tweet?text={blogPost.Title}&url={permalinkShort}" :
                                   $"https://twitter.com/intent/tweet?text={blogPost.Title}&url={permalinkShort}&hashtags={hash}",
                FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={permalinkShort}",
                LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={permalinkShort}&title={blogPost.Title}",
            });
        }
Esempio n. 3
0
        public BlogPostViewModel(BlogPost blogPost, BlogSettings blogSettings, HttpRequest request)
        {
            Title            = blogPost.Title;
            Body             = blogPost.Body;
            Excerpt          = blogPost.Excerpt;
            Author           = blogPost.User.DisplayName;
            CreatedOn        = blogPost.CreatedOn;
            CreatedOnDisplay = blogPost.CreatedOnDisplay;
            Tags             = blogPost.Tags.OrderBy(t => t.Title).ToList();
            Category         = blogPost.Category;

            RelativeLink = BlogRoutes.GetPostRelativeLink(CreatedOn, blogPost.Slug);
            var permalinkPart = BlogRoutes.GetPostPermalink(blogPost.Id);

            Permalink    = $"{request.Scheme}://{request.Host}{permalinkPart}";
            CanonicalUrl = $"{request.Scheme}://{request.Host}{RelativeLink}";
            EditLink     = BlogRoutes.GetPostEditLink(blogPost.Id);

            DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}";
            ShowDisqus           = blogSettings.AllowComments && blogSettings.CommentProvider == ECommentProvider.Disqus && !blogSettings.DisqusShortname.IsNullOrEmpty();
            DisqusShortname      = blogSettings.DisqusShortname;

            var hash = "";

            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i < blogPost.Tags.Count - 1)
                    {
                        sb.Append(",");
                    }
                }
                hash = sb.ToString();
            }

            var requestHostShort = request.Host.ToString().StartsWith("www.") ?
                                   request.Host.ToString().Remove(0, 4) : request.Host.ToString();
            var permalinkShort = $"{request.Scheme}://{requestHostShort}{permalinkPart}";

            TwitterShareLink = hash.IsNullOrEmpty() ?
                               $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}" :
                               $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}&hashtags={hash}";
            FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={permalinkShort}";
            GoogleShareLink   = $"https://plus.google.com/share?url={permalinkShort}";
            LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={permalinkShort}&title={Title}";
        }
Esempio n. 4
0
        public async Task <IViewComponentResult> InvokeAsync(Widget widget)
        {
            var recentBlogPostsWidget = (RecentBlogPostsWidget)widget;
            // ask for 1 more than number of posts to show
            var postList = await blogPostService.GetRecentPublishedPostsAsync(recentBlogPostsWidget.NumberOfPostsToShow + 1);

            // must have at least 2 posts
            if (postList.TotalPostCount < 2)
            {
                return(await Task.FromResult <IViewComponentResult>(Content(string.Empty)));
            }

            // get current url
            var relativeUrl = httpContextAccessor.HttpContext.Request.Path;
            var list        = new List <RecentPostViewModel>();

            foreach (var post in postList.Posts)
            {
                // if post url is current url then skip this post
                var postUrl = BlogRoutes.GetPostRelativeLink(post.CreatedOn, post.Slug);
                if (postUrl.Equals(relativeUrl, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                list.Add(new RecentPostViewModel
                {
                    Title   = post.Title,
                    Link    = BlogRoutes.GetPostPermalink(post.Id),
                    Author  = post.User.DisplayName,
                    Excerpt = recentBlogPostsWidget.ShowPostExcerpt ? post.Excerpt : null,
                    Date    = recentBlogPostsWidget.ShowPostDate ? post.CreatedOn.ToString("yyyy-MM-dd") : null,
                });

                if (list.Count >= recentBlogPostsWidget.NumberOfPostsToShow)
                {
                    break;
                }
            }

            return(View("~/Components/RecentBlogPosts.cshtml",
                        new Tuple <List <RecentPostViewModel>, RecentBlogPostsWidget>(list, recentBlogPostsWidget)));
        }