-
-
Notifications
You must be signed in to change notification settings - Fork 258
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
Feature: add injectable token/service that provides list of content w/frontmatter #222
Comments
What do you think about having a more flexible result, so attributes type can be provided based on the project needs? This will also provide the ability to set proper typing for plain markdown content without frontmatter. export interface ContentFile<
Attributes extends Record<string, any> = Record<string, any>
> {
filename: string;
content: string;
attributes: Attributes;
}
const CONTENT_FILES_TOKEN = new InjectionToken<ContentFile[]>(
'@analogjs/content Content Files',
{
providedIn: 'root',
factory() {
const rawContentFiles = import.meta.glob('/src/content/**/*.md', {
eager: true,
as: 'raw',
});
return Object.keys(rawContentFiles).map((filename) => {
const { body, attributes } = fm<Record<string, any>>(
rawContentFiles[filename]
);
return {
filename,
content: body,
attributes,
};
});
},
}
);
export function injectContentFiles<
Attributes extends Record<string, any>
>(): ContentFile<T>[] {
return inject(CONTENT_FILES_TOKEN) as ContentFile<Attributes>[];
}
// usage:
interface PostAttributes {
title: string;
coverSrc: string;
published: boolean;
}
@Component({
selector: 'app-blog',
standalone: true,
imports: [NgFor],
template: `
<article *ngFor="let post of publishedPosts">
<h2>{{ post.attributes.title }}</h2>
<img [src]="post.attributes.coverSrc" [alt]="post.attributes.title" />
</article>
`,
})
export default class BlogComponent {
private readonly posts = injectContentFiles<PostAttributes>();
readonly publishedPosts = this.posts.filter(
(post) => post.attributes.published
);
} Changes:
|
Definitely prefer the more flexible option with better types. |
@markostanimirovic @brandonroberts I can implement as laid out here: #222 (comment) |
…ate routes in blog-app The new function is used to inject the content files parsed with front-matter to the blog-app's blog page. Then, the attributes of each file are used to dynamically create links to them. Closes analogjs#222
…tent w/frontmatter (analogjs#225) Closes analogjs#222
Which scope/s are relevant/related to the feature request?
content
Information
Define an injectable token or service that provides a list of the files markdown files scanned from the
src/content
folder in an array that includes frontmatter.Example structure below
Usage
This could be used to list blog posts for example and could be filtered further based on needs, such as only listing
published
posts as an example.Describe any alternatives/workarounds you're currently using
No response
I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: