Merge pull request #15489 from abpframework/issue-15458

CMS-KIT: add link types to links in comments as optional
pull/15506/head
Berkan Sasmaz 3 years ago committed by GitHub
commit a8d9f39797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -60,7 +60,7 @@
<abp-column size-md="_12">
@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-row>

@ -4,5 +4,5 @@ namespace Volo.CmsKit.Web.Renderers;
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);
}

@ -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<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)
{
@ -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("<a(.*?>)", RegexOptions.IgnoreCase |
RegexOptions.Singleline |
RegexOptions.Multiline |
RegexOptions.Compiled);
return regex.Replace(html, $"<a rel=\"{referralLinks}\" $1");
}
private static List<CodeBlockIndexPair> GetCodeBlockIndices(string markdownText)
{

@ -59,8 +59,10 @@ public class CommentingViewComponent : AbpViewComponent
public virtual async Task<IViewComponentResult> InvokeAsync(
string entityType,
string entityId)
string entityId,
IEnumerable<string> referralLinks = null)
{
referralLinks ??= Enumerable.Empty<string>();
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<Guid, string>();
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<string> ReferralLinks { get; set; }
public string LoginUrl { get; set; }

Loading…
Cancel
Save