From d6a0caf16fff462d8bc4678c42eb299dae88e272 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Tue, 7 Apr 2020 11:45:50 +0300 Subject: [PATCH] Refactor getting description field --- .../Pages/Documents/Project/Index.cshtml.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index 44bdb3e632..b3eceffdef 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -69,6 +69,7 @@ namespace Volo.Docs.Pages.Documents.Project public bool FullSearchEnabled { get; set; } + private const int MaxDescriptionMetaTagLength = 200; private readonly IDocumentAppService _documentAppService; private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory; private readonly IProjectAppService _projectAppService; @@ -596,14 +597,19 @@ namespace Volo.Docs.Pages.Documents.Project public string GetDescription() { - var startIndex = Document.Content.IndexOf("

", StringComparison.Ordinal); - var lastIndex = Document.Content.IndexOf("

", StringComparison.Ordinal); - var description = Document.Content.Substring(startIndex,lastIndex); + var firstParagraph = new Regex(@"

(.*?)

"); + var match = firstParagraph.Match(Document.Content); + if (!match.Success) + { + return null; + } + + var description = HttpUtility.HtmlDecode(match.Value); - Regex rx = new Regex("<[^>]*>"); - description = rx.Replace(description, ""); + var htmlTagReplacer = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase); + description = htmlTagReplacer.Replace(description, m => string.Empty); - return description.Truncate(200); + return description.Truncate(MaxDescriptionMetaTagLength); } } }