Skip to content

Commit 4b75631

Browse files
Working solution (only if EssentialCSharp.Common adds orderOnPage)
1 parent eca311b commit 4b75631

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace EssentialCSharp.Web.Services;
2+
3+
public class SiteMappingDto
4+
{
5+
public required int Level { get; set; }
6+
public required List<string> Key { get; set; }
7+
public required string Href { get; set; }
8+
public required string Title { get; set; }
9+
public required IEnumerable<SiteMappingDto> Items { get; set; }
10+
}

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> orderedX = x.OrderBy(i => i.PageNumber).ThenBy(i => i.OrderOnPage);
21+
SiteMapping firstX = orderedX.First();
22+
return new SiteMappingDto()
23+
{
24+
Level = 0,
25+
Key = [firstX.Keys.First()],
26+
Href = $"{firstX.Keys.First()}#{firstX.AnchorId}",
27+
Title = $"Chapter {x.Key}: {firstX.ChapterTitle}",
28+
Items = GetItems(orderedX.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,
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: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -266,32 +266,33 @@
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.Keys.First(),
281-
Href = $"{i.Keys.First()}#{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.Keys.First() != q.Keys.First()), 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().Keys.First(),
291-
Href = $"{x.First().Keys.First()}#{x.First().AnchorId}",
292-
Title = $"Chapter {x.Key}: {x.First().ChapterTitle}",
293-
Items = GetItems(x, 1)
294-
});
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.Keys,
281+
// Href = $"{i.Keys.First()}#{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.Keys.First() != q.Keys.First()), 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().Keys.First(),
291+
// Href = $"{x.First().Keys.First()}#{x.First().AnchorId}",
292+
// Title = $"Chapter {x.Key}: {x.First().ChapterTitle}",
293+
// Items = GetItems(x, 1)
294+
// });
295+
var tocData = _SiteMappings.GetTocData();
295296
}
296297
297298
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)

0 commit comments

Comments
 (0)