Browse Source

CMS-KIT: add link types to links in comments as optional

pull/15489/head
Salih 3 years ago
parent
commit
9510153940
  1. 2
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml
  2. 2
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/IMarkdownToHtmlRenderer.cs
  3. 17
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs
  4. 9
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs

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

@ -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", rels = new [] {"nofollow"}})
}
</abp-column>
</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
{
Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true);
Task<string> RenderAsync(string rawMarkdown, bool allowHtmlTags = true, bool preventXSS = true, string[] rels = null);
}

17
modules/cms-kit/src/Volo.CmsKit.Common.Web/Renderers/MarkdownToHtmlRenderer.cs

@ -20,7 +20,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[] rels = null)
{
if (!allowHtmlTags)
{
@ -33,10 +33,25 @@ public class MarkdownToHtmlRenderer : IMarkdownToHtmlRenderer, ITransientDepende
{
html = _htmlSanitizer.Sanitize(html);
}
if(rels != null && rels.Any())
{
html = AddRelToLinks(html, rels);
}
return Task.FromResult(html);
}
private string AddRelToLinks(string html, string[] rels)
{
var rel = string.Join(" ", rels);
var regex = new Regex("<a(.*?>)", RegexOptions.IgnoreCase |
RegexOptions.Singleline |
RegexOptions.Multiline |
RegexOptions.Compiled);
return regex.Replace(html, $"<a rel=\"{rel}\" $1");
}
private static List<CodeBlockIndexPair> GetCodeBlockIndices(string markdownText)
{

9
modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs

@ -59,7 +59,8 @@ public class CommentingViewComponent : AbpViewComponent
public virtual async Task<IViewComponentResult> InvokeAsync(
string entityType,
string entityId)
string entityId,
IEnumerable<string> rels = null)
{
var comments = (await CommentPublicAppService
.GetListAsync(entityType, entityId)).Items;
@ -70,6 +71,7 @@ public class CommentingViewComponent : AbpViewComponent
{
EntityId = entityId,
EntityType = entityType,
Rels = rels,
LoginUrl = loginUrl,
Comments = comments.OrderByDescending(i => i.CreationTime).ToList()
};
@ -98,11 +100,12 @@ public class CommentingViewComponent : AbpViewComponent
private async Task ConvertMarkdownTextsToHtml(CommentingViewModel viewModel)
{
viewModel.RawCommentTexts = new Dictionary<Guid, string>();
var rels = viewModel.Rels?.ToArray();
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, rels: rels);
foreach (var reply in comment.Replies)
{
@ -117,6 +120,8 @@ public class CommentingViewComponent : AbpViewComponent
public string EntityType { get; set; }
public string EntityId { get; set; }
public IEnumerable<string> Rels { get; set; }
public string LoginUrl { get; set; }

Loading…
Cancel
Save