I am generating pages for a Content Collection using their filenames and am trying to figure out the best way to pass the desired URL for link rendering on other pages.
example article at src/content/articles/1970-01-01-example_article.md being rendered via src/pages/[year]/[month]/[day]/[article].astro:
---
import { PageLayout } from "salt-theme:layouts";
import { getCollection, render } from "astro:content";
export async function getStaticPaths() {
const articles = await getCollection("articles");
return articles.map((article) => ({
params: {
year: article.id.slice(0,4),
month: article.id.slice(5,7),
day: article.id.slice(8,10),
article: article.id.slice(11),
},
props: {
article,
},
}));
}
const { article } = Astro.props;
const { Content } = await render(article);
---
<PageLayout>
<Content />
</PageLayout>
example of listing:
---
import { getCollection } from "astro:content";
const articles = await getCollection("articles");
---
<ul>
{articles.map((post) =>
<li><a href={post.data.slug}>{post.data.title}</a></li>
<li><a href={`${post.id.slice(0,4)}/${post.id.slice(5,7)}/${post.id.slice(8,10)}/${post.id.slice(11)}`}>{post.data.title}</a></li>
)}
</ul>
The first listing item is the sort of thing I'd like to use, because while the second one works, it is pretty clunky.
I'd assume there is a way to inject a new slug value via the renderer props but am not quite sure how.
I've also explored the API reference and was wondering if there is perhaps a way to query routePattern or get a context object within the Collect mapping?
All suggestions and directions appreciated!