Skip to content

Commit

Permalink
feat: add filterFn argument to the injectContentFiles function (#348)
Browse files Browse the repository at this point in the history
Closes #347
  • Loading branch information
gergobergo committed Apr 18, 2023
1 parent aeea815 commit 018b70d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
6 changes: 4 additions & 2 deletions apps/docs-app/docs/features/routing/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Hello World

## Using the Content Files List

To get a list using the list of content files in the `src/content` folder, use the `injectContentFiles()` function from the `@analogjs/content` package in your component.
To get a list using the list of content files in the `src/content` folder, use the `injectContentFiles<Attributes>(filterFn?: InjectContentFilesFilterFunction<Attributes>)` function from the `@analogjs/content` package in your component. To narrow the files, you can use the `filterFn` predicate function as an argument. You can use the `InjectContentFilesFilterFunction<T>` type to set up your predicate.

```ts
import { Component } from '@angular/core';
Expand Down Expand Up @@ -122,7 +122,9 @@ export interface PostAttributes {
`,
})
export default class BlogComponent {
readonly posts = injectContentFiles<PostAttributes>();
private readonly contentFilterFn: InjectContentFilesFilterFunction<PostAttributes> =
(contentFile) => !!contentFile.filename.includes('/src/content/blog/');
readonly posts = injectContentFiles<PostAttributes>(contentFilterFn);
}
```

Expand Down
55 changes: 52 additions & 3 deletions packages/content/src/lib/inject-content-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { TestBed } from '@angular/core/testing';
import { expect } from 'vitest';
import { CONTENT_FILES_LIST_TOKEN } from './content-files-list-token';
import { ContentFile } from './content-file';
import { injectContentFiles } from './inject-content-files';
import {
injectContentFiles,
InjectContentFilesFilterFunction,
} from './inject-content-files';

describe('injectContentFiles', () => {
it('should provide empty files if no files provided', () => {
Expand All @@ -27,16 +30,62 @@ describe('injectContentFiles', () => {
const injectedFiles = injectContentFiles();
expect(injectedFiles).toEqual(contentFiles);
});
it('should filter the result based on the provided filter function', () => {
type TestAttributes = { testAttribute1: string; testAttribute2: number };
const contentFiles: ContentFile<TestAttributes>[] = [
{
filename: '/src/content/test.md',
slug: 'test',
attributes: {
testAttribute1: 'hello world',
testAttribute2: 2,
},
content: 'I am the content',
},
{
filename: '/src/content/projects/test.md',
slug: 'test-projects',
attributes: {
testAttribute1: 'hello world',
testAttribute2: 2,
},
content: 'I am the project',
},
{
filename: '/src/content/blog/test.md',
slug: 'test-blog',
attributes: {
testAttribute1: 'hello world',
testAttribute2: 2,
},
content: 'I am the blog post',
},
];

const contentFilterFn: InjectContentFilesFilterFunction<TestAttributes> = (
contentFile
) => !!contentFile.filename.includes('/src/content/blog/');
const { injectContentFiles } = setup<TestAttributes>(
contentFiles,
contentFilterFn
);
const injectedFiles = injectContentFiles();
const expectedContentFiles = [contentFiles[2]];
expect(injectedFiles).toEqual(expectedContentFiles);
});

function setup<Attributes extends Record<string, any>>(
contentFiles: ContentFile[] = []
contentFiles: ContentFile[] = [],
filterFn?: InjectContentFilesFilterFunction<Attributes>
) {
TestBed.overrideProvider(CONTENT_FILES_LIST_TOKEN, {
useValue: contentFiles,
});
return {
injectContentFiles: () =>
TestBed.runInInjectionContext(() => injectContentFiles<Attributes>()),
TestBed.runInInjectionContext(() =>
injectContentFiles<Attributes>(filterFn)
),
};
}
});
23 changes: 19 additions & 4 deletions packages/content/src/lib/inject-content-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ import { ContentFile } from './content-file';
import { inject } from '@angular/core';
import { CONTENT_FILES_LIST_TOKEN } from './content-files-list-token';

export function injectContentFiles<
Attributes extends Record<string, any>
>(): ContentFile<Attributes>[] {
return inject(CONTENT_FILES_LIST_TOKEN) as ContentFile<Attributes>[];
export function injectContentFiles<Attributes extends Record<string, any>>(
filterFn?: InjectContentFilesFilterFunction<Attributes>
): ContentFile<Attributes>[] {
const allContentFiles = inject(
CONTENT_FILES_LIST_TOKEN
) as ContentFile<Attributes>[];

if (filterFn) {
const filteredContentFiles = allContentFiles.filter(filterFn);

return filteredContentFiles;
}
return allContentFiles;
}

export type InjectContentFilesFilterFunction<T extends Record<string, any>> = (
value: ContentFile<T>,
index: number,
array: ContentFile<T>[]
) => boolean;

0 comments on commit 018b70d

Please sign in to comment.