Skip to content

Commit 690232b

Browse files
committed
Reproducible IDs
Generate the ID based on a stable unique attribute
1 parent 55e52e2 commit 690232b

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

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 = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(FilePath)))[..8];
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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;
911

@@ -125,7 +127,7 @@ public class DocumentationGroup : INavigationGroup
125127
{
126128
private readonly TableOfContentsTreeCollector _treeCollector;
127129

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

130132
public string NavigationRootId => NavigationRoot.Id;
131133

@@ -196,6 +198,7 @@ protected DocumentationGroup(
196198
GroupsInOrder = groups;
197199
FilesInOrder = files;
198200
NavigationItems = navigationItems;
201+
Id = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(FolderName + depth)))[..8];
199202

200203
if (Index is not null)
201204
FilesInOrder = [.. FilesInOrder.Except([Index])];

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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;
57
using Elastic.Markdown.Myst.Settings;
68

@@ -56,7 +58,13 @@ public class ImageViewModel
5658
public required string? Target { get; init; }
5759
public required string? Width { get; init; }
5860
public required string? ImageUrl { get; init; }
59-
public string UniqueImageId { get; } = Guid.NewGuid().ToString("N")[..8];
61+
62+
private string? _uniqueImageId;
63+
64+
public string UniqueImageId =>
65+
_uniqueImageId ??= string.IsNullOrEmpty(ImageUrl)
66+
? 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];
6068
public required string? Screenshot { get; init; }
6169

6270
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)