diff --git a/src/Blake.BuildTools/Generator/ContentIndexBuilder.cs b/src/Blake.BuildTools/Generator/ContentIndexBuilder.cs index b35c6ce..74b3c1a 100644 --- a/src/Blake.BuildTools/Generator/ContentIndexBuilder.cs +++ b/src/Blake.BuildTools/Generator/ContentIndexBuilder.cs @@ -16,7 +16,10 @@ public static void WriteIndex(string outputPath, List allPages, bool sb.AppendLine(" public static partial List GetPages() => new()"); sb.AppendLine(" {"); - foreach (var page in allPages) + // Filter out draft pages - they should be accessible by URL but not listed in navigation + var nonDraftPages = allPages.Where(p => !p.Draft).ToList(); + + foreach (var page in nonDraftPages) { try { @@ -29,6 +32,8 @@ public static void WriteIndex(string outputPath, List allPages, bool if (page.Date.HasValue) sb.AppendLine( $" Date = new DateTime({page.Date.Value.Year}, {page.Date.Value.Month}, {page.Date.Value.Day}),"); + // Draft property is always false here since drafts are filtered out above + // We include it for consistency with PageModel structure and explicit state indication sb.AppendLine($" Draft = {page.Draft.ToString().ToLowerInvariant()},"); sb.AppendLine($" IconIdentifier = @\"{page.IconIdentifier}\","); if (page.Tags.Count > 0) diff --git a/tests/Blake.IntegrationTests/Commands/BlakeBakeCommandTests.cs b/tests/Blake.IntegrationTests/Commands/BlakeBakeCommandTests.cs index e0e091a..d9ef3a6 100644 --- a/tests/Blake.IntegrationTests/Commands/BlakeBakeCommandTests.cs +++ b/tests/Blake.IntegrationTests/Commands/BlakeBakeCommandTests.cs @@ -203,6 +203,55 @@ public async Task BlakeBake_WithIncludeDraftsFlag_IncludesDrafts() FileSystemHelper.AssertFileExists(Path.Combine(testDir, ".generated", "posts", "DraftPost.razor")); } + [Fact] + public async Task BlakeBake_WithIncludeDraftsFlag_ExcludesDraftsFromContentIndex() + { + // Arrange + var testDir = CreateTempDirectory("blake-bake-drafts-not-in-index"); + + // Create a published post + FileSystemHelper.CreateMarkdownFile( + Path.Combine(testDir, "Posts", "published-post.md"), + "Published Post", + "This post is published.", + new Dictionary { ["draft"] = false } + ); + + // Create a draft post + FileSystemHelper.CreateMarkdownFile( + Path.Combine(testDir, "Posts", "draft-post.md"), + "Draft Post", + "This post is a draft.", + new Dictionary { ["draft"] = true } + ); + + FileSystemHelper.CreateRazorTemplate( + Path.Combine(testDir, "Posts", "template.razor"), + @"@page ""/posts/{Slug}"" +

@Title

+
@Body
" + ); + + // Act + var result = await RunBlakeCommandAsync(["bake", testDir, "--includeDrafts"]); + + // Assert + Assert.Equal(0, result.ExitCode); + + // Both posts should be generated as razor files + FileSystemHelper.AssertFileExists(Path.Combine(testDir, ".generated", "posts", "PublishedPost.razor")); + FileSystemHelper.AssertFileExists(Path.Combine(testDir, ".generated", "posts", "DraftPost.razor")); + + // Content index should be created + var indexPath = Path.Combine(testDir, ".generated", "GeneratedContentIndex.cs"); + FileSystemHelper.AssertFileExists(indexPath); + + // Content index should contain published post but not draft post + var indexContent = File.ReadAllText(indexPath); + Assert.Contains("Published Post", indexContent); + Assert.DoesNotContain("Draft Post", indexContent); + } + [Fact] public async Task BlakeBake_WithTemplateInSameFolder_UsesTemplate() {