-
Notifications
You must be signed in to change notification settings - Fork 32
Fix mutation operators not working in links and code blocks #1606
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b338d3e
Add tests and fix subs
theletterf cce710c
Remove duplication
theletterf ee27f2a
Refactor
theletterf d9c344e
Update docs
theletterf 7897b6b
Remove dupe logic
theletterf b647f62
Fix rendering
theletterf c8d27f8
Peer edits
theletterf 3de5f7c
Fix capitalization issue
theletterf 75e9406
Subs in docs
theletterf 68b4edc
Fix glitch in code blocks
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
124 changes: 124 additions & 0 deletions
124
src/Elastic.Markdown/Myst/InlineParsers/Substitution/SubstitutionMutationHelper.cs
This file contains hidden or 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,124 @@ | ||
| // 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; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using Elastic.Documentation; | ||
|
|
||
| namespace Elastic.Markdown.Myst.InlineParsers.Substitution; | ||
|
|
||
| /// <summary> | ||
| /// Shared utility for parsing and applying substitution mutations | ||
| /// </summary> | ||
| public static class SubstitutionMutationHelper | ||
| { | ||
| /// <summary> | ||
| /// Parses a substitution key with mutations and returns the key and mutation components | ||
| /// </summary> | ||
| /// <param name="rawKey">The raw substitution key (e.g., "version.stack | M.M")</param> | ||
| /// <returns>A tuple containing the cleaned key and array of mutation strings</returns> | ||
| public static (string Key, string[] Mutations) ParseKeyWithMutations(string rawKey) | ||
| { | ||
| // Improved handling of pipe-separated components with better whitespace handling | ||
| var components = rawKey.Split('|', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); | ||
| var key = components[0].Trim(); | ||
| var mutations = components.Length > 1 ? components[1..] : []; | ||
|
|
||
| return (key, mutations); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies mutations to a value using the existing SubstitutionMutation system | ||
| /// </summary> | ||
| /// <param name="value">The original value to transform</param> | ||
| /// <param name="mutations">Collection of SubstitutionMutation enums to apply</param> | ||
| /// <returns>The transformed value</returns> | ||
| public static string ApplyMutations(string value, IReadOnlyCollection<SubstitutionMutation> mutations) | ||
| { | ||
| var result = value; | ||
| foreach (var mutation in mutations) | ||
| { | ||
| result = ApplySingleMutation(result, mutation); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies mutations to a value using the existing SubstitutionMutation system | ||
| /// </summary> | ||
| /// <param name="value">The original value to transform</param> | ||
| /// <param name="mutations">Array of mutation strings to apply</param> | ||
| /// <returns>The transformed value</returns> | ||
| public static string ApplyMutations(string value, string[] mutations) | ||
| { | ||
| var result = value; | ||
| foreach (var mutationStr in mutations) | ||
| { | ||
| var trimmedMutation = mutationStr.Trim(); | ||
| if (SubstitutionMutationExtensions.TryParse(trimmedMutation, out var mutation, true, true)) | ||
| { | ||
| result = ApplySingleMutation(result, mutation); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies a single mutation to a value | ||
| /// </summary> | ||
| /// <param name="value">The value to transform</param> | ||
| /// <param name="mutation">The mutation to apply</param> | ||
| /// <returns>The transformed value</returns> | ||
| private static string ApplySingleMutation(string value, SubstitutionMutation mutation) | ||
| { | ||
| var (success, result) = mutation switch | ||
| { | ||
| SubstitutionMutation.MajorComponent => TryGetVersion(value, v => $"{v.Major}"), | ||
| SubstitutionMutation.MajorX => TryGetVersion(value, v => $"{v.Major}.x"), | ||
| SubstitutionMutation.MajorMinor => TryGetVersion(value, v => $"{v.Major}.{v.Minor}"), | ||
| SubstitutionMutation.IncreaseMajor => TryGetVersion(value, v => $"{v.Major + 1}.0.0"), | ||
| SubstitutionMutation.IncreaseMinor => TryGetVersion(value, v => $"{v.Major}.{v.Minor + 1}.0"), | ||
| SubstitutionMutation.LowerCase => (true, value.ToLowerInvariant()), | ||
| SubstitutionMutation.UpperCase => (true, value.ToUpperInvariant()), | ||
| SubstitutionMutation.Capitalize => (true, Capitalize(value)), | ||
| SubstitutionMutation.KebabCase => (true, ToKebabCase(value)), | ||
| SubstitutionMutation.CamelCase => (true, ToCamelCase(value)), | ||
| SubstitutionMutation.PascalCase => (true, ToPascalCase(value)), | ||
| SubstitutionMutation.SnakeCase => (true, ToSnakeCase(value)), | ||
| SubstitutionMutation.TitleCase => (true, TitleCase(value)), | ||
| SubstitutionMutation.Trim => (true, Trim(value)), | ||
| _ => (false, value) | ||
| }; | ||
| return success ? result : value; | ||
| } | ||
|
|
||
| private static (bool Success, string Result) TryGetVersion(string version, Func<SemVersion, string> transform) | ||
| { | ||
| if (!SemVersion.TryParse(version, out var v) && !SemVersion.TryParse(version + ".0", out v)) | ||
| return (false, version); | ||
| return (true, transform(v)); | ||
| } | ||
|
|
||
| // These methods match the exact implementation in SubstitutionRenderer | ||
| private static string Capitalize(string input) => | ||
| input switch | ||
| { | ||
| null => string.Empty, | ||
| "" => string.Empty, | ||
| _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1)) | ||
| }; | ||
|
|
||
| private static string ToKebabCase(string str) => JsonNamingPolicy.KebabCaseLower.ConvertName(str).Replace(" ", string.Empty); | ||
|
|
||
| private static string ToCamelCase(string str) => JsonNamingPolicy.CamelCase.ConvertName(str).Replace(" ", string.Empty); | ||
|
|
||
| private static string ToPascalCase(string str) => TitleCase(str).Replace(" ", string.Empty); | ||
|
|
||
| private static string ToSnakeCase(string str) => JsonNamingPolicy.SnakeCaseLower.ConvertName(str).Replace(" ", string.Empty); | ||
|
|
||
| private static string TitleCase(string str) => str.Split(' ').Select(word => Capitalize(word)).Aggregate((a, b) => $"{a} {b}"); | ||
|
|
||
| private static string Trim(string str) => str.TrimEnd('!', '.', ',', ';', ':', '?', ' ', '\t', '\n', '\r'); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.