Skip to content

Commit 9b2f9df

Browse files
cottireakaleek
andauthored
Implement sitemap (#828)
* WIP * Output sitemap on .artifacts/assembly * Remove duplicate entries and validate sitemap as per specs * Send SitemapBuilder to its own class * Add newline --------- Co-authored-by: Jan Calanog <[email protected]>
1 parent 2de46fb commit 9b2f9df

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.IO.Abstractions;
6+
using System.Xml.Linq;
7+
using Elastic.Markdown.IO.Navigation;
8+
9+
namespace Documentation.Assembler.Building;
10+
11+
public class SitemapBuilder(IReadOnlyCollection<INavigationItem> navigationItems, IFileSystem fileSystem, IDirectoryInfo outputFolder)
12+
{
13+
private static readonly Uri BaseUri = new("https://www.elastic.co");
14+
private readonly IReadOnlyCollection<INavigationItem> _navigationItems = navigationItems;
15+
private readonly IFileSystem _fileSystem = fileSystem;
16+
private readonly IDirectoryInfo _outputFolder = outputFolder;
17+
18+
public void Generate()
19+
{
20+
var flattenedNavigationItems = GetNavigationItems(_navigationItems);
21+
22+
var doc = new XDocument()
23+
{
24+
Declaration = new XDeclaration("1.0", "utf-8", "yes"),
25+
};
26+
27+
var root = new XElement(
28+
"urlset",
29+
new XAttribute("xlmns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
30+
flattenedNavigationItems
31+
.OfType<FileNavigationItem>()
32+
.Select(n => n.File.Url)
33+
.Distinct()
34+
.Select(u => new Uri(BaseUri, u))
35+
.Select(u => new XElement("url", new XElement("loc", u)))
36+
);
37+
38+
doc.Add(root);
39+
40+
using var fileStream = _fileSystem.File.Create(Path.Combine(_outputFolder.ToString() ?? string.Empty, "sitemap.xml"));
41+
doc.Save(fileStream);
42+
}
43+
44+
private static IReadOnlyCollection<INavigationItem> GetNavigationItems(IReadOnlyCollection<INavigationItem> items)
45+
{
46+
var result = new List<INavigationItem>();
47+
foreach (var item in items)
48+
{
49+
switch (item)
50+
{
51+
case FileNavigationItem file:
52+
result.Add(file);
53+
break;
54+
case GroupNavigationItem group:
55+
result.AddRange(GetNavigationItems(group.Group.NavigationItems));
56+
break;
57+
}
58+
}
59+
return result;
60+
}
61+
}

src/docs-assembler/Cli/RepositoryCommands.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Documentation.Assembler.Sourcing;
1313
using Elastic.Documentation.Tooling.Diagnostics.Console;
1414
using Elastic.Markdown.CrossLinks;
15+
using Elastic.Markdown.IO.Navigation;
1516
using Microsoft.Extensions.Logging;
1617

1718
namespace Documentation.Assembler.Cli;
@@ -77,7 +78,6 @@ public async Task<int> BuildAll(
7778
Force = force ?? false,
7879
AllowIndexing = allowIndexing ?? false,
7980
};
80-
8181
var cloner = new AssemblerRepositorySourcer(logger, assembleContext);
8282
var checkouts = cloner.GetAll().ToArray();
8383
if (checkouts.Length == 0)
@@ -90,10 +90,13 @@ public async Task<int> BuildAll(
9090

9191
var pathProvider = new GlobalNavigationPathProvider(assembleSources, assembleContext);
9292
var htmlWriter = new GlobalNavigationHtmlWriter(assembleContext, navigation, assembleSources);
93-
var builder = new AssemblerBuilder(logger, assembleContext, htmlWriter, pathProvider);
9493

94+
var builder = new AssemblerBuilder(logger, assembleContext, htmlWriter, pathProvider);
9595
await builder.BuildAllAsync(assembleSources.AssembleSets, ctx);
9696

97+
var sitemapBuilder = new SitemapBuilder(navigation.NavigationItems, assembleContext.WriteFileSystem, assembleContext.OutputDirectory);
98+
sitemapBuilder.Generate();
99+
97100
if (strict ?? false)
98101
return collector.Errors + collector.Warnings;
99102
return collector.Errors;

0 commit comments

Comments
 (0)