("Type"), new
+ {
+ extraProperties = contentFragment.ExtraProperties
+ }
+ )
}
}
-
@if (Model.BlogPost.LastModificationTime != null)
{
From a07eaf4c4536b23e37a3692990d27acbec80e1bd Mon Sep 17 00:00:00 2001
From: malik masis
Date: Tue, 14 Jun 2022 14:45:42 +0300
Subject: [PATCH 26/60] Used dynamic values instead of hard-coded in
ContentParser
---
.../Volo/CmsKit/Contents/ContentParser.cs | 31 +++++++------------
.../Shared/Components/Pages/Default.cshtml | 14 ++++-----
2 files changed, 19 insertions(+), 26 deletions(-)
diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
index c4da64a986..3b34c66346 100644
--- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
+++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
@@ -21,18 +21,11 @@ public class ContentParser : ITransientDependency
public async Task> ParseAsync(string content)
{
- return new List {
- new ContentFragment() { Type = "Markdown" }.SetProperty("Content", "This is *a markdown* text."),
- new ContentFragment() { Type = "Widget" }.SetProperty("Type", "Poll").SetProperty("Code", "6dhah8dd"),
- new ContentFragment() { Type = "Markdown" }.SetProperty("Content", "This is *another markdown* text.")
- };
-
- /*
if (!_options.WidgetConfigs.Any())
{
- return new List()
+ return new List
{
- new MarkdownContentFragment() { Content = content }
+ new ContentFragment() { Type = "Markdown" }.SetProperty("Content", content),
};
}
@@ -87,31 +80,31 @@ public class ContentParser : ITransientDependency
if (parsedWidgets.Count > k)
{
- var name = _options.WidgetConfigs.Where(p => p.Key == values[0]).Select(p => p.Value.Name).FirstOrDefault();
+ var widgetType = values[0];
+ var name = _options.WidgetConfigs.Where(p => p.Key == widgetType).Select(p => p.Value.Name).FirstOrDefault();
if (name is not null && parsedWidgets.Count > k)
{
- var properties = new Dictionary();
+ values[0] = name;
+ var contentFragment = new ContentFragment() { Type = "Widget" };
+ contentFragments.Add(contentFragment);
for (int kv = 0; kv < values.Count; kv++)
{
- properties.Add(keys[kv], values[kv]);
+
+ contentFragments.FindLast(p=>p == contentFragment)
+ .SetProperty(keys[kv], values[kv]);
}
-
- contentFragments.Add(new WidgetContentFragment(name)
- {
- Properties = properties
- });
}
}
k++;
}
else
{
- contentFragments.Add(new MarkdownContentFragment() { Content = parsedList[i] });
+ contentFragments.Add(new ContentFragment() { Type = "Markdown" }
+ .SetProperty("Content", parsedList[i]));
}
}
return contentFragments;
- */
}
private void ParseWidgets(string content, Dictionary>> parsedWidgets)
diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Pages/Default.cshtml b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Pages/Default.cshtml
index edb6f1dbec..396628ae66 100644
--- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Pages/Default.cshtml
+++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Pages/Default.cshtml
@@ -17,13 +17,13 @@
@Html.Raw(await MarkdownRenderer.RenderAsync(contentFragment.GetProperty("Content")))
}
else if (contentFragment.Type == "Widget") //TODO: Constant
- {
- @await Component.InvokeAsync(contentFragment.GetProperty("Type"), new
- {
- extraProperties = contentFragment.ExtraProperties
- }
- )
- }
+ {
+ @await Component.InvokeAsync(contentFragment.GetProperty("Type"), new
+ {
+ extraProperties = contentFragment.ExtraProperties
+ }
+ )
}
+ }
From 09b59e220ae64f315350f45c6a94f8a2b92d158b Mon Sep 17 00:00:00 2001
From: malik masis
Date: Tue, 14 Jun 2022 16:47:46 +0300
Subject: [PATCH 27/60] Refactored ContentParser
---
.../Volo/CmsKit/Contents/ContentParser.cs | 86 +++++++++----------
1 file changed, 39 insertions(+), 47 deletions(-)
diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
index 3b34c66346..9badeab8d3 100644
--- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
+++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Contents/ContentParser.cs
@@ -19,20 +19,28 @@ public class ContentParser : ITransientDependency
_options = options.Value;
}
- public async Task> ParseAsync(string content)
+ public Task> ParseAsync(string content)
{
if (!_options.WidgetConfigs.Any())
{
- return new List
+ return Task.FromResult(new List
{
new ContentFragment() { Type = "Markdown" }.SetProperty("Content", content),
- };
+ });
}
- var replacedText = Regex.Replace(content, @"\[.*?\]", delimeter);
-
var parsedList = new List();
+ ParseContent(content, parsedList);
+
+ var contentFragments = new List();
+ FillContentFragment(content, parsedList, contentFragments);
+ return Task.FromResult(contentFragments);
+ }
+
+ private void ParseContent(string content, List parsedList)
+ {
+ var replacedText = Regex.Replace(content, @"\[.*?\]", delimeter);
if (!replacedText.Contains(delimeter))
{
parsedList.Add(replacedText);
@@ -64,34 +72,40 @@ public class ContentParser : ITransientDependency
parsedList.Add(replacedText);
}
}
+ }
-
- Dictionary>> parsedWidgets = new();
- ParseWidgets(content, parsedWidgets);
-
- var contentFragments = new List();
-
+ private void FillContentFragment(string content, List parsedList, List contentFragments)
+ {
+ content = Regex.Replace(content, @"=\s*""", @"=""");
+ content = Regex.Replace(content, @"""\s*=", @"""=");
+ var widgets = Regex.Matches(content, @"(?<=\[Widget)(.*?)(?=\])").Cast().Select(p => p.Value).ToList();
for (int i = 0, k = 0; i < parsedList.Count; i++)
{
if (parsedList[i] == delimeter)
{
- var values = parsedWidgets.GetOrDefault($"{k}.Widget").Select(p => p.Value).ToList();
- var keys = parsedWidgets.GetOrDefault($"{k}.Widget").Select(p => p.Key).ToList();
-
- if (parsedWidgets.Count > k)
+ if (widgets.Count > k)
{
- var widgetType = values[0];
- var name = _options.WidgetConfigs.Where(p => p.Key == widgetType).Select(p => p.Value.Name).FirstOrDefault();
- if (name is not null && parsedWidgets.Count > k)
+ var preparedContent = string.Join("", widgets[k]);
+ var keys = Regex.Matches(preparedContent, @"(?<=\s)(.*?)(?==\s*"")").Cast()
+ .Select(p => p.Value).Where(p => p != string.Empty).ToList();
+ var values = Regex.Matches(preparedContent, @"(?<=\s*[a-zA-Z]*=\s*"")(.*?)(?="")").Cast()
+ .Select(p => p.Value).ToList();
+
+ var widgetTypeIndex = keys.IndexOf("Type");
+ if (widgetTypeIndex != -1)
{
- values[0] = name;
- var contentFragment = new ContentFragment() { Type = "Widget" };
- contentFragments.Add(contentFragment);
- for (int kv = 0; kv < values.Count; kv++)
+ var widgetType = values[widgetTypeIndex];
+ var name = _options.WidgetConfigs.Where(p => p.Key == widgetType).Select(p => p.Value.Name).FirstOrDefault();
+ if (name is not null && widgets.Count > k)
{
-
- contentFragments.FindLast(p=>p == contentFragment)
- .SetProperty(keys[kv], values[kv]);
+ values[0] = name;
+ var contentFragment = new ContentFragment() { Type = "Widget" };
+ contentFragments.Add(contentFragment);
+ for (int kv = 0; kv < values.Count; kv++)
+ {
+ contentFragments.FindLast(p => p == contentFragment)
+ .SetProperty(keys[kv], values[kv]);
+ }
}
}
}
@@ -103,28 +117,6 @@ public class ContentParser : ITransientDependency
.SetProperty("Content", parsedList[i]));
}
}
-
- return contentFragments;
}
- private void ParseWidgets(string content, Dictionary>> parsedWidgets)
- {
- var widgets = Regex.Matches(content, @"(?<=\[Widget)(.*?)(?=\])").Cast().Select(p => p.Value).ToList();
- for (int p = 0; p < widgets.Count; p++)
- {
- var preparedContent = string.Join("", widgets[p]);
- var keys = Regex.Matches(preparedContent, @"(?<=[\[Widget]?\s)(.*?)(?=="")").Cast()
- .Select(p => p.Value).Where(p => p != string.Empty).ToList();
- var values = Regex.Matches(preparedContent, @"(?<=\s*[a-zA-Z]*=\s*"")(.*?)(?="")").Cast()
- .Select(p => p.Value).ToList();
-
- var list = new List>();
- for (int kv = 0; kv < keys.Count; kv++)
- {
- list.Add(new KeyValuePair(keys[kv], values[kv]));
- }
-
- parsedWidgets.Add($"{p}.Widget", list);
- }
- }
}
\ No newline at end of file
From 05ef73658f1bb6cccb9152ccae797d951a6f5069 Mon Sep 17 00:00:00 2001
From: malik masis
Date: Tue, 14 Jun 2022 16:48:34 +0300
Subject: [PATCH 28/60] Run all cases of the unit tests successfully
---
.../Contents/ContentParser_Test.cs | 29 ++++++++++---------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentParser_Test.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentParser_Test.cs
index 00dc4c184e..4a51206a15 100644
--- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentParser_Test.cs
+++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentParser_Test.cs
@@ -19,30 +19,33 @@ public class ContentParser_Test : CmsKitDomainTestBase
}
[Fact]
- public async Task AA_ParseAsync_ShouldWorkMoreDynamically()
+ public async Task ParseAsync_ShouldWorkWithDifferentWidgetTypes()
{
_options.Value.AddWidgetConfig(testData.PollName, new ContentWidgetConfig(testData.WidgetName));
_options.Value.AddWidgetConfig("ImageGallery", new ContentWidgetConfig("ImageGallery"));//test
contentParser = new ContentParser(_options);
var content = @"**ABP Framework** is completely open source and developed in a community-driven manner.
- [Widget Type=""Poll"" PollName =""poll-name""]
+ [Widget Type=""Poll"" Code=""poll-name""]
Thanks _for_ *your * feedback.
- [Widget Type=""ImageGallery"" GalleryName=""Xyz"" Source=""GoogleDrive""]";
+ [Widget GalleryName=""Xyz"" Type=""ImageGallery"" Source=""GoogleDrive""]";
var widgets = await contentParser.ParseAsync(content);
widgets.ShouldNotBeNull();
widgets.Count.ShouldBe(4);
+ widgets[1].ExtraProperties.Count.ShouldBe(2);
+ widgets[3].ExtraProperties.Count.ShouldBe(3);
}
[Fact]
public async Task ParseAsync_ShouldWorkWithoutConfigOptions()
{
var content = @"**ABP Framework** is completely open source and developed in a community-driven manner.
- [Widget Type= ""Poll"" PollName =""poll-name""]
+ [Widget Type= ""Poll"" Code =""poll-name""]
Thanks _for_ *your * feedback.";
+ contentParser = new ContentParser(_options);
var widgets = await contentParser.ParseAsync(content);
widgets.ShouldNotBeNull();
@@ -56,7 +59,7 @@ public class ContentParser_Test : CmsKitDomainTestBase
contentParser = new ContentParser(_options);
var content = @"**ABP Framework** is completely open source and developed in a community-driven manner.
- [Widget Type= ""Poll"" PollName =""poll-name""]
+ [Widget Type= ""Poll"" Code =""poll-name""]
Thanks _for_ *your * feedback.";
var widgets = await contentParser.ParseAsync(content);
@@ -72,7 +75,7 @@ public class ContentParser_Test : CmsKitDomainTestBase
contentParser = new ContentParser(_options);
var content = @"**ABP Framework** is completely open source and developed in a community-driven manner.
- [Widget Wrong Type= ""Poll"" PollName =""poll-name""]
+ [Widget Wrong Type= ""Poll"" Code =""poll-name""]
Thanks _for_ *your * feedback.";
var widgets = await contentParser.ParseAsync(content);
@@ -88,13 +91,13 @@ public class ContentParser_Test : CmsKitDomainTestBase
contentParser = new ContentParser(_options);
var content = @"**ABP Framework** is completely open source and developed in a community-driven manner.
- [Widget Type= ""Poll"" PollWrongName =""poll-name""]
+ [Widget Type= ""Poll"" PollWrongName =""poll-name""]
Thanks _for_ *your * feedback.";
var widgets = await contentParser.ParseAsync(content);
widgets.ShouldNotBeNull();
- widgets.Count.ShouldBe(2);
+ widgets.Count.ShouldBe(3);
}
[Theory]
@@ -114,19 +117,19 @@ public class ContentParser_Test : CmsKitDomainTestBase
new List