Browse Source

Merge pull request #15489 from abpframework/issue-15458

CMS-KIT: add link types to links in comments as optional
pull/15506/head
Berkan Sasmaz 4 years ago
committed by GitHub
parent
commit
a8d9f39797
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      docs/en/Modules/Cms-Kit/Comments.md
  2. 2
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml
  3. 2
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs
  4. 19
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs
  5. 10
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs

5
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 @await Component.InvokeAsync(typeof(CommentingViewComponent), new
{ {
entityType = "Product", 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 ## User Interface

2
modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml

@ -60,7 +60,7 @@
<abp-column size-md="_12"> <abp-column size-md="_12">
@if (GlobalFeatureManager.Instance.IsEnabled<CommentsFeature>()) @if (GlobalFeatureManager.Instance.IsEnabled<CommentsFeature>())
{ {
@await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "1"}) @await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "1", referralLinks = new [] {"nofollow"}})
} }
</abp-column> </abp-column>
</abp-row> </abp-row>

2
modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs

@ -4,5 +4,5 @@ namespace Volo.CmsKit.Web.Renderers;
public interface IMarkdownToHtmlRenderer public interface IMarkdownToHtmlRenderer
{ {
Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true); Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true, string referralLinks = null);
} }

19
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.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,7 +21,7 @@ public class MarkdownToHtmlRenderer : IMarkdownToHtmlRenderer, ITransientDepende
_htmlSanitizer = new HtmlSanitizer(); _htmlSanitizer = new HtmlSanitizer();
} }
public Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true) public Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true, string referralLinks = null)
{ {
if (!allowHtmlTags) if (!allowHtmlTags)
{ {
@ -33,10 +34,24 @@ public class MarkdownToHtmlRenderer : IMarkdownToHtmlRenderer, ITransientDepende
{ {
html = _htmlSanitizer.Sanitize(html); html = _htmlSanitizer.Sanitize(html);
} }
if(!referralLinks.IsNullOrWhiteSpace())
{
html = SetReferralLinks(html, referralLinks);
}
return Task.FromResult(html); return Task.FromResult(html);
} }
private string SetReferralLinks(string html, string referralLinks)
{
var regex = new Regex("<a(.*?>)", RegexOptions.IgnoreCase |
RegexOptions.Singleline |
RegexOptions.Multiline |
RegexOptions.Compiled);
return regex.Replace(html, $"<a rel=\"{referralLinks}\" $1");
}
private static List<CodeBlockIndexPair> GetCodeBlockIndices(string markdownText) private static List<CodeBlockIndexPair> GetCodeBlockIndices(string markdownText)
{ {

10
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<IViewComponentResult> InvokeAsync( public virtual async Task<IViewComponentResult> InvokeAsync(
string entityType, string entityType,
string entityId) string entityId,
IEnumerable<string> referralLinks = null)
{ {
referralLinks ??= Enumerable.Empty<string>();
var comments = (await CommentPublicAppService var comments = (await CommentPublicAppService
.GetListAsync(entityType, entityId)).Items; .GetListAsync(entityType, entityId)).Items;
@ -70,6 +72,7 @@ public class CommentingViewComponent : AbpViewComponent
{ {
EntityId = entityId, EntityId = entityId,
EntityType = entityType, EntityType = entityType,
ReferralLinks = referralLinks,
LoginUrl = loginUrl, LoginUrl = loginUrl,
Comments = comments.OrderByDescending(i => i.CreationTime).ToList() Comments = comments.OrderByDescending(i => i.CreationTime).ToList()
}; };
@ -98,11 +101,12 @@ public class CommentingViewComponent : AbpViewComponent
private async Task ConvertMarkdownTextsToHtml(CommentingViewModel viewModel) private async Task ConvertMarkdownTextsToHtml(CommentingViewModel viewModel)
{ {
viewModel.RawCommentTexts = new Dictionary<Guid, string>(); viewModel.RawCommentTexts = new Dictionary<Guid, string>();
var referralLinks = viewModel.ReferralLinks?.JoinAsString(" ");
foreach (var comment in viewModel.Comments) foreach (var comment in viewModel.Comments)
{ {
viewModel.RawCommentTexts.Add(comment.Id, comment.Text); 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) foreach (var reply in comment.Replies)
{ {
@ -117,6 +121,8 @@ public class CommentingViewComponent : AbpViewComponent
public string EntityType { get; set; } public string EntityType { get; set; }
public string EntityId { get; set; } public string EntityId { get; set; }
public IEnumerable<string> ReferralLinks { get; set; }
public string LoginUrl { get; set; } public string LoginUrl { get; set; }

Loading…
Cancel
Save