Skip to content

Commit 0fd16ed

Browse files
committed
Address peer edits
1 parent d597bec commit 0fd16ed

File tree

5 files changed

+22
-39
lines changed

5 files changed

+22
-39
lines changed

src/Elastic.Documentation.Site/Assets/open-details-with-anchor.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ export function openDetailsWithAnchor() {
66
if (target) {
77
const closestDetails = target.closest('details')
88
if (closestDetails && !closestDetails.open) {
9-
// Mark that we're doing a programmatic open
10-
isProgrammaticOpen = true
11-
closestDetails.open = true
12-
13-
// Reset the flag after the toggle event has fired
14-
setTimeout(() => {
15-
isProgrammaticOpen = false
16-
}, 10)
9+
// Only open if it's not already open by default
10+
if (!closestDetails.dataset.openDefault) {
11+
closestDetails.open = true
12+
}
1713
}
1814

1915
// Chrome automatically ensures parent content is visible, scroll immediately
@@ -38,9 +34,6 @@ function updateUrlForDropdown(details: HTMLDetailsElement, isOpening: boolean) {
3834
// This keeps the URL consistent with how headings behave
3935
}
4036

41-
// Track if we're currently in a programmatic open operation
42-
let isProgrammaticOpen = false
43-
4437
// Initialize the anchor handling functionality
4538
export function initOpenDetailsWithAnchor() {
4639
// Handle initial page load
@@ -49,23 +42,22 @@ export function initOpenDetailsWithAnchor() {
4942
// Handle hash changes within the same page (e.g., clicking anchor links)
5043
window.addEventListener('hashchange', openDetailsWithAnchor)
5144

52-
// Remove data-skip-url-update on first click to enable URL updates
45+
// Remove data-open-default on first click to enable URL updates
5346
document.addEventListener(
5447
'click',
5548
(event) => {
5649
const target = event.target as HTMLElement
5750
const dropdown = target.closest(
5851
'details.dropdown'
5952
) as HTMLDetailsElement
60-
if (dropdown && dropdown.dataset.skipUrlUpdate === 'true') {
61-
delete dropdown.dataset.skipUrlUpdate
53+
if (dropdown && dropdown.dataset.openDefault === 'true') {
54+
delete dropdown.dataset.openDefault
6255
}
6356
},
6457
true
6558
)
6659

6760
// Handle manual dropdown toggling to update URL
68-
// Use event delegation to catch all toggle events
6961
document.addEventListener(
7062
'toggle',
7163
(event) => {
@@ -79,15 +71,12 @@ export function initOpenDetailsWithAnchor() {
7971
const details = target as HTMLDetailsElement
8072
const isOpening = details.open
8173

82-
// Only update URL if NOT skipping and NOT a programmatic open
83-
if (!details.dataset.skipUrlUpdate && !isProgrammaticOpen) {
84-
// Use setTimeout to ensure the toggle state has been processed
85-
setTimeout(() => {
86-
updateUrlForDropdown(details, isOpening)
87-
}, 0)
74+
// Only update URL if NOT open by default (until first interaction)
75+
if (!details.dataset.openDefault) {
76+
updateUrlForDropdown(details, isOpening)
8877
}
8978
}
9079
},
9180
true
92-
) // Use capture phase to ensure we catch the event
81+
)
9382
}

src/Elastic.Markdown/Myst/Directives/Admonition/AdmonitionBlock.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public override void FinalizeAndValidate(ParserContext context)
4747
// Auto-generate CrossReferenceName for dropdowns without explicit name
4848
if (string.IsNullOrEmpty(CrossReferenceName) && (Admonition == "dropdown" || Classes == "dropdown"))
4949
{
50+
// Only generate a simple slug if no explicit name is provided
51+
// This prevents ugly numbered anchors and forces content authors to be explicit about naming
52+
// when they need linkable dropdowns
5053
var baseSlug = Title.Slugify();
51-
CrossReferenceName = context.GetUniqueSlug($"dropdown-{baseSlug}");
54+
CrossReferenceName = baseSlug;
5255
}
5356
}
5457
}

src/Elastic.Markdown/Myst/Directives/Dropdown/DropdownView.cshtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
@inherits RazorSlice<Elastic.Markdown.Myst.Directives.Admonition.AdmonitionViewModel>
2-
<details class="dropdown @Model.Classes" id="@Model.CrossReferenceName" open="@Model.Open" data-skip-url-update="@(Model.Open != null ? "true" : null)">
2+
<details class="dropdown @Model.Classes"
3+
id="@Model.CrossReferenceName"
4+
open="@Model.Open"
5+
data-open-default="@(Model.Open != null ? "true" : null)">
36
<summary class="dropdown-title">
47
<span class="sd-summary-text">@Model.Title</span>
58
<svg

src/Elastic.Markdown/Myst/ParserContext.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public class ParserContext : MarkdownParserContext, IParserResolvers
6464
public IReadOnlyDictionary<string, string> Substitutions { get; }
6565
public IReadOnlyDictionary<string, string> ContextSubstitutions { get; }
6666

67-
private readonly Dictionary<string, int> _slugCounters = [];
68-
6967
public ParserContext(ParserState state)
7068
{
7169
Build = state.Build;
@@ -112,16 +110,6 @@ public ParserContext(ParserState state)
112110
ContextSubstitutions = contextSubs;
113111
}
114112

115-
public string GetUniqueSlug(string baseSlug)
116-
{
117-
if (!_slugCounters.TryGetValue(baseSlug, out var count))
118-
{
119-
_slugCounters[baseSlug] = 1;
120-
return baseSlug;
121-
}
122-
123-
count++;
124-
_slugCounters[baseSlug] = count;
125-
return $"{baseSlug}-{count}";
126-
}
113+
// Removed GetUniqueSlug method - we don't want to automatically create unique slugs
114+
// as this masks content organization issues and creates ugly, persistent anchors
127115
}

tests/Elastic.Markdown.Tests/FileSystemExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Elastic.Markdown.Tests;
1515

16-
public class FileSystemExtensionsTest(ITestOutputHelper output)
16+
public class FileSystemExtensionsTest
1717
{
1818
[Fact]
1919
public void IsSubPathOfTests()

0 commit comments

Comments
 (0)