|
| 1 | +using downpatch.Data; |
| 2 | +using downpatch.Services; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using System.Text; |
| 5 | +using System.Xml.Linq; |
| 6 | + |
| 7 | +namespace downpatch.Endpoints |
| 8 | +{ |
| 9 | + public static class SitemapEndpoints |
| 10 | + { |
| 11 | + public static IEndpointRouteBuilder MapSitemap(this IEndpointRouteBuilder app) |
| 12 | + { |
| 13 | + app.MapGet("/sitemap.xml", ( |
| 14 | + HttpContext ctx, |
| 15 | + MarkdownStore store, |
| 16 | + IOptions<MarkdownOptions> opt) => |
| 17 | + { |
| 18 | + var max = Math.Clamp(opt.Value.MaxSitemapUrls, 1, 50_000); |
| 19 | + |
| 20 | + var baseUrl = $"{ctx.Request.Scheme}://{ctx.Request.Host}".TrimEnd('/'); |
| 21 | + |
| 22 | + XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; |
| 23 | + var urlset = new XElement(ns + "urlset"); |
| 24 | + |
| 25 | + int count = 0; |
| 26 | + foreach (var (slug, last) in store.EnumerateAll().OrderBy(x => x.slug)) |
| 27 | + { |
| 28 | + if (count++ >= max) break; |
| 29 | + |
| 30 | + var loc = string.IsNullOrWhiteSpace(slug) ? $"{baseUrl}/" : $"{baseUrl}/guide/{slug}"; |
| 31 | + //trim "index" from the end of URLs |
| 32 | + if (loc.EndsWith("/index", StringComparison.OrdinalIgnoreCase)) |
| 33 | + { |
| 34 | + loc = loc.Substring(0, loc.Length - 6); |
| 35 | + } |
| 36 | + |
| 37 | + urlset.Add(new XElement(ns + "url", |
| 38 | + new XElement(ns + "loc", loc), |
| 39 | + new XElement(ns + "lastmod", last.ToString("yyyy-MM-dd")) |
| 40 | + )); |
| 41 | + } |
| 42 | + |
| 43 | + //Double check no duplicates (shouldn't be any, but just in case) |
| 44 | + var distinctUrls = urlset.Elements(ns + "url") |
| 45 | + .GroupBy(x => x.Element(ns + "loc")?.Value) |
| 46 | + .Select(g => g.First()) |
| 47 | + .ToList(); |
| 48 | + |
| 49 | + urlset.RemoveAll(); |
| 50 | + urlset.Add(distinctUrls); |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + var doc = new XDocument(urlset); |
| 55 | + return Results.Text(doc.ToString(SaveOptions.DisableFormatting), "application/xml", Encoding.UTF8); |
| 56 | + }); |
| 57 | + |
| 58 | + return app; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments