Skip to content

Commit 06ba5d4

Browse files
committed
return fsPath when list files
1 parent 3eeabf3 commit 06ba5d4

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/module/src/runtime/host.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ref } from 'vue'
22
import { ensure } from './utils/ensure'
3-
import type { CollectionItemBase, CollectionSource, DatabaseAdapter } from '@nuxt/content'
3+
import type { CollectionInfo, CollectionItemBase, CollectionSource, DatabaseAdapter } from '@nuxt/content'
44
import type { ContentDatabaseAdapter } from '../types/content'
55
import { getCollectionByFilePath, generateIdFromFsPath, generateRecordDeletion, generateRecordInsert, generateFsPathFromId, getCollectionById } from './utils/collection'
66
import { createCollectionDocument, normalizeDocument } from './utils/document'
@@ -89,7 +89,7 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
8989
return localDatabaseAdapter!(collection)
9090
}
9191

92-
function useContentCollections() {
92+
function useContentCollections(): Record<string, CollectionInfo> {
9393
return Object.fromEntries(
9494
Object.entries(useContent().collections).filter(([, collection]) => {
9595
if (!collection.source.length || collection.source.some((source: CollectionSource) => source.repository || (source as unknown as { _custom: boolean })._custom)) {
@@ -197,12 +197,19 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
197197
return normalizeDocument(fsPath, item as DatabaseItem)
198198
},
199199
list: async (): Promise<DatabaseItem[]> => {
200-
const collections = Object.keys(useContentCollections()).filter(c => c !== 'info')
201-
const contents = await Promise.all(collections.map(async (collection) => {
202-
return await useContentCollectionQuery(collection).all() as DatabaseItem[]
200+
const collections = Object.values(useContentCollections()).filter(collection => collection.name !== 'info')
201+
const documentsByCollection = await Promise.all(collections.map(async (collection) => {
202+
const documents = await useContentCollectionQuery(collection.name).all() as DatabaseItem[]
203+
204+
return documents.map((document) => {
205+
const source = getCollectionSourceById(document.id, collection.source)
206+
const fsPath = generateFsPathFromId(document.id, source!)
207+
208+
return normalizeDocument(fsPath, document)
209+
})
203210
}))
204211

205-
return contents.flat()
212+
return documentsByCollection.flat()
206213
},
207214
create: async (fsPath: string, content: string) => {
208215
const collections = useContentCollections()

src/module/src/templates.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Storage } from 'unstorage'
22
import type { Nuxt } from '@nuxt/schema'
3+
import { withLeadingSlash } from 'ufo'
34

45
export async function getAssetsStorageDevTemplate(_assetsStorage: Storage, _nuxt: Nuxt) {
56
return [
@@ -19,11 +20,13 @@ export async function getAssetsStorageTemplate(assetsStorage: Storage, _nuxt: Nu
1920
'const storage = createStorage({})',
2021
'',
2122
...keys.map((key) => {
23+
const path = withLeadingSlash(key.replace(/:/g, '/'))
2224
const value = {
2325
id: `public-assets/${key.replace(/:/g, '/')}`,
2426
extension: key.split('.').pop(),
2527
stem: key.split('.').join('.'),
26-
path: '/' + key.replace(/:/g, '/'),
28+
path,
29+
fsPath: path,
2730
}
2831
return `storage.setItem('${value.id}', ${JSON.stringify(value)})`
2932
}),

0 commit comments

Comments
 (0)