diff --git a/src/MUI.Web/Components/Pages/DatasetStructuredData.cs b/src/MUI.Web/Components/Pages/DatasetStructuredData.cs index fa28eb4..62e2dda 100644 --- a/src/MUI.Web/Components/Pages/DatasetStructuredData.cs +++ b/src/MUI.Web/Components/Pages/DatasetStructuredData.cs @@ -32,7 +32,10 @@ public static string For(Uri origin, DatasetLicenceOptions licence) var root = origin.ToString().TrimEnd('/'); var page = root + ApiRoutes.Documentation; - var publisher = new JsonObject { ["@id"] = root + SiteStructuredData.OrganizationId }; + + // A fresh node per use: a JsonNode has one parent, and attaching the same instance a second + // time throws, which answered every measured request for the API guide with a 500. + JsonObject Publisher() => new() { ["@id"] = root + SiteStructuredData.OrganizationId }; var dataset = new JsonObject { @@ -42,8 +45,8 @@ public static string For(Uri origin, DatasetLicenceOptions licence) ["description"] = licence.Notice, ["url"] = page, ["isAccessibleForFree"] = true, - ["creator"] = publisher, - ["publisher"] = publisher, + ["creator"] = Publisher(), + ["publisher"] = Publisher(), ["license"] = (JsonNode?)licence.LicenceUrl ?? licence.LicenceId, ["keywords"] = new JsonArray("MUD", "MUSH", "MUCK", "MOO", "MU*", "telnet", "MSSP", "text games"), @@ -63,7 +66,7 @@ public static string For(Uri origin, DatasetLicenceOptions licence) ["@id"] = $"{root}#catalog", ["name"] = "MUIndex", ["url"] = page, - ["publisher"] = publisher, + ["publisher"] = Publisher(), ["dataset"] = new JsonObject { ["@id"] = $"{root}#dataset" }, }; diff --git a/src/MUI.Web/SiteIndex.cs b/src/MUI.Web/SiteIndex.cs index 641d010..62f1801 100644 --- a/src/MUI.Web/SiteIndex.cs +++ b/src/MUI.Web/SiteIndex.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using MUI.Web.Api; +using MUI.Web.Components; using MUI.Web.Data; using MUI.Web.Reference; @@ -126,9 +127,18 @@ private static string Robots(string sitemap) /// it, and the evidence for citation lift is absent. It is here because what it points at is true /// independently: a text rendering of every page, and a documented, versioned API. /// + /// The llmstxt.org shape, exactly: one H1, one blockquote, prose with no headings, then H2 + /// sections that hold nothing but - [name](url): note lines, the last of them + /// Optional. A parser for the format reads each H2 as a file list, so the reading rules and + /// the licence sit in the prose above the first H2 rather than under headings of their own, where + /// such a parser drops them. Pages are linked at their ?plain=1 address, the text + /// rendering the convention asks for, wherever says one exists. + /// + /// /// English, since it addresses a program. The licence comes from configuration so this cannot - /// claim terms the dump does not go out under, and over the fixture it says so in the first line - /// — no banner reaches this file. + /// claim terms the dump does not go out under, and over the fixture the summary opens by saying + /// so — no banner reaches this file. The reference pages come from the shipped library, so the + /// list cannot name one that is not served. /// /// private static string Llms(HttpContext http, bool measured, DatasetLicenceOptions licence) @@ -141,8 +151,8 @@ private static string Llms(HttpContext http, bool measured, DatasetLicenceOption if (!measured) { text.AppendLine("> DEMO DATA — no database is configured on this deployment, so every " - + "game below is a fixture and nothing here was measured. Do not cite it."); - text.AppendLine(); + + "game here is a fixture and nothing here was measured. Do not cite it."); + text.AppendLine(">"); } text.AppendLine("> A directory of the MU* hobby — MUSHes, MUDs, MUCKs and MOOs. Every value " @@ -158,27 +168,13 @@ private static string Llms(HttpContext http, bool measured, DatasetLicenceOption + "colour or a cell shape."); text.AppendLine(); - text.AppendLine("## Data"); - text.AppendLine(); - Link(text, "OpenAPI contract", ApiRoutes.OpenApi, "every endpoint below, machine-readable"); - Link(text, "API guide", ApiRoutes.Documentation, "what each endpoint is for, and the limits on it"); - Link(text, "Games", ApiRoutes.Games, "the catalogue, with the same facets the listing page offers"); - Link(text, "Bulk export", ApiRoutes.Dump, "the whole catalogue as one JSON document"); - Link(text, "Bulk export, by line", ApiRoutes.DumpLines, "the same, one game per line, for a loader"); + text.AppendLine("One game is `" + ApiRoutes.Games + "/{id}` in the API, with `" + + ApiRoutes.PresenceSuffix + "` and `" + ApiRoutes.AvailabilitySuffix + "` beneath it " + + "for its presence and reachability series, and `/g/{slug}` on the site. The id is " + + "durable; a slug can change, and an old one redirects for ever."); text.AppendLine(); - text.AppendLine("## Pages"); - text.AppendLine(); - Link(text, "Games", "/games", "every game we have reached, faceted on what we measured"); - Link(text, "Rankings", "/rankings", "busiest and most reachable, computed from measurements"); - Link(text, "Ecosystem", "/ecosystem", "codebase share and protocol adoption, as shares never totals"); - Link(text, "Reference", "/reference", "written pages on the codebases, clients and protocols"); - Link(text, "Archive", "/archive", "games that went dark, still probed, nothing deleted"); - Link(text, "Crawler", "/crawler", "what the crawler did, and what it is due to do"); - Link(text, "About", "/about", "how the catalogue is built, and how an operator opts out"); - text.AppendLine(); - - text.AppendLine("## Reading a value correctly"); + text.AppendLine("Reading a value correctly:"); text.AppendLine(); text.AppendLine("- **Measured** means we connected and observed it. **Declared** means the " + "game published it about itself and we did not verify it. **Derived** means it is a " @@ -196,20 +192,70 @@ private static string Llms(HttpContext http, bool measured, DatasetLicenceOption + "measures a game's quality and nothing here should be cited as ranking one."); text.AppendLine(); - text.AppendLine("## Licence"); - text.AppendLine(); text.AppendLine(licence.LicenceUrl is { } url - ? $"The published data is {licence.LicenceName} ({licence.LicenceId}), {url}." - : $"The published data is {licence.LicenceName} ({licence.LicenceId})."); - text.AppendLine($"Attribute it to {licence.Attribution}."); + ? $"The published data is {licence.LicenceName} ({licence.LicenceId}), {url}. " + + $"Attribute it to {licence.Attribution}." + : $"The published data is {licence.LicenceName} ({licence.LicenceId}). " + + $"Attribute it to {licence.Attribution}."); + text.AppendLine(); + + text.AppendLine("## Data"); + text.AppendLine(); + Link(text, "OpenAPI contract", ApiRoutes.OpenApi, "every endpoint, machine-readable"); + Link(text, "API guide", ApiRoutes.Documentation, "what each endpoint is for, and the limits on it"); + Link(text, "Games", ApiRoutes.Games, "the catalogue, with the same facets the listing page offers"); + Link(text, "Bulk export", ApiRoutes.Dump, "the whole catalogue as one JSON document"); + Link(text, "Bulk export, by line", ApiRoutes.DumpLines, "the same, one game per line, for a loader"); + Link(text, "Change feeds", ApiRoutes.Feeds, "games newly discovered, gone dark and come back, as JSON"); + text.AppendLine(); + + text.AppendLine("## Pages"); + text.AppendLine(); + Plain(text, "Games", "/games", "every game we have reached, faceted on what we measured"); + Plain(text, "Find a game", "/find", "a short set of questions that narrows the listing"); + Plain(text, "Rankings", "/rankings", "busiest and most reachable, computed from measurements"); + Plain(text, "Ecosystem", "/ecosystem", "codebase share and protocol adoption, as shares never totals"); + Plain(text, "Reference", "/reference", "written pages on the codebases, clients and protocols"); + Plain(text, "Archive", "/archive", "games that went dark, still probed, nothing deleted"); + Plain(text, "Crawler", "/crawler", "what the crawler did, and what it is due to do"); + Plain(text, "About", "/about", "how the catalogue is built, and how an operator opts out"); + text.AppendLine(); + + text.AppendLine("## Optional"); + text.AppendLine(); + // The feeds' own titles and descriptions, so this cannot describe one differently. + foreach (var register in Enum.GetValues()) + { + Link(text, RssFeed.Title(register) + " (RSS)", RssFeed.Path(register), RssFeed.Description(register)); + } + + Plain(text, "Submit a game", "/submit", "how an address is proposed, and what has to be true before it is listed"); + + foreach (var document in ReferenceLibrary.Shipped.Documents) + { + Plain(text, document.Title, document.Path, document.Summary); + } return text.ToString(); + // TextMirror is the one list of pages that have a text rendering, so a page without one is + // linked as itself rather than at an address that would serve the same markup. + void Plain(StringBuilder to, string name, string path, string note) => + Link(to, name, TextMirror.Offers(path) ? path + "?plain=1" : path, note); + void Link(StringBuilder to, string name, string path, string note) => to.AppendLine(CultureInfo.InvariantCulture, - $"- [{name}]({SiteUrls.Absolute(http, path)}): {note}"); + $"- [{Label(name)}]({SiteUrls.Absolute(http, path)}): {OneLine(note)}"); } + /// A link label that cannot close itself early. + private static string Label(string name) => + name.Replace("[", "\\[", StringComparison.Ordinal).Replace("]", "\\]", StringComparison.Ordinal); + + /// A note folded to one line, since a line break ends the list item that holds it. + private static string OneLine(string note) => + string.Join(' ', note.Split((char[])['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); + private static string Sitemap( HttpContext http, IReadOnlyList games, IReadOnlyList categories) { diff --git a/tests/MUI.Web.Tests/SiteIndexTests.cs b/tests/MUI.Web.Tests/SiteIndexTests.cs index 2247021..d35e25e 100644 --- a/tests/MUI.Web.Tests/SiteIndexTests.cs +++ b/tests/MUI.Web.Tests/SiteIndexTests.cs @@ -263,6 +263,72 @@ public async Task LlmsTxtConfessesOverTheFixtureAndDoesNotWhenMeasured() await Assert.That(await measured.Client.GetStringAsync("/llms.txt")).DoesNotContain("DEMO DATA"); } + [Test] + [Arguments(false)] + [Arguments(true)] + public async Task LlmsTxtHasTheShapeTheConventionDefines(bool measured) + { + // llmstxt.org: one H1 first, then one blockquote, then prose with no headings, then H2 + // sections holding nothing but "- [name](url): note" lines, "Optional" last. A parser for + // the format reads every H2 as a file list, so a rule written under one is dropped. + await using var site = await SiteHost.StartAsync(measured: measured); + + // ReplaceLineEndings: AppendLine writes the host's newline, so Windows serves CRLF. + var lines = (await site.Client.GetStringAsync("/llms.txt")).ReplaceLineEndings("\n").Split('\n'); + + await Assert.That(lines[0]).IsEqualTo("# mu*index"); + await Assert.That(lines.Count(l => l.StartsWith("# ", StringComparison.Ordinal))).IsEqualTo(1); + await Assert.That(lines.Any(l => l.StartsWith("### ", StringComparison.Ordinal))).IsFalse(); + + // The blockquote is the first thing after the title, and there is only one. + await Assert.That(lines.Skip(1).SkipWhile(string.IsNullOrEmpty).First()).StartsWith(">"); + var quoted = lines.Select((l, i) => (l, i)).Where(x => x.l.StartsWith('>')).Select(x => x.i).ToArray(); + await Assert.That(quoted[^1] - quoted[0] + 1).IsEqualTo(quoted.Length); + + var sections = lines + .Select((l, i) => (l, i)) + .Where(x => x.l.StartsWith("## ", StringComparison.Ordinal)) + .ToArray(); + await Assert.That(string.Join('|', sections.Select(s => s.l))).IsEqualTo("## Data|## Pages|## Optional"); + + var item = new System.Text.RegularExpressions.Regex(@"^- \[[^\]]+\]\((https?://[^)\s]+)\): \S"); + foreach (var line in lines.Skip(sections[0].i + 1).Where(l => l.Length > 0 && !l.StartsWith("## ", StringComparison.Ordinal))) + { + await Assert.That(item.IsMatch(line)).IsTrue().Because(line); + } + } + + [Test] + public async Task EveryAddressLlmsTxtNamesIsServed() + { + await using var site = await SiteHost.StartAsync(measured: true); + + var text = await site.Client.GetStringAsync("/llms.txt"); + var urls = System.Text.RegularExpressions.Regex.Matches(text, @"\]\((https?://[^)\s]+)\)") + .Select(m => new Uri(m.Groups[1].Value).PathAndQuery) + .ToArray(); + + await Assert.That(urls.Length).IsGreaterThan(10); + foreach (var url in urls) + { + using var response = await site.Client.GetAsync(url); + await Assert.That((int)response.StatusCode).IsEqualTo(200).Because(url); + } + } + + [Test] + public async Task LlmsTxtListsEveryShippedReferencePage() + { + await using var site = await SiteHost.StartAsync(); + + var text = await site.Client.GetStringAsync("/llms.txt"); + + foreach (var document in MUI.Web.Reference.ReferenceLibrary.Shipped.Documents) + { + await Assert.That(text).Contains(document.Path + "?plain=1"); + } + } + [Test] public async Task LlmsTxtIsServedAsText() {