Skip to content

Commit 830ab85

Browse files
committed
Refactor in to static helper class
1 parent 3c66ec3 commit 830ab85

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Security.Cryptography;
6+
using System.Text;
7+
8+
namespace Elastic.Markdown.Helpers;
9+
10+
public static class ShortId
11+
{
12+
public static string Create(params string[] text) => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(string.Join("", text))))[..8];
13+
}

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ DocumentationSet set
5353
_configurationFile = build.Configuration.SourceFile;
5454
_globalSubstitutions = build.Configuration.Substitutions;
5555
_set = set;
56-
Id = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(FilePath)))[..8];
56+
Id = ShortId.Create(FilePath);
5757
//may be updated by DocumentationGroup.ProcessTocItems
5858
//todo refactor mutability of MarkdownFile as a whole
5959
ScopeDirectory = build.Configuration.ScopeDirectory;

src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using Elastic.Documentation;
1010
using Elastic.Documentation.Configuration.TableOfContents;
11+
using Elastic.Markdown.Helpers;
1112

1213
namespace Elastic.Markdown.IO.Navigation;
1314

@@ -198,7 +199,7 @@ protected DocumentationGroup(
198199
GroupsInOrder = groups;
199200
FilesInOrder = files;
200201
NavigationItems = navigationItems;
201-
Id = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(NavigationSource + FolderName + depth)))[..8];
202+
Id = ShortId.Create(NavigationSource.ToString(), FolderName);
202203
if (Index is not null)
203204
FilesInOrder = [.. FilesInOrder.Except([Index])];
204205
}

src/Elastic.Markdown/Slices/Directives/_ViewModels.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Security.Cryptography;
66
using System.Text;
7+
using Elastic.Markdown.Helpers;
78
using Elastic.Markdown.Myst.Settings;
89

910
namespace Elastic.Markdown.Slices.Directives;
@@ -64,7 +65,7 @@ public class ImageViewModel
6465
public string UniqueImageId =>
6566
_uniqueImageId ??= string.IsNullOrEmpty(ImageUrl)
6667
? Guid.NewGuid().ToString("N")[..8] // fallback to a random ID if ImageUrl is null or empty
67-
: Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(ImageUrl)))[..8];
68+
: ShortId.Create(ImageUrl);
6869
public required string? Screenshot { get; init; }
6970

7071
public string Style

0 commit comments

Comments
 (0)