-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interpolation support and unify substitution logic (#201)
Introduced interpolation functionality for handling string replacements in Markdown. Refactored and centralized substitution logic into a reusable helper, replacing redundant implementations. Updated navigation title and code block parsing to leverage the new interpolation system, ensuring consistency and cleaner code.
- Loading branch information
Showing
8 changed files
with
122 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace Elastic.Markdown.Helpers; | ||
|
||
internal static partial class InterpolationRegex | ||
{ | ||
[GeneratedRegex(@"\{\{[^\r\n}]+?\}\}", RegexOptions.IgnoreCase, "en-US")] | ||
public static partial Regex MatchSubstitutions(); | ||
} | ||
|
||
public static class Interpolation | ||
{ | ||
public static bool ReplaceSubstitutions(this ReadOnlySpan<char> span, Dictionary<string, string>? properties, out string? replacement) | ||
{ | ||
replacement = null; | ||
var substitutions = properties ?? new(); | ||
if (substitutions.Count == 0) | ||
return false; | ||
|
||
var matchSubs = InterpolationRegex.MatchSubstitutions().EnumerateMatches(span); | ||
var lookup = substitutions.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
|
||
var replaced = false; | ||
foreach (var match in matchSubs) | ||
{ | ||
if (match.Length == 0) | ||
continue; | ||
|
||
var spanMatch = span.Slice(match.Index, match.Length); | ||
var key = spanMatch.Trim(['{', '}']); | ||
|
||
if (!lookup.TryGetValue(key, out var value)) | ||
continue; | ||
|
||
replacement ??= span.ToString(); | ||
replacement = replacement.Replace(spanMatch.ToString(), value); | ||
replaced = true; | ||
|
||
} | ||
|
||
return replaced; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/Elastic.Markdown.Tests/Interpolation/InterpolationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Helpers; | ||
using FluentAssertions; | ||
|
||
namespace Elastic.Markdown.Tests.Interpolation; | ||
|
||
public class InterpolationTests | ||
{ | ||
[Fact] | ||
public void ReplacesVariables() | ||
{ | ||
var span = "My text {{with-variables}} {{not-defined}}".AsSpan(); | ||
var replacements = new Dictionary<string, string> { { "with-variables", "With Variables" } }; | ||
var replaced = span.ReplaceSubstitutions(replacements, out var replacement); | ||
|
||
replaced.Should().BeTrue(); | ||
replacement.Should().Be("My text With Variables {{not-defined}}"); | ||
} | ||
|
||
[Fact] | ||
public void OnlyReplacesDefinedVariables() | ||
{ | ||
var span = "My text {{not-defined}}".AsSpan(); | ||
var replacements = new Dictionary<string, string> { { "with-variables", "With Variables" } }; | ||
var replaced = span.ReplaceSubstitutions(replacements, out var replacement); | ||
|
||
replaced.Should().BeFalse(); | ||
// no need to allocate replacement we can continue with span | ||
replacement.Should().BeNull(); | ||
} | ||
} |