Skip to content

Commit e183c9d

Browse files
authored
Include Documentation Group index urls in sitemap (#1441)
* Include Documentation Group index urls in sitemap * Include group itself in GetNavigationItems() * Skip hidden navigation items in sitemap builder * Explicitly include xelements into namespace * ignore hidden for DetectionRuleFile
1 parent 634b95c commit e183c9d

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
namespace Elastic.Markdown.IO.Navigation;
99

1010
[DebuggerDisplay("Current: {Model.RelativePath}")]
11-
public record FileNavigationItem(MarkdownFile Model, DocumentationGroup Group, bool Hidden = false) : ILeafNavigationItem<MarkdownFile>
11+
public record FileNavigationItem(MarkdownFile Model, DocumentationGroup Group, bool Hidden = false)
12+
: ILeafNavigationItem<MarkdownFile>
1213
{
1314
public INodeNavigationItem<INavigationModel, INavigationItem>? Parent { get; set; } = Group;
1415
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; } = Group.NavigationRoot;

src/tooling/docs-assembler/Building/SitemapBuilder.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO.Abstractions;
66
using System.Xml.Linq;
77
using Elastic.Documentation.Site.Navigation;
8+
using Elastic.Markdown.Extensions.DetectionRules;
89
using Elastic.Markdown.IO.Navigation;
910

1011
namespace Documentation.Assembler.Building;
@@ -21,25 +22,32 @@ public void Generate()
2122
{
2223
var flattenedNavigationItems = GetNavigationItems(navigationItems);
2324

24-
var doc = new XDocument()
25+
var doc = new XDocument
2526
{
26-
Declaration = new XDeclaration("1.0", "utf-8", "yes"),
27+
Declaration = new XDeclaration("1.0", "utf-8", "yes")
2728
};
2829

30+
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
31+
2932
var currentDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz");
3033
var root = new XElement(
31-
"urlset",
32-
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
33-
flattenedNavigationItems
34-
.OfType<FileNavigationItem>()
35-
.Select(n => n.Model.Url)
36-
.Distinct()
37-
.Select(u => new Uri(BaseUri, u))
38-
.Select(u => new XElement("url", [
39-
new XElement("loc", u),
40-
new XElement("lastmod", currentDate)
41-
]))
42-
);
34+
ns + "urlset",
35+
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
36+
flattenedNavigationItems
37+
.Select(n => n switch
38+
{
39+
DocumentationGroup group => (group.Index.Url, NavigationItem: group),
40+
FileNavigationItem file => (file.Model.Url, NavigationItem: file as INavigationItem),
41+
_ => throw new Exception($"Unhandled navigation item type: {n.GetType()}")
42+
})
43+
.Select(n => n.Url)
44+
.Distinct()
45+
.Select(u => new Uri(BaseUri, u))
46+
.Select(u => new XElement(ns + "url", [
47+
new XElement(ns + "loc", u),
48+
new XElement(ns + "lastmod", currentDate)
49+
]))
50+
);
4351

4452
doc.Add(root);
4553

@@ -55,13 +63,24 @@ private static IReadOnlyCollection<INavigationItem> GetNavigationItems(IReadOnly
5563
switch (item)
5664
{
5765
case FileNavigationItem file:
66+
// these are hidden from the navigation programatically.
67+
// TODO find a cleaner way to model this.
68+
if (item.Hidden && file.Model is not DetectionRuleFile)
69+
continue;
5870
result.Add(file);
5971
break;
6072
case DocumentationGroup group:
73+
if (item.Hidden)
74+
continue;
75+
6176
result.AddRange(GetNavigationItems(group.NavigationItems));
77+
result.Add(group);
6278
break;
79+
default:
80+
throw new Exception($"Unhandled navigation item type: {item.GetType()}");
6381
}
6482
}
83+
6584
return result;
6685
}
6786
}

0 commit comments

Comments
 (0)