From 1f6375eb2fabe0612ccbb549b65963575734063d Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 20 Feb 2020 09:29:47 +0300 Subject: [PATCH] Blogging module: Fix ShortContent generation resolves https://github.com/abpframework/abp/issues/2865 --- .../Pages/Blogs/BloggingPage.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/BloggingPage.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/BloggingPage.cs index ed3bb0971c..0808432521 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/BloggingPage.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/BloggingPage.cs @@ -1,5 +1,7 @@ using System; +using System.Linq; using System.Text; +using System.Text.RegularExpressions; using CommonMark; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Localization; @@ -30,30 +32,27 @@ namespace Volo.Blogging.Pages.Blog public string GetShortContent(string content) //TODO: This should be moved to its own place! { - var openingTag = "

"; - var closingTag = "

"; - var html = RenderMarkdownToString(content); - if (string.IsNullOrWhiteSpace(html)) + var plainText = Regex.Replace(html, "/<[^>]*>/g", ""); + + if (string.IsNullOrWhiteSpace(plainText)) { return ""; } - var splittedHtml = html.Split(closingTag); - if (splittedHtml.Length < 1) + var firsParag = plainText.Split(Environment.NewLine).FirstOrDefault(s => !string.IsNullOrWhiteSpace(s)); + + if (firsParag == null) { - return ""; + return plainText; } - var firstHtmlPart = splittedHtml[0]; - var paragraphStartIndex = firstHtmlPart.IndexOf(openingTag, StringComparison.Ordinal) + openingTag.Length; - - if (firstHtmlPart.Length - paragraphStartIndex <= MaxShortContentLength) + if (firsParag.Length <= MaxShortContentLength) { - return firstHtmlPart.Substring(paragraphStartIndex); + return firsParag; } - return firstHtmlPart.Substring(paragraphStartIndex, MaxShortContentLength) + "..."; + return firsParag.Substring(0, MaxShortContentLength) + "..."; } public IHtmlContent RenderMarkdownToHtml(string content)