8
8
using Kontent . Ai . Delivery . ContentItems . ContentLinks ;
9
9
using Kontent . Ai . Delivery . ContentItems . RichText ;
10
10
using Kontent . Ai . Delivery . ContentItems . RichText . Blocks ;
11
+ using Microsoft . Extensions . Options ;
11
12
12
13
namespace Kontent . Ai . Delivery . ContentItems
13
14
{
14
15
internal class RichTextContentConverter : IPropertyValueConverter < string >
15
16
{
16
17
public IHtmlParser Parser { get ; }
18
+ public IOptionsMonitor < DeliveryOptions > Options { get ; }
17
19
18
- public RichTextContentConverter ( IHtmlParser parser )
20
+ public RichTextContentConverter ( IHtmlParser parser , IOptionsMonitor < DeliveryOptions > options )
19
21
{
20
22
Parser = parser ;
23
+ Options = options ;
21
24
}
22
25
23
26
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,
56
59
if ( img != null )
57
60
{
58
61
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 ] ) ;
60
77
}
61
78
}
62
79
else
@@ -67,5 +84,21 @@ public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property,
67
84
68
85
return blocks ;
69
86
}
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
+ }
70
103
}
71
104
}
0 commit comments