Skip to content

Commit

Permalink
feat(content): add slug to metadata based on filename (#248)
Browse files Browse the repository at this point in the history
Closes #247
  • Loading branch information
d-koppenhagen committed Feb 9, 2023
1 parent 9d5e244 commit bf8581a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apps/blog-app/src/app/routes/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const routeMeta: RouteMeta = {
imports: [RouterOutlet, RouterLink, NgFor],
template: `
<ng-container *ngFor="let post of posts">
<a [routerLink]="post.attributes.slug"> {{ post.attributes.title }}</a> |
<a [routerLink]="post.slug"> {{ post.attributes.title }}</a> |
</ng-container>
<a routerLink="/about">About</a> |
<a routerLink="/contact">Contact</a>
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-app/docs/features/routing/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export interface PostAttributes {
template: `
<ul *ngFor="let post of posts">
<li>
<a [routerLink]="['/blog', 'posts', post.attributes.slug]">
<a [routerLink]="['/blog', 'posts', post.slug]">
{{ post.attributes.title }}</a
>
</li>
Expand Down
1 change: 1 addition & 0 deletions packages/content/src/lib/content-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface ContentFile<
Attributes extends Record<string, any> = Record<string, any>
> {
filename: string;
slug: string;
content?: string;
attributes: Attributes;
}
25 changes: 25 additions & 0 deletions packages/content/src/lib/content-files-list-token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ vi.mock('./get-content-files', () => {
return {
getContentFilesList: () => ({
'/test.md': { title: 'Test' },
'/path/to/test.md': { title: 'Test' },
'\\path\\to\\test.md': { title: 'Test' },
'/path/to/test with spaces.md': { title: 'Test' },
}),
};
});
Expand All @@ -14,9 +17,31 @@ describe('CONTENT_FILES_LIST_TOKEN', () => {
const firstParsedFile = CONTENT_FILES_LIST_TOKEN[0];
expect(CONTENT_FILES_LIST_TOKEN).toBeTruthy();
expect(firstParsedFile.filename).toEqual('/test.md');
expect(firstParsedFile.slug).toEqual('test');
expect(firstParsedFile.attributes['title']).toEqual('Test');
});

it('should extract the slug from nested path', () => {
const { CONTENT_FILES_LIST_TOKEN } = setup();
const firstParsedFile = CONTENT_FILES_LIST_TOKEN[1];
expect(CONTENT_FILES_LIST_TOKEN).toBeTruthy();
expect(firstParsedFile.slug).toEqual('test');
});

it('should extract the slug from nested path (windows)', () => {
const { CONTENT_FILES_LIST_TOKEN } = setup();
const firstParsedFile = CONTENT_FILES_LIST_TOKEN[2];
expect(CONTENT_FILES_LIST_TOKEN).toBeTruthy();
expect(firstParsedFile.slug).toEqual('test');
});

it('should extract the slug without spaces', () => {
const { CONTENT_FILES_LIST_TOKEN } = setup();
const firstParsedFile = CONTENT_FILES_LIST_TOKEN[3];
expect(CONTENT_FILES_LIST_TOKEN).toBeTruthy();
expect(firstParsedFile.slug).toEqual('test%20with%20spaces');
});

function setup() {
TestBed.configureTestingModule({});
return {
Expand Down
6 changes: 6 additions & 0 deletions packages/content/src/lib/content-files-list-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { InjectionToken } from '@angular/core';
import { ContentFile } from './content-file';
import { getContentFilesList } from './get-content-files';

function getSlug(filename: string) {
const parts = filename.match(/^(\\|\/)(.+(\\|\/))*(.+)\.(.+)$/);
return parts?.length ? parts[4] : '';
}

export const CONTENT_FILES_LIST_TOKEN = new InjectionToken<ContentFile[]>(
'@analogjs/content Content Files List',
{
Expand All @@ -16,6 +21,7 @@ export const CONTENT_FILES_LIST_TOKEN = new InjectionToken<ContentFile[]>(
return {
filename,
attributes,
slug: encodeURI(getSlug(filename)),
};
});
},
Expand Down
2 changes: 2 additions & 0 deletions packages/content/src/lib/content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Test Content`),
expect(c.content).toMatch('Test Content');
expect(c.attributes).toEqual({ slug: 'test' });
expect(c.filename).toEqual('/src/content/test.md');
expect(c.slug).toEqual('test');
});
flushMicrotasks();
}));
Expand Down Expand Up @@ -90,6 +91,7 @@ Test Content`),
expect(c.content).toMatch('Test Content');
expect(c.attributes).toEqual({ slug: 'custom-slug-test' });
expect(c.filename).toEqual('/src/content/custom-slug-test.md');
expect(c.slug).toEqual('custom-slug-test');
});
flushMicrotasks();
}));
Expand Down
2 changes: 2 additions & 0 deletions packages/content/src/lib/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function injectContent<
return of({
attributes: {},
filename,
slug: slug || '',
content: fallback,
});
}
Expand All @@ -56,6 +57,7 @@ export function injectContent<

return {
filename,
slug: slug || '',
attributes,
content,
};
Expand Down
1 change: 1 addition & 0 deletions packages/content/src/lib/inject-content-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('injectContentFiles', () => {
const contentFiles: ContentFile<TestAttributes>[] = [
{
filename: '/src/content/test.md',
slug: 'test',
attributes: {
testAttribute1: 'hello world',
testAttribute2: 2,
Expand Down

0 comments on commit bf8581a

Please sign in to comment.