Skip to content

Commit 67a6148

Browse files
Adjust web side for new tooling changes (Key -> Keys and OrderOnPage) (#674)
## Description Adding support for new Tooling side update: adjusting from `SiteMapping.Key` to `Keys` as well as sorting by `OrderOnPage` since the new Tooling update changes how sitemap.json is ordered. ### Ensure that your pull request has followed all the steps below: - [x] Code compilation - [ ] Created tests which fail without the change (if possible) - [x] All tests passing --------- Co-authored-by: Benjamin Michaelis <[email protected]>
1 parent 11d19d4 commit 67a6148

File tree

8 files changed

+63
-35
lines changed

8 files changed

+63
-35
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
5-
<ToolingPackagesVersion>1.1.1.4415</ToolingPackagesVersion>
5+
<ToolingPackagesVersion>1.1.1.4520</ToolingPackagesVersion>
66
<AccessToNugetFeed>true</AccessToNugetFeed>
77
<RestoreSources>
88
https://api.nuget.org/v3/index.json;

EssentialCSharp.Web.Tests/SiteMappingTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace EssentialCSharp.Web.Tests;
55
public class SiteMappingTests
66
{
77
static SiteMapping HelloWorldSiteMapping => new(
8-
key: "hello-world",
8+
keys: ["hello-world"],
99
pagePath:
1010
[
1111
"Chapters",
@@ -15,14 +15,15 @@ public class SiteMappingTests
1515
],
1616
chapterNumber: 1,
1717
pageNumber: 1,
18+
orderOnPage: 1,
1819
chapterTitle: "Introducing C#",
1920
rawHeading: "Introduction",
2021
anchorId: "hello-world",
2122
indentLevel: 0
2223
);
2324

2425
static SiteMapping CSyntaxFundamentalsSiteMapping => new(
25-
key: "c-syntax-fundamentals",
26+
keys: ["c-syntax-fundamentals"],
2627
pagePath:
2728
[
2829
"Chapters",
@@ -32,6 +33,7 @@ public class SiteMappingTests
3233
],
3334
chapterNumber: 1,
3435
pageNumber: 2,
36+
orderOnPage: 1,
3537
chapterTitle: "Introducing C#",
3638
rawHeading: "C# Syntax Fundamentals",
3739
anchorId: "c-syntax-fundamentals",
@@ -52,30 +54,30 @@ public void FindHelloWorldWithAnchorSlugReturnsCorrectSiteMap()
5254
{
5355
SiteMapping? foundSiteMap = GetSiteMap().Find("hello-world#hello-world");
5456
Assert.NotNull(foundSiteMap);
55-
Assert.Equal(HelloWorldSiteMapping, foundSiteMap);
57+
Assert.Equivalent(HelloWorldSiteMapping, foundSiteMap);
5658
}
5759

5860
[Fact]
5961
public void FindCSyntaxFundamentalsWithSpacesReturnsCorrectSiteMap()
6062
{
6163
SiteMapping? foundSiteMap = GetSiteMap().Find("C# Syntax Fundamentals");
6264
Assert.NotNull(foundSiteMap);
63-
Assert.Equal(CSyntaxFundamentalsSiteMapping, foundSiteMap);
65+
Assert.Equivalent(CSyntaxFundamentalsSiteMapping, foundSiteMap);
6466
}
6567

6668
[Fact]
6769
public void FindCSyntaxFundamentalsWithSpacesAndAnchorReturnsCorrectSiteMap()
6870
{
6971
SiteMapping? foundSiteMap = GetSiteMap().Find("C# Syntax Fundamentals#hello-world");
7072
Assert.NotNull(foundSiteMap);
71-
Assert.Equal(CSyntaxFundamentalsSiteMapping, foundSiteMap);
73+
Assert.Equivalent(CSyntaxFundamentalsSiteMapping, foundSiteMap);
7274
}
7375

7476
[Fact]
7577
public void FindCSyntaxFundamentalsSanitizedWithAnchorReturnsCorrectSiteMap()
7678
{
7779
SiteMapping? foundSiteMap = GetSiteMap().Find("c-syntax-fundamentals#hello-world");
7880
Assert.NotNull(foundSiteMap);
79-
Assert.Equal(CSyntaxFundamentalsSiteMapping, foundSiteMap);
81+
Assert.Equivalent(CSyntaxFundamentalsSiteMapping, foundSiteMap);
8082
}
8183
}

EssentialCSharp.Web/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private string FlipPage(int currentChapter, int currentPage, bool next)
125125
return "";
126126
}
127127
}
128-
return $"{siteMap.Key}#{siteMap.AnchorId}";
128+
return $"{siteMap.Keys.First()}#{siteMap.AnchorId}";
129129
}
130130

131131
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class SiteMappingListExtensions
1616
}
1717
foreach (string? potentialMatch in key.GetPotentialMatches())
1818
{
19-
if (siteMappings.FirstOrDefault(x => x.Key == potentialMatch) is { } siteMap)
19+
if (siteMappings.FirstOrDefault(x => x.Keys.Any(x => x == potentialMatch)) is { } siteMap)
2020
{
2121
return siteMap;
2222
}

EssentialCSharp.Web/Services/ISiteMappingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
public interface ISiteMappingService
44
{
55
IList<SiteMapping> SiteMappings { get; }
6+
IEnumerable<SiteMappingDto> GetTocData();
67
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace EssentialCSharp.Web.Services;
2+
3+
// Data transfer object to pass necessary SiteMapping data info
4+
// to frontend for use in table of contents
5+
public class SiteMappingDto
6+
{
7+
public required int Level { get; set; }
8+
public required string Key { get; set; }
9+
public required string Href { get; set; }
10+
public required string Title { get; set; }
11+
public required IEnumerable<SiteMappingDto> Items { get; set; }
12+
}

EssentialCSharp.Web/Services/SiteMappingService.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,42 @@ public SiteMappingService(IWebHostEnvironment webHostEnvironment)
1212
List<SiteMapping>? siteMappings = System.Text.Json.JsonSerializer.Deserialize<List<SiteMapping>>(File.OpenRead(path)) ?? throw new InvalidOperationException("No table of contents found");
1313
SiteMappings = siteMappings;
1414
}
15+
16+
public IEnumerable<SiteMappingDto> GetTocData()
17+
{
18+
return SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key).Select(x =>
19+
{
20+
IEnumerable<SiteMapping> orderedGrouping = x.OrderBy(i => i.PageNumber).ThenBy(i => i.OrderOnPage);
21+
SiteMapping firstElement = orderedGrouping.First();
22+
return new SiteMappingDto()
23+
{
24+
Level = 0,
25+
Key = firstElement.Keys.First(),
26+
Href = $"{firstElement.Keys.First()}#{firstElement.AnchorId}",
27+
Title = $"Chapter {x.Key}: {firstElement.ChapterTitle}",
28+
Items = GetItems(orderedGrouping.Skip(1), 1)
29+
};
30+
}
31+
);
32+
}
33+
34+
private static IEnumerable<SiteMappingDto> GetItems(IEnumerable<SiteMapping> chapterItems, int indentLevel)
35+
{
36+
return chapterItems
37+
// Examine all items up until we move up to a level higher than where we're starting,
38+
// which would indicate that we've reached the end of the entries nested under `indentationLevel`
39+
.TakeWhile(i => i.IndentLevel >= indentLevel)
40+
// Of all the multi-level descendants we found, take only those at the current level that we're wanting to render.
41+
.Where(i => i.IndentLevel == indentLevel)
42+
.Select(i => new SiteMappingDto()
43+
{
44+
Level = indentLevel,
45+
Key = i.Keys.First(),
46+
Href = $"{i.Keys.First()}#{i.AnchorId}",
47+
Title = i.RawHeading,
48+
// Any children of this node will be /after/ this node,
49+
// so skip any items that are /before/ the current node.
50+
Items = GetItems(chapterItems.SkipWhile(q => i.Keys.First() != q.Keys.First()).Skip(1), indentLevel + 1)
51+
});
52+
}
1553
}

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -266,32 +266,7 @@
266266
@await RenderSectionAsync("Scripts", required: false);
267267
<script>
268268
@{
269-
object GetItems(IEnumerable<SiteMapping> chapterItems, int indentLevel) => chapterItems
270-
// Skip the chapter entry itself
271-
.Skip(1)
272-
// Examine all items up until we move up to a level higher than where we're starting,
273-
// which would indicate that we've reached the end of the entries nested under `indentationLevel`
274-
.TakeWhile(i => i.IndentLevel >= indentLevel)
275-
// Of all the multi-level descendants we found, take only those at the current level that we're wanting to render.
276-
.Where(i => i.IndentLevel == indentLevel)
277-
.Select(i => new
278-
{
279-
Level = indentLevel,
280-
Key = i.Key,
281-
Href = $"{i.Key}#{i.AnchorId}",
282-
Title = i.RawHeading,
283-
// Any children of this node will be /after/ this node,
284-
// so skip any items that are /before/ the current node.
285-
Items = GetItems(chapterItems.SkipWhile(q => i.Key != q.Key), indentLevel + 1)
286-
});
287-
var tocData = _SiteMappings.SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key).Select(x => new
288-
{
289-
Level = 0,
290-
Key = x.First().Key,
291-
Href = $"{x.First().Key}#{x.First().AnchorId}",
292-
Title = $"Chapter {x.Key}: {x.First().ChapterTitle}",
293-
Items = GetItems(x, 1)
294-
});
269+
var tocData = _SiteMappings.GetTocData();
295270
}
296271
297272
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)

0 commit comments

Comments
 (0)