forked from analogjs/analog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content): add support for lazy loading content files (analogjs#235)
Closes analogjs#234
- Loading branch information
1 parent
c240c0c
commit 0c21771
Showing
15 changed files
with
214 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { CONTENT_FILES_LIST_TOKEN } from './content-files-list-token'; | ||
|
||
vi.mock('./get-content-files', () => { | ||
return { | ||
getContentFilesList: () => ({ | ||
'/test.md': { title: 'Test' }, | ||
}), | ||
}; | ||
}); | ||
describe('CONTENT_FILES_LIST_TOKEN', () => { | ||
it('should be the token', () => { | ||
const { CONTENT_FILES_LIST_TOKEN } = setup(); | ||
const firstParsedFile = CONTENT_FILES_LIST_TOKEN[0]; | ||
expect(CONTENT_FILES_LIST_TOKEN).toBeTruthy(); | ||
expect(firstParsedFile.filename).toEqual('/test.md'); | ||
expect(firstParsedFile.attributes['title']).toEqual('Test'); | ||
}); | ||
|
||
function setup() { | ||
TestBed.configureTestingModule({}); | ||
return { | ||
CONTENT_FILES_LIST_TOKEN: TestBed.inject(CONTENT_FILES_LIST_TOKEN), | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { ContentFile } from './content-file'; | ||
import { getContentFilesList } from './get-content-files'; | ||
|
||
export const CONTENT_FILES_LIST_TOKEN = new InjectionToken<ContentFile[]>( | ||
'@analogjs/content Content Files List', | ||
{ | ||
providedIn: 'root', | ||
factory() { | ||
const contentFiles = getContentFilesList(); | ||
|
||
return Object.keys(contentFiles).map((filename) => { | ||
const attributes = contentFiles[filename]; | ||
|
||
return { | ||
filename, | ||
attributes, | ||
}; | ||
}); | ||
}, | ||
} | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,14 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import fm from 'front-matter'; | ||
import { ContentFile } from './content-file'; | ||
import { getRawFiles } from './get-raw-files'; | ||
|
||
export const CONTENT_FILES_TOKEN = new InjectionToken<ContentFile[]>( | ||
'@analogjs/content Content Files', | ||
{ | ||
providedIn: 'root', | ||
factory() { | ||
const rawContentFiles = getRawFiles(); | ||
import { getContentFiles } from './get-content-files'; | ||
|
||
return Object.keys(rawContentFiles).map((filename) => { | ||
const { body, attributes } = fm<Record<string, any>>( | ||
rawContentFiles[filename] | ||
); | ||
export const CONTENT_FILES_TOKEN = new InjectionToken< | ||
Record<string, () => Promise<string>> | ||
>('@analogjs/content Content Files', { | ||
providedIn: 'root', | ||
factory() { | ||
const contentFiles = getContentFiles(); | ||
|
||
return { | ||
filename, | ||
content: body, | ||
attributes, | ||
}; | ||
}); | ||
}, | ||
} | ||
); | ||
return contentFiles; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Returns the list of content files by filename with ?analog-content-list=true. | ||
* We use the query param to transform the return into an array of | ||
* just front matter attributes. | ||
* | ||
* @returns | ||
*/ | ||
export const getContentFilesList = () => | ||
import.meta.glob<Record<string, any>>('/src/content/**/*.md', { | ||
eager: true, | ||
import: 'default', | ||
query: { 'analog-content-list': true }, | ||
}); | ||
|
||
/** | ||
* Returns the lazy loaded content files for lookups. | ||
* | ||
* @returns | ||
*/ | ||
export const getContentFiles = () => | ||
import.meta.glob(['/src/content/**/*.md'], { | ||
as: 'raw', | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { ContentFile } from './content-file'; | ||
import { inject } from '@angular/core'; | ||
import { CONTENT_FILES_TOKEN } from './content-files-token'; | ||
import { CONTENT_FILES_LIST_TOKEN } from './content-files-list-token'; | ||
|
||
export function injectContentFiles< | ||
Attributes extends Record<string, any> | ||
>(): ContentFile<Attributes>[] { | ||
return inject(CONTENT_FILES_TOKEN) as ContentFile<Attributes>[]; | ||
return inject(CONTENT_FILES_LIST_TOKEN) as ContentFile<Attributes>[]; | ||
} |
Oops, something went wrong.