Skip to content

Commit 7da097b

Browse files
xantaripokornyd
authored andcommitted
update for rich text content converter
1 parent 34f3683 commit 7da097b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Kontent.Ai.Delivery/ContentItems/ModelProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private IPropertyValueConverter GetValueConverter(PropertyInfo property)
377377
// Specific type converters
378378
if (typeof(IRichTextContent).IsAssignableFrom(property.PropertyType))
379379
{
380-
return new RichTextContentConverter(HtmlParser);
380+
return new RichTextContentConverter(HtmlParser, DeliveryOptions);
381381
}
382382

383383
if (typeof(IDateTimeContent).IsAssignableFrom(property.PropertyType))
@@ -389,7 +389,7 @@ private IPropertyValueConverter GetValueConverter(PropertyInfo property)
389389
{
390390
return new AssetElementValueConverter(DeliveryOptions);
391391
}
392-
392+
393393
return null;
394394
}
395395

Kontent.Ai.Delivery/ContentItems/RichTextContentConverter.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
using Kontent.Ai.Delivery.ContentItems.ContentLinks;
99
using Kontent.Ai.Delivery.ContentItems.RichText;
1010
using Kontent.Ai.Delivery.ContentItems.RichText.Blocks;
11+
using Microsoft.Extensions.Options;
1112

1213
namespace Kontent.Ai.Delivery.ContentItems
1314
{
1415
internal class RichTextContentConverter : IPropertyValueConverter<string>
1516
{
1617
public IHtmlParser Parser { get; }
18+
public IOptionsMonitor<DeliveryOptions> Options { get; }
1719

18-
public RichTextContentConverter(IHtmlParser parser)
20+
public RichTextContentConverter(IHtmlParser parser, IOptionsMonitor<DeliveryOptions> options)
1921
{
2022
Parser = parser;
23+
Options = options;
2124
}
2225

2326
public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property, TElement contentElement, ResolvingContext context) where TElement : IContentElementValue<string>
@@ -56,7 +59,21 @@ public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property,
5659
if (img != null)
5760
{
5861
var assetId = Guid.Parse(img.GetAttribute("data-asset-id"));
59-
blocks.Add(element.Images[assetId]);
62+
if (!string.IsNullOrEmpty(Options.CurrentValue.AssetUrlReplacement))
63+
{
64+
var assetToReplace = element.Images[assetId];
65+
var replacedAsset = new InlineImage()
66+
{
67+
Url = ReplaceAssetUrlWIthCustomAssetUrl(assetToReplace.Url),
68+
Description = assetToReplace.Description,
69+
Height = assetToReplace.Height,
70+
Width = assetToReplace.Width,
71+
ImageId = assetToReplace.ImageId
72+
};
73+
blocks.Add(replacedAsset);
74+
}
75+
else
76+
blocks.Add(element.Images[assetId]);
6077
}
6178
}
6279
else
@@ -67,5 +84,21 @@ public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property,
6784

6885
return blocks;
6986
}
87+
88+
/// <summary>
89+
/// Replace the beginning part of the asset URL with the AssetUrlReplacement value.
90+
/// </summary>
91+
/// <param name="url">Original Asset Url</param>
92+
/// <returns>New URL with the CDN URL replaces with AssetUrlReplacement</returns>
93+
private string ReplaceAssetUrlWIthCustomAssetUrl(string url)
94+
{
95+
// Replace the beginning part of the asset URL with the AssetUrlReplacement value by taking the third forward slash as the ending point for the string replacement
96+
var endOfUrlIndex = url.IndexOf("/", url.IndexOf("/", url.IndexOf("/", 0) + 1) + 1);
97+
if (endOfUrlIndex > 0)
98+
{
99+
return Options.CurrentValue.AssetUrlReplacement + url.Substring(endOfUrlIndex);
100+
}
101+
return url;
102+
}
70103
}
71104
}

0 commit comments

Comments
 (0)