-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: updated loaders #14594
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
base: next
Are you sure you want to change the base?
feat: updated loaders #14594
Changes from 14 commits
c227582
e79202f
17d6570
2b4e38e
e350966
75d6661
532143b
68a810f
cec45ce
3d36f09
05d1954
f03e5f2
ad5a7f6
6bac68f
cb08d45
c8567da
771939a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||
| import { existsSync, promises as fs } from 'node:fs'; | ||||
| import { createMarkdownProcessor, type MarkdownProcessor } from '@astrojs/markdown-remark'; | ||||
| import { Traverse } from 'neotraverse/modern'; | ||||
| import PQueue from 'p-queue'; | ||||
| import type { FSWatcher } from 'vite'; | ||||
| import xxhash from 'xxhash-wasm'; | ||||
|
|
@@ -17,12 +18,13 @@ import { | |||
| } from './consts.js'; | ||||
| import type { RenderedContent } from './data-store.js'; | ||||
| import type { LoaderContext } from './loaders/types.js'; | ||||
| import type { MutableDataStore } from './mutable-data-store.js'; | ||||
| import { InMemoryDataStore, type MutableDataStore } from './mutable-data-store.js'; | ||||
| import { | ||||
| type ContentObservable, | ||||
| getEntryConfigByExtMap, | ||||
| getEntryDataAndImages, | ||||
| getEntryData, | ||||
| globalContentConfigObserver, | ||||
| isReference, | ||||
| loaderReturnSchema, | ||||
| safeStringify, | ||||
| } from './utils.js'; | ||||
|
|
@@ -114,21 +116,19 @@ class ContentLayer { | |||
| async #getLoaderContext({ | ||||
| collectionName, | ||||
| loaderName = 'content', | ||||
| parseData, | ||||
| refreshContextData, | ||||
| }: { | ||||
| collectionName: string; | ||||
| loaderName: string; | ||||
| parseData: LoaderContext['parseData']; | ||||
| refreshContextData?: Record<string, unknown>; | ||||
| }): Promise<LoaderContext> { | ||||
| }): Promise<Omit<LoaderContext, 'store'>> { | ||||
| return { | ||||
| collection: collectionName, | ||||
| store: this.#store.scopedStore(collectionName), | ||||
| meta: this.#store.metaStore(collectionName), | ||||
| logger: this.#logger.forkIntegrationLogger(loaderName), | ||||
| config: this.#settings.config, | ||||
| parseData, | ||||
| // TODO: remove in v7 | ||||
| parseData: async (props) => props.data, | ||||
| renderMarkdown: this.#processMarkdown.bind(this), | ||||
| generateDigest: await this.#getGenerateDigest(), | ||||
| watcher: this.#watcher, | ||||
|
|
@@ -243,7 +243,7 @@ class ContentLayer { | |||
| this.#watcher?.removeAllTrackedListeners(); | ||||
| } | ||||
|
|
||||
| await Promise.all( | ||||
| const rawLoaderResults = await Promise.all( | ||||
| Object.entries(contentConfig.config.collections).map(async ([name, collection]) => { | ||||
| if (collection.type !== CONTENT_LAYER_TYPE) { | ||||
| return; | ||||
|
|
@@ -267,44 +267,121 @@ class ContentLayer { | |||
| return; | ||||
| } | ||||
|
|
||||
| const collectionWithResolvedSchema = { ...collection, schema }; | ||||
|
|
||||
| const parseData: LoaderContext['parseData'] = async ({ id, data, filePath = '' }) => { | ||||
| const { data: parsedData } = await getEntryDataAndImages( | ||||
| { | ||||
| id, | ||||
| collection: name, | ||||
| unvalidatedData: data, | ||||
| _internal: { | ||||
| rawData: undefined, | ||||
| filePath, | ||||
| }, | ||||
| }, | ||||
| collectionWithResolvedSchema, | ||||
| false, | ||||
| ); | ||||
|
|
||||
| return parsedData; | ||||
| }; | ||||
|
|
||||
| const context = await this.#getLoaderContext({ | ||||
| collectionName: name, | ||||
| parseData, | ||||
| loaderName: collection.loader.name, | ||||
| refreshContextData: options?.context, | ||||
| }); | ||||
|
|
||||
| const realStore = this.#store.scopedStore(name); | ||||
|
|
||||
| if (typeof collection.loader === 'function') { | ||||
| return simpleLoader(collection.loader as CollectionLoader<{ id: string }>, context); | ||||
| await simpleLoader(collection.loader as CollectionLoader<{ id: string }>, { | ||||
| ...context, | ||||
| store: realStore, | ||||
| }); | ||||
| return; | ||||
| } | ||||
|
|
||||
| if (!collection.loader.load) { | ||||
| throw new Error(`Collection loader for ${name} does not have a load method`); | ||||
| } | ||||
|
|
||||
| return collection.loader.load(context); | ||||
| const memoryStore = new InMemoryDataStore(new Map(realStore.entries())); | ||||
|
|
||||
| const result = collection.loader.load({ | ||||
| ...context, | ||||
| store: memoryStore, | ||||
| }); | ||||
|
|
||||
| if (!schema && result?.schema) { | ||||
| schema = result.schema; | ||||
| } | ||||
|
|
||||
| return { | ||||
| schema, | ||||
| types: result?.types, | ||||
| realStore, | ||||
| memoryStore, | ||||
| name, | ||||
| collection, | ||||
| }; | ||||
| }), | ||||
| ); | ||||
|
|
||||
| const filteredRawLoaderResults = rawLoaderResults.filter((result) => !!result); | ||||
|
|
||||
| await Promise.all( | ||||
| filteredRawLoaderResults.map(async ({ memoryStore, realStore, schema, name, collection }) => { | ||||
| for (const event of memoryStore.events) { | ||||
| switch (event.type) { | ||||
| case 'set': { | ||||
| let data = event.entry.data; | ||||
| if (schema) { | ||||
| data = await getEntryData( | ||||
| { | ||||
| id: event.entry.id, | ||||
| collection: name, | ||||
| unvalidatedData: data, | ||||
| _internal: { | ||||
| rawData: undefined, | ||||
| filePath: event.entry.filePath!, | ||||
| }, | ||||
| }, | ||||
| { | ||||
| ...collection, | ||||
| schema, | ||||
| }, | ||||
| false, | ||||
| ); | ||||
|
|
||||
| new Traverse(data).forEach((ctx, value) => { | ||||
| if (!isReference(value)) { | ||||
| return; | ||||
| } | ||||
| // TODO: better data structure | ||||
| const collectionStore = filteredRawLoaderResults.find( | ||||
| (e) => e.name === value.collection, | ||||
| )!.memoryStore; | ||||
| const id = 'id' in value ? value.id : value.slug; | ||||
| if (!collectionStore.has(id)) { | ||||
| // TODO: AstroError | ||||
| throw new Error( | ||||
| `Invalid reference for ${name}.${ctx.keys!.map((key) => key.toString()).join('.')}: cannot find entry ${id}`, | ||||
| ); | ||||
| } | ||||
| }); | ||||
|
||||
| const entry = store.get(collection, lookup); |
Uh oh!
There was an error while loading. Please reload this page.