@@ -5,6 +5,33 @@ import { join, dirname, parse } from 'pathe'
55import { withoutLeadingSlash , withoutTrailingSlash } from 'ufo'
66import { parseSourceBase } from './source'
77
8+ function joinSourcePath ( root : string , path : string ) {
9+ return withoutLeadingSlash ( withoutTrailingSlash ( ! root || root === '/' ? path : join ( root , path ) ) )
10+ }
11+
12+ function getSourceRoot ( source : ResolvedCollectionSource ) {
13+ const { fixed } = parseSourceBase ( source )
14+ const normalizedFixed = withoutLeadingSlash ( withoutTrailingSlash ( fixed ) )
15+ const normalizedCwd = withoutLeadingSlash ( withoutTrailingSlash ( source . cwd || '' ) )
16+
17+ let cwdRoot = ''
18+
19+ if ( normalizedCwd === 'content' || normalizedCwd . endsWith ( '/content' ) ) {
20+ cwdRoot = ''
21+ }
22+ else if ( normalizedCwd . startsWith ( 'content/' ) ) {
23+ cwdRoot = normalizedCwd . slice ( 'content/' . length )
24+ }
25+ else if ( normalizedCwd . includes ( '/content/' ) ) {
26+ cwdRoot = normalizedCwd . split ( '/content/' ) . pop ( ) || ''
27+ }
28+ else if ( normalizedCwd ) {
29+ cwdRoot = normalizedCwd
30+ }
31+
32+ return joinSourcePath ( cwdRoot , normalizedFixed )
33+ }
34+
835/**
936 * Generation methods
1037 */
@@ -13,33 +40,29 @@ export function generateStemFromFsPath(path: string) {
1340}
1441
1542export function generateIdFromFsPath ( path : string , collectionInfo : CollectionInfo ) {
16- const { fixed } = parseSourceBase ( collectionInfo . source [ 0 ] ! )
43+ const source = collectionInfo . source [ 0 ] !
44+ const sourceRoot = getSourceRoot ( source )
45+ const normalizedPath = withoutLeadingSlash ( path )
1746
18- const pathWithoutFixed = path . substring ( fixed . length )
47+ const pathWithoutFixed = sourceRoot && normalizedPath . startsWith ( sourceRoot + '/' )
48+ ? normalizedPath . substring ( sourceRoot . length + 1 )
49+ : normalizedPath
1950
20- return join ( collectionInfo . name , collectionInfo . source [ 0 ] ? .prefix || '' , pathWithoutFixed )
51+ return join ( collectionInfo . name , source . prefix || '' , pathWithoutFixed )
2152}
2253
2354export function generateFsPathFromId ( id : string , source : ResolvedCollectionSource ) {
2455 const [ _ , ...rest ] = id . split ( / [ / : ] / )
2556 let path = rest . join ( '/' )
26-
27- const { fixed } = parseSourceBase ( source )
28- const normalizedFixed = withoutTrailingSlash ( fixed )
57+ const sourceRoot = getSourceRoot ( source )
2958
3059 // If source has a prefix and the path starts with the prefix, remove the prefix
3160 const prefix = withoutTrailingSlash ( withoutLeadingSlash ( source . prefix || '' ) )
3261 if ( prefix && prefix !== '/' && path . startsWith ( prefix + '/' ) ) {
3362 path = path . substring ( prefix . length + 1 )
3463 }
3564
36- // If path already starts with the fixed part, return as is
37- if ( normalizedFixed && path . startsWith ( normalizedFixed ) ) {
38- return path
39- }
40-
41- // Otherwise, join fixed part with path
42- return join ( fixed , path )
65+ return joinSourcePath ( sourceRoot , path )
4366}
4467
4568/**
@@ -70,8 +93,9 @@ export function getCollectionByFilePath(path: string, collections: Record<string
7093 const paths = path === '/' ? [ 'index.yml' , 'index.yaml' , 'index.md' , 'index.json' ] : [ path ]
7194 return paths . some ( ( p ) => {
7295 matchedSource = collection . source . find ( ( source ) => {
73- const include = minimatch ( p , source . include , { dot : true } )
74- const exclude = source . exclude ?. some ( exclude => minimatch ( p , exclude ) )
96+ const sourceRoot = getSourceRoot ( source )
97+ const include = minimatch ( p , joinSourcePath ( sourceRoot , source . include ) , { dot : true } )
98+ const exclude = source . exclude ?. some ( exclude => minimatch ( p , joinSourcePath ( sourceRoot , exclude ) ) )
7599
76100 return include && ! exclude
77101 } )
0 commit comments