22// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33// See the LICENSE file in the project root for more information
44
5+ using System . Diagnostics . CodeAnalysis ;
56using System . Text . RegularExpressions ;
7+ using Elastic . Markdown . Myst ;
68
79namespace Elastic . Markdown . Helpers ;
810
@@ -14,22 +16,60 @@ internal static partial class InterpolationRegex
1416
1517public static class Interpolation
1618{
17- public static bool ReplaceSubstitutions ( this ReadOnlySpan < char > span , IReadOnlyDictionary < string , string > ? properties , out string ? replacement )
19+ public static string ReplaceSubstitutions (
20+ this string input ,
21+ ParserContext context
22+ )
23+ {
24+ var span = input . AsSpan ( ) ;
25+ if ( span . ReplaceSubstitutions ( [ context . Substitutions , context . ContextSubstitutions ] , out var replacement ) )
26+ return replacement ;
27+ return input ;
28+ }
29+
30+
31+ public static bool ReplaceSubstitutions (
32+ this ReadOnlySpan < char > span ,
33+ ParserContext context ,
34+ [ NotNullWhen ( true ) ] out string ? replacement
35+ ) =>
36+ span . ReplaceSubstitutions ( [ context . Substitutions , context . ContextSubstitutions ] , out replacement ) ;
37+
38+ public static bool ReplaceSubstitutions (
39+ this ReadOnlySpan < char > span ,
40+ IReadOnlyDictionary < string , string > ? properties ,
41+ [ NotNullWhen ( true ) ] out string ? replacement
42+ )
1843 {
1944 replacement = null ;
45+ if ( properties is null || properties . Count == 0 )
46+ return false ;
47+
2048 if ( span . IndexOf ( "}}" ) < 0 )
2149 return false ;
2250
23- if ( properties is null || properties . Count == 0 )
51+ return span . ReplaceSubstitutions ( [ properties ] , out replacement ) ;
52+ }
53+
54+ public static bool ReplaceSubstitutions (
55+ this ReadOnlySpan < char > span ,
56+ IReadOnlyDictionary < string , string > [ ] properties ,
57+ [ NotNullWhen ( true ) ] out string ? replacement
58+ )
59+ {
60+ replacement = null ;
61+ if ( span . IndexOf ( "}}" ) < 0 )
2462 return false ;
2563
26- var substitutions = properties as Dictionary < string , string >
27- ?? new Dictionary < string , string > ( properties , StringComparer . OrdinalIgnoreCase ) ;
28- if ( substitutions . Count == 0 )
64+ if ( properties . Length == 0 || properties . Sum ( p => p . Count ) == 0 )
2965 return false ;
3066
67+ var lookups = properties
68+ . Select ( p => p as Dictionary < string , string > ?? new Dictionary < string , string > ( p , StringComparer . OrdinalIgnoreCase ) )
69+ . Select ( d => d . GetAlternateLookup < ReadOnlySpan < char > > ( ) )
70+ . ToArray ( ) ;
71+
3172 var matchSubs = InterpolationRegex . MatchSubstitutions ( ) . EnumerateMatches ( span ) ;
32- var lookup = substitutions . GetAlternateLookup < ReadOnlySpan < char > > ( ) ;
3373
3474 var replaced = false ;
3575 foreach ( var match in matchSubs )
@@ -39,14 +79,15 @@ public static bool ReplaceSubstitutions(this ReadOnlySpan<char> span, IReadOnlyD
3979
4080 var spanMatch = span . Slice ( match . Index , match . Length ) ;
4181 var key = spanMatch . Trim ( [ '{' , '}' ] ) ;
82+ foreach ( var lookup in lookups )
83+ {
84+ if ( ! lookup . TryGetValue ( key , out var value ) )
85+ continue ;
4286
43- if ( ! lookup . TryGetValue ( key , out var value ) )
44- continue ;
45-
46- replacement ??= span . ToString ( ) ;
47- replacement = replacement . Replace ( spanMatch . ToString ( ) , value ) ;
48- replaced = true ;
49-
87+ replacement ??= span . ToString ( ) ;
88+ replacement = replacement . Replace ( spanMatch . ToString ( ) , value ) ;
89+ replaced = true ;
90+ }
5091 }
5192
5293 return replaced ;
0 commit comments