Skip to content

Commit 4f81f27

Browse files
committed
fix(tree): fs path for prefixed source
1 parent a14720c commit 4f81f27

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/module/src/runtime/utils/collection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,21 @@ export function getCollectionSource(id: string, collection: CollectionInfo) {
7272
return matchedSource
7373
}
7474

75-
export function getFsPath(id: string, source: CollectionInfo['source'][0]) {
75+
export function generateFsPathFromId(id: string, source: CollectionInfo['source'][0]) {
7676
const [_, ...rest] = id.split(/[/:]/)
7777
const path = rest.join('/')
7878

7979
const { fixed } = parseSourceBase(source)
8080

81-
return join(fixed, path)
81+
const pathWithoutFixed = path.substring(fixed.length)
82+
return join(fixed, pathWithoutFixed)
8283
}
8384

8485
export function getCollectionInfo(id: string, collections: Record<string, CollectionInfo>) {
8586
const collection = getCollection(id.split(/[/:]/)[0]!, collections)
8687
const source = getCollectionSource(id, collection)
8788

88-
const fsPath = getFsPath(id, source!)
89+
const fsPath = generateFsPathFromId(id, source!)
8990

9091
return {
9192
collection,

src/module/test/utils/collection.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest'
2-
import { getCollectionByFilePath, generateIdFromFsPath } from '../../src/runtime/utils/collection'
2+
import { getCollectionByFilePath, generateIdFromFsPath, generateFsPathFromId } from '../../src/runtime/utils/collection'
33
import type { CollectionInfo, ResolvedCollectionSource } from '@nuxt/content'
44
import { collections } from '../mocks/collection'
55

@@ -87,3 +87,40 @@ describe('generateIdFromFsPath', () => {
8787
// expect(result).toBe('multi-source/first/content/test.md')
8888
// })
8989
})
90+
91+
describe('generateFsPathFromId', () => {
92+
it('One file included', () => {
93+
const id = 'landing/index.md'
94+
const source = {
95+
prefix: '/',
96+
include: 'index.md',
97+
} as ResolvedCollectionSource
98+
const result = generateFsPathFromId(id, source)
99+
expect(result).toBe('index.md')
100+
})
101+
102+
it('Global pattern included', () => {
103+
const id = 'docs/1.getting-started/2.introduction.md'
104+
const source = {
105+
prefix: '/',
106+
include: '**',
107+
exclude: ['index.md'],
108+
} as ResolvedCollectionSource
109+
const result = generateFsPathFromId(id, source)
110+
expect(result).toBe('1.getting-started/2.introduction.md')
111+
})
112+
113+
it('Custom pattern with prefix', () => {
114+
const id = 'docs_en/en/1.getting-started/2.introduction.md'
115+
const source = {
116+
prefix: '/en',
117+
include: 'en/**/*',
118+
exclude: [
119+
'en/index.md',
120+
],
121+
} as ResolvedCollectionSource
122+
123+
const result = generateFsPathFromId(id, source)
124+
expect(result).toBe('en/1.getting-started/2.introduction.md')
125+
})
126+
})

0 commit comments

Comments
 (0)