Skip to content

Commit e787974

Browse files
authored
Reproducible IDs (#1335)
* Reproducible IDs Generate the ID based on a stable unique attribute * Add NavigationSource * Refactor in to static helper class * rename
1 parent 7ebaae6 commit e787974

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
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[] components) => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(string.Join("", components))))[..8];
13+
}

src/Elastic.Markdown/IO/MarkdownFile.cs

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

55
using System.IO.Abstractions;
66
using System.Runtime.InteropServices;
7+
using System.Security.Cryptography;
8+
using System.Text;
79
using Elastic.Documentation.Diagnostics;
810
using Elastic.Documentation.Navigation;
911
using Elastic.Markdown.Diagnostics;
@@ -51,6 +53,7 @@ DocumentationSet set
5153
_configurationFile = build.Configuration.SourceFile;
5254
_globalSubstitutions = build.Configuration.Substitutions;
5355
_set = set;
56+
Id = ShortId.Create(FilePath);
5457
//may be updated by DocumentationGroup.ProcessTocItems
5558
//todo refactor mutability of MarkdownFile as a whole
5659
ScopeDirectory = build.Configuration.ScopeDirectory;
@@ -65,7 +68,7 @@ DocumentationSet set
6568

6669
public Uri NavigationSource { get; set; }
6770

68-
public string Id { get; } = Guid.NewGuid().ToString("N")[..8];
71+
public string Id { get; }
6972

7073
private IDiagnosticsCollector Collector { get; }
7174

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Security.Cryptography;
8+
using System.Text;
79
using Elastic.Documentation;
810
using Elastic.Documentation.Configuration.TableOfContents;
11+
using Elastic.Markdown.Helpers;
912

1013
namespace Elastic.Markdown.IO.Navigation;
1114

@@ -125,7 +128,7 @@ public class DocumentationGroup : INavigationGroup
125128
{
126129
private readonly TableOfContentsTreeCollector _treeCollector;
127130

128-
public string Id { get; } = Guid.NewGuid().ToString("N")[..8];
131+
public string Id { get; }
129132

130133
public string NavigationRootId => NavigationRoot.Id;
131134

@@ -196,7 +199,7 @@ protected DocumentationGroup(
196199
GroupsInOrder = groups;
197200
FilesInOrder = files;
198201
NavigationItems = navigationItems;
199-
202+
Id = ShortId.Create(NavigationSource.ToString(), FolderName);
200203
if (Index is not null)
201204
FilesInOrder = [.. FilesInOrder.Except([Index])];
202205
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
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
4+
5+
using System.Security.Cryptography;
46
using System.Text;
7+
using Elastic.Markdown.Helpers;
58
using Elastic.Markdown.Myst.Settings;
69

710
namespace Elastic.Markdown.Slices.Directives;
@@ -56,7 +59,13 @@ public class ImageViewModel
5659
public required string? Target { get; init; }
5760
public required string? Width { get; init; }
5861
public required string? ImageUrl { get; init; }
59-
public string UniqueImageId { get; } = Guid.NewGuid().ToString("N")[..8];
62+
63+
private string? _uniqueImageId;
64+
65+
public string UniqueImageId =>
66+
_uniqueImageId ??= string.IsNullOrEmpty(ImageUrl)
67+
? Guid.NewGuid().ToString("N")[..8] // fallback to a random ID if ImageUrl is null or empty
68+
: ShortId.Create(ImageUrl);
6069
public required string? Screenshot { get; init; }
6170

6271
public string Style

src/Elastic.Markdown/Slices/Layout/_PagesNav.cshtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
<aside class="sidebar bg-white fixed md:sticky shadow-2xl md:shadow-none left-[100%] group-has-[#pages-nav-hamburger:checked]/body:left-0 bottom-0 md:left-auto pl-6 md:pl-2 top-[calc(var(--offset-top)+1px)] w-[80%] md:w-auto shrink-0 border-r-1 border-r-grey-20 z-40 md:z-auto">
33
<nav
44
id="pages-nav"
5-
class="sidebar-nav h-full"
6-
@* used to invalidate session storage *@
7-
data-current-navigation="@LayoutViewModel.CurrentNavigationId">
5+
class="sidebar-nav h-full">
86
@(new HtmlString(Model.NavigationHtml))
97
</nav>
108
@* ReSharper disable once Html.IdNotResolved *@

src/Elastic.Markdown/Slices/_ViewModels.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public class IndexViewModel
4646
public class LayoutViewModel
4747
{
4848
public required string DocSetName { get; init; }
49-
50-
/// Used to identify the navigation for the current compilation
51-
/// We want to reset users sessionStorage every time this changes to invalidate
52-
/// the guids that no longer exist
53-
public static string CurrentNavigationId { get; } = Guid.NewGuid().ToString("N")[..8];
5449
public string Title { get; set; } = "Elastic Documentation";
5550
public required string Description { get; init; }
5651
public required IReadOnlyCollection<PageTocItem> PageTocItems { get; init; }

0 commit comments

Comments
 (0)