Repository navigation
feat(seo): add per-page descriptions and canonical metadata #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BenjaminMichaelis
merged 3 commits into
main
from
benjaminmichaelis/per-page-meta-descriptions
Oct 17, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
EssentialCSharp.Web.Tests/ChapterDescriptionServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using EssentialCSharp.Web.Services; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Moq; | ||
|
|
||
| namespace EssentialCSharp.Web.Tests; | ||
|
|
||
| public class ChapterDescriptionServiceTests | ||
| { | ||
| [Test] | ||
| public async Task GeneratedDescriptionsOverrideHeadingFallback() | ||
| { | ||
| string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
| Directory.CreateDirectory(Path.Combine(directory, "Chapters")); | ||
| try | ||
| { | ||
| File.WriteAllText(Path.Combine(directory, "Chapters", "sitemap.descriptions.json"), | ||
| """[{"Keys":["example-page","alias"],"Value":"A tailored chapter description."}]"""); | ||
| Mock<IWebHostEnvironment> environment = new(); | ||
| environment.SetupGet(e => e.ContentRootPath).Returns(directory); | ||
| ChapterDescriptionService service = new(environment.Object); | ||
| SiteMapping mapping = CreateMapping("alias", "Examples"); | ||
|
|
||
| await Assert.That(service.GetDescription(mapping)).IsEqualTo("A tailored chapter description."); | ||
| await Assert.That(service.GetDescription(CreateMapping("missing", "Other Examples"))).Contains("Other Examples"); | ||
| } | ||
| finally | ||
| { | ||
| Directory.Delete(directory, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task WithoutGeneratedDescriptionsUsesPageHeading() | ||
| { | ||
| Mock<IWebHostEnvironment> environment = new(); | ||
| environment.SetupGet(e => e.ContentRootPath).Returns(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); | ||
| ChapterDescriptionService service = new(environment.Object); | ||
|
|
||
| await Assert.That(service.GetDescription(CreateMapping("sample", "Sample Heading"))).Contains("Sample Heading"); | ||
| } | ||
|
|
||
| private static SiteMapping CreateMapping(string key, string heading) => | ||
| new([key], key, ["Chapters", "01", "Pages", "01.html"], 1, 1, 1, | ||
| "Chapter One", heading, null, 1); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using System.Text.Json; | ||
|
|
||
| namespace EssentialCSharp.Web.Services; | ||
|
|
||
| public sealed class ChapterDescriptionService | ||
| { | ||
| private readonly Dictionary<string, string> _Descriptions; | ||
|
|
||
| public ChapterDescriptionService(IWebHostEnvironment environment) | ||
| { | ||
| string path = Path.Combine(environment.ContentRootPath, "Chapters", "sitemap.descriptions.json"); | ||
| if (!File.Exists(path)) | ||
| { | ||
| _Descriptions = new Dictionary<string, string>(); | ||
| return; | ||
| } | ||
|
|
||
| using FileStream stream = File.OpenRead(path); | ||
| List<ChapterDescription> entries = JsonSerializer.Deserialize<List<ChapterDescription>>(stream) | ||
| ?? throw new InvalidDataException($"No chapter descriptions found in {path}"); | ||
| Dictionary<string, string> descriptions = new(StringComparer.OrdinalIgnoreCase); | ||
| foreach (ChapterDescription entry in entries) | ||
| { | ||
| foreach (string key in entry.Keys) | ||
| { | ||
| descriptions.Add(key, entry.Value); | ||
| } | ||
| } | ||
| _Descriptions = descriptions; | ||
| } | ||
|
|
||
| public string GetDescription(SiteMapping mapping) | ||
| { | ||
| foreach (string key in mapping.Keys) | ||
| { | ||
| if (_Descriptions.TryGetValue(key, out string? description) && !string.IsNullOrWhiteSpace(description)) | ||
| { | ||
| return description; | ||
| } | ||
| } | ||
|
|
||
| string heading = mapping.IndentLevel == 0 ? mapping.ChapterTitle : mapping.RawHeading; | ||
| return $"Learn about {heading} in Essential C# with practical examples and explanations from the online C# guide."; | ||
| } | ||
|
|
||
| private sealed record ChapterDescription(List<string> Keys, string Value); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.