Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #13

Merged
merged 6 commits into from
Jul 22, 2024
Merged

Next #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/sca-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ jobs:
security-sca:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Checkout repository
uses: actions/checkout@master
- uses: snyk/actions/setup@master
- name: Setup .NET
uses: actions/[email protected]
- name: Restore dependencies
run: dotnet restore ./Contentstack.Utils.sln
- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/dotnet@master
run: cd Contentstack.Utils && snyk test --fail-on=all
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --fail-on=all
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
fileignoreconfig:
- filename: Contentstack.Utils/Models/Options.cs
checksum: 3dc51f0de02429ef9a43b66e666ac4dbde41195e245f8ecc0094548ca8603245
- filename: Contentstack.Utils/Utils.cs
checksum: dfcfa04bedf8e5f9e74f68d02a9f8cb4e35de399e272da015453c6bc4c481e3f
version: ""
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

### Version: 1.0.3
#### Date: Jul-10-2024
- Editable tags added

### Version: 1.0.2
#### Date: Mar-14-2024
- Style attributes supported in converted HTML.
Expand Down
8 changes: 8 additions & 0 deletions Contentstack.Utils/Interfaces/IEmbeddedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ string Title
}
}

public interface EditableEntry: IEmbeddedEntry
{

string Locale { get; set; }

object this[string key] { get; set; }
}

public interface IEmbeddedAsset: IEmbeddedObject
{
string Title
Expand Down
89 changes: 89 additions & 0 deletions Contentstack.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,94 @@ private static IEmbeddedObject findEmbedded(Metadata metadata, List<IEmbeddedObj
}
return null;
}

public static void addEditableTags(EditableEntry entry, string contentTypeUid, bool tagsAsObject, string locale = "en-us")
{
if (entry != null)
entry["$"] = GetTag(entry, $"{contentTypeUid}.{entry.Uid}.{locale}", tagsAsObject, locale);
}

private static Dictionary<string, object> GetTag(object content, string prefix, bool tagsAsObject, string locale)
{
var tags = new Dictionary<string, object>();
foreach (var property in (Dictionary<string, object>)content)
{
var key = property.Key;
var value = property.Value;

if (key == "$")
continue;

switch (value)
{
case object obj when obj is object[] array:
for (int index = 0; index < array.Length; index++)
{
object objValue = array[index];
string childKey = $"{key}__{index}";
string parentKey = $"{key}__parent";

tags[childKey] = GetTagsValue($"{prefix}.{key}.{index}", tagsAsObject);
tags[parentKey] = GetParentTagsValue($"{prefix}.{key}", tagsAsObject);

if (objValue != null &&
objValue.GetType().GetProperty("_content_type_uid") != null &&
objValue.GetType().GetProperty("Uid") != null)
{
var typedObj = (EditableEntry)objValue;
string locale_ = Convert.ToString(typedObj.GetType().GetProperty("locale").GetValue(typedObj));
string ctUid = Convert.ToString(typedObj.GetType().GetProperty("_content_type_uid").GetValue(typedObj));
string uid = Convert.ToString(typedObj.GetType().GetProperty("uid").GetValue(typedObj));
string localeStr = "";
if (locale_ != null)
{
localeStr = locale_;
} else
{
localeStr = locale;
}
typedObj["$"] = GetTag(typedObj, $"{ctUid}.{uid}.{localeStr}", tagsAsObject, locale);
}
else if (value is object)
{
((EditableEntry)value)["$"] = GetTag(value, $"{prefix}.{key}.{index}", tagsAsObject, locale);
}
}
tags[key] = GetTagsValue($"{prefix}.{key}", tagsAsObject);
break;
case object obj when obj != null:
if (value != null)
{
((EditableEntry)value)["$"] = GetTag(value, $"{prefix}.{key}", tagsAsObject, locale);
}
break;
}
}
return tags;
}

private static object GetTagsValue(string dataValue, bool tagsAsObject)
{
if (tagsAsObject)
{
return new Dictionary<string, object> { { "data-cslp", dataValue } };
}
else
{
return $"data-cslp={dataValue}";
}
}

private static object GetParentTagsValue(string dataValue, bool tagsAsObject)
{
if (tagsAsObject)
{
return new Dictionary<string, object> { { "data-cslp-parent-field", dataValue } };
}
else
{
return $"data-cslp-parent-field={dataValue}";
}
}
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
</PropertyGroup>
</Project>
Loading