Skip to content

Commit fc42ab0

Browse files
farnabazlarbish
andcommitted
improve tests and fix behavior
Co-authored-by: Baptiste Leproux <leproux.baptiste@gmail.com>
1 parent 669966e commit fc42ab0

File tree

7 files changed

+65
-20
lines changed

7 files changed

+65
-20
lines changed

playground/docus/ahad/index.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
const { data: page } = await useAsyncData('prefixed-page', () => queryCollection('prefixed').path(useRoute().path).first())
3+
</script>
4+
5+
<template>
6+
<UContainer>
7+
Hello
8+
<ContentRenderer
9+
v-if="page"
10+
:value="page"
11+
/>
12+
</UContainer>
13+
</template>

playground/docus/content.config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ const collections: Record<string, DefinedCollection> = {
4646
type: 'page',
4747
source: {
4848
include: '**',
49-
exclude: ['index.md', '3.pages/**/*.md', 'authors/**/*'],
49+
exclude: ['index.md', '3.pages/**/*.md', 'authors/**/*', 'fs-prefix/**'],
5050
},
5151
schema: createDocsSchema(),
5252
}),
53-
ahad: defineCollection({
53+
prefixed: defineCollection({
5454
type: 'page',
5555
source: {
56-
cwd: '~~/ahad',
57-
include: '**',
58-
prefix: '/ahad-prefix',
56+
include: 'fs-prefix/**',
57+
prefix: '/route-prefix',
5958
},
6059
schema: createDocsSchema(),
6160
}),

playground/docus/content/a2/hello.md

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Prefixed File
3+
description: Hello
4+
---
5+
6+
# Prefixed File
7+
8+
Hello Baptiste 🎉

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ export function getCollectionByFilePath(path: string, collections: Record<string
7070
const paths = path === '/' ? ['index.yml', 'index.yaml', 'index.md', 'index.json'] : [path]
7171
return paths.some((p) => {
7272
matchedSource = collection.source.find((source) => {
73-
const prefix = withoutTrailingSlash(withoutLeadingSlash(source.prefix || ''))
74-
if (prefix && prefix !== '/' && !p.startsWith(prefix + '/')) {
75-
return false
76-
}
7773
const include = minimatch(p, source.include, { dot: true })
7874
const exclude = source.exclude?.some(exclude => minimatch(p, exclude))
7975

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ describe('getCollectionByFilePath', () => {
4141

4242
expect(result).toBeUndefined()
4343
})
44+
45+
it('Custom include pattern with different prefix', () => {
46+
const id = 'prefixed/route-prefix/hello.md'
47+
const source: ResolvedCollectionSource = {
48+
include: 'fs-prefix/**',
49+
prefix: '/route-prefix',
50+
cwd: '',
51+
_resolved: true,
52+
}
53+
const result = generateFsPathFromId(id, source)
54+
expect(result).toBe('fs-prefix/hello.md')
55+
})
4456
})
4557

4658
describe('generateFsPathFromId', () => {
@@ -168,4 +180,32 @@ describe('generateIdFromFsPath', () => {
168180
const result = generateIdFromFsPath(path, customCollection)
169181
expect(result).toBe('pages/about.md')
170182
})
183+
184+
it.only('should handle collection with route prefix different from the fixed part of the include pattern', () => {
185+
const customCollection: CollectionInfo = {
186+
name: 'prefixed',
187+
pascalName: 'Prefixed',
188+
tableName: '_content_prefixed',
189+
source: [
190+
{
191+
_resolved: true,
192+
include: 'fs-prefix/**',
193+
prefix: '/route-prefix',
194+
cwd: '',
195+
},
196+
],
197+
type: 'page',
198+
fields: {},
199+
schema: {
200+
$schema: 'http://json-schema.org/draft-07/schema#',
201+
$ref: '#/definitions/prefixed',
202+
definitions: {},
203+
},
204+
tableDefinition: '',
205+
}
206+
207+
const path = 'fs-prefix/hello.md'
208+
const result = generateIdFromFsPath(path, customCollection)
209+
expect(result).toBe('prefixed/route-prefix/hello.md')
210+
})
171211
})

0 commit comments

Comments
 (0)