Latest post as index page? #200
-
I would like my most recent post to also be available as the main index page (e.g. foo.com/). Is there a simple way to pretty much just copy the right file over after it's been rendered? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Sure! Statiq is one of those tools that can do anything, but it sometimes takes a bit of background and experience to figure stuff out quickly. In this case, the approach that came to me only took a few minutes to write, but it would certainly have been tricky to figure out on your own. This relies on using the CleanBlog theme (or any other theme that has an archive of posts as the default index file): using Microsoft.Extensions.DependencyInjection;
using Statiq.App;
using Statiq.Web;
using Statiq.Web.Modules;
await Bootstrapper.Factory
.CreateWeb(args)
// Prevent the original archives index page from being output
.ModifyPipeline(
nameof(Statiq.Web.Pipelines.Archives),
pipeline => pipeline.OutputModules.Insert(
0, // Insert new modules at the beginning of the output phase
new FilterDocuments(Config.FromDocument(doc => doc.Source == "index.cshtml")), // Filter to the index file
new SetDestination(NormalizedPath.Null))) // Set the destination to null to prevent outputting the file
// Create a new new pipeline that gets the latest post document (using the original index archive), renders it, then outputs it as the new index
.BuildPipeline(
"LatestPost",
builder => builder
.WithDependencies(nameof(Statiq.Web.Pipelines.Archives)) // Mark the archives pipeline as a dependency (the index file is an archive)
.WithPostProcessModules(
new ReplaceDocuments(nameof(Statiq.Web.Pipelines.Archives)), // Replace the documents in this pipeline with the archives documents
new FilterDocuments(Config.FromDocument(doc => doc.Destination == "index.html")), // Filter to the index file
new ExecuteConfig(Config.FromDocument(doc => doc.GetChildren().First())), // The posts are children of the index file, get the first
new RenderContentPostProcessTemplates(builder.Services.GetService<Templates>()), // Render it as content (I.e. Razor)
new SetDestination("index.html")) // Set it's destination to the root index file
.WithOutputWriteFiles()) // Make sure to write out this file as the index file
.RunAsync(); Basically this tells Statiq not to output the original index file, but to instead to get the first post from its embedded list of posts and render then output that as the index file instead. It uses a new "LatestPost" pipeline to do that last part (though you could call the new pipeline anything). There's certainly some other ways this could have been done, for example another approach that came to mind was to write a new Let me know if this works for you! |
Beta Was this translation helpful? Give feedback.
-
That worked great, thanks! For my particular application, I had moved the all-posts archive to a different location so I just skipped the Thanks again for your help. |
Beta Was this translation helpful? Give feedback.
Sure! Statiq is one of those tools that can do anything, but it sometimes takes a bit of background and experience to figure stuff out quickly. In this case, the approach that came to me only took a few minutes to write, but it would certainly have been tricky to figure out on your own.
This relies on using the CleanBlog theme (or any other theme that has an archive of posts as the default index file):