diff --git a/docs/en/Modules/Cms-Kit/Comments.md b/docs/en/Modules/Cms-Kit/Comments.md index ac8853a4d8..ef476600eb 100644 --- a/docs/en/Modules/Cms-Kit/Comments.md +++ b/docs/en/Modules/Cms-Kit/Comments.md @@ -35,11 +35,12 @@ The comment system provides a commenting [widget](../../UI/AspNetCore/Widgets.md @await Component.InvokeAsync(typeof(CommentingViewComponent), new { entityType = "Product", - entityId = "..." + entityId = "...", + referralLinks = new [] {"nofollow"} }) ``` -`entityType` was explained in the previous section. `entityId` should be the unique id of the product, in this example. If you have a Product entity, you can use its Id here. +`entityType` was explained in the previous section. `entityId` should be the unique id of the product, in this example. If you have a Product entity, you can use its Id here. `referralLinks` is an optional parameter. You can use this parameter to add values (such as "nofollow", "noreferrer", or any other values) to the [rel attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel) of links. ## User Interface diff --git a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml index eee29c4cbb..1b3f3ff94c 100644 --- a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml +++ b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml @@ -60,7 +60,7 @@ @if (GlobalFeatureManager.Instance.IsEnabled()) { - @await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "1"}) + @await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "1", referralLinks = new [] {"nofollow"}}) } diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs index 565437af7e..7a3c91db6f 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs @@ -4,5 +4,5 @@ namespace Volo.CmsKit.Web.Renderers; public interface IMarkdownToHtmlRenderer { - Task RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true); + Task RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true, string referralLinks = null); } diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs index 83317c2111..9dd5555051 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -20,7 +21,7 @@ public class MarkdownToHtmlRenderer : IMarkdownToHtmlRenderer, ITransientDepende _htmlSanitizer = new HtmlSanitizer(); } - public Task RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true) + public Task RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true, string referralLinks = null) { if (!allowHtmlTags) { @@ -33,10 +34,24 @@ public class MarkdownToHtmlRenderer : IMarkdownToHtmlRenderer, ITransientDepende { html = _htmlSanitizer.Sanitize(html); } + + if(!referralLinks.IsNullOrWhiteSpace()) + { + html = SetReferralLinks(html, referralLinks); + } return Task.FromResult(html); } + private string SetReferralLinks(string html, string referralLinks) + { + var regex = new Regex(")", RegexOptions.IgnoreCase | + RegexOptions.Singleline | + RegexOptions.Multiline | + RegexOptions.Compiled); + return regex.Replace(html, $" GetCodeBlockIndices(string markdownText) { diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs index bdf8d3ffa0..304cfec687 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs @@ -59,8 +59,10 @@ public class CommentingViewComponent : AbpViewComponent public virtual async Task InvokeAsync( string entityType, - string entityId) + string entityId, + IEnumerable referralLinks = null) { + referralLinks ??= Enumerable.Empty(); var comments = (await CommentPublicAppService .GetListAsync(entityType, entityId)).Items; @@ -70,6 +72,7 @@ public class CommentingViewComponent : AbpViewComponent { EntityId = entityId, EntityType = entityType, + ReferralLinks = referralLinks, LoginUrl = loginUrl, Comments = comments.OrderByDescending(i => i.CreationTime).ToList() }; @@ -98,11 +101,12 @@ public class CommentingViewComponent : AbpViewComponent private async Task ConvertMarkdownTextsToHtml(CommentingViewModel viewModel) { viewModel.RawCommentTexts = new Dictionary(); + var referralLinks = viewModel.ReferralLinks?.JoinAsString(" "); foreach (var comment in viewModel.Comments) { viewModel.RawCommentTexts.Add(comment.Id, comment.Text); - comment.Text = await MarkdownToHtmlRenderer.RenderAsync(comment.Text, allowHtmlTags: false, preventXSS: true); + comment.Text = await MarkdownToHtmlRenderer.RenderAsync(comment.Text, allowHtmlTags: false, preventXSS: true, referralLinks: referralLinks); foreach (var reply in comment.Replies) { @@ -117,6 +121,8 @@ public class CommentingViewComponent : AbpViewComponent public string EntityType { get; set; } public string EntityId { get; set; } + + public IEnumerable ReferralLinks { get; set; } public string LoginUrl { get; set; }