From 6520ec50350ff3b87a6834254e1f915fac3ac27e Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Tue, 18 Oct 2022 17:05:42 +0200 Subject: [PATCH 1/2] fix(`queryContent`): use path argument as prefix if there is another condition --- playground/basic/pages/features/[slug].vue | 21 +++++++++ src/runtime/composables/query.ts | 4 +- test/features/content-query.ts | 43 +++++++++++++++++-- .../content/_partial/mdc-props-inline.md | 2 + .../basic/content/_partial/mdc-props.md | 2 + .../basic/pages/features/query-content.vue | 20 ++++++--- 6 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 playground/basic/pages/features/[slug].vue diff --git a/playground/basic/pages/features/[slug].vue b/playground/basic/pages/features/[slug].vue new file mode 100644 index 000000000..8f6540976 --- /dev/null +++ b/playground/basic/pages/features/[slug].vue @@ -0,0 +1,21 @@ + + + + + diff --git a/src/runtime/composables/query.ts b/src/runtime/composables/query.ts index 039473c40..4d059b44e 100644 --- a/src/runtime/composables/query.ts +++ b/src/runtime/composables/query.ts @@ -11,7 +11,9 @@ import { addPrerenderPath, shouldUseClientDB, withContentBase } from './utils' */ export const createQueryFetch = (path?: string) => async (query: QueryBuilder) => { if (path) { - if (query.params().first) { + if (query.params().first && (query.params().where || []).length === 0) { + // If query contains `path` and does not contain any `where` condition + // Then can use `path` as `where` condition to find exact match query.where({ _path: withoutTrailingSlash(path) }) } else { query.where({ _path: new RegExp(`^${path.replace(/[-[\]{}()*+.,^$\s/]/g, '\\$&')}`) }) diff --git a/test/features/content-query.ts b/test/features/content-query.ts index ef0d0dc3c..d81383ed5 100644 --- a/test/features/content-query.ts +++ b/test/features/content-query.ts @@ -11,33 +11,68 @@ export const testContentQuery = () => { }) test('exact match foo not found', async () => { - const content = await $fetch('/features/query-content?path=/foo&findOne=1') + const content = await $fetch('/features/query-content?path=/prefix/foo&findOne=1') // empty expect(content).includes('$$$$') }) + // test `queryContent( PREFIX ).findOne()` test('exact match foo/bar found', async () => { - const content = await $fetch('/features/query-content?path=/foo/bar&findOne=1') + const content = await $fetch('/features/query-content?path=/prefix/foo/bar&findOne=1') // empty expect(content).includes('prefix:foo:bar.md$$') }) + // test `queryContent( PREFIX ).find()` test('prefix queries', async () => { - const content = await $fetch('/features/query-content?path=/foo') + const content = await $fetch('/features/query-content?path=/prefix/foo') expect(content).includes('prefix:foo:bar.md') expect(content).includes('prefix:foo:baz.md') expect(content).includes('prefix:foobarbaz.md') }) + // test `queryContent( PREFIX ).find()` with trailing slash test('directory listing', async () => { - const content = await $fetch('/features/query-content?path=/foo/') + const content = await $fetch('/features/query-content?path=/prefix/foo/') expect(content).includes('prefix:foo:bar.md') expect(content).includes('prefix:foo:baz.md') expect(content).not.includes('prefix:foobarbaz.md') }) + + // test `queryContent( PREFIX ).where( CONDITION ).find()` + test('list contents with tag', async () => { + const content = await $fetch('/features/query-content?where={"tags": { "$contains": "mdc" } }') + + expect(content).includes(':mdc-props-inline.md') + expect(content).includes(':mdc-props.md') + }) + + // test `queryContent( PREFIX ).where( CONDITION ).findOne()` + test('find contents with tag', async () => { + const content = await $fetch('/features/query-content?where={"tags": { "$contains": "mdc" } }&findOne=1') + + expect(content).includes(':mdc-props-inline.md') + expect(content).not.includes(':mdc-props.md') + }) + + // test `queryContent().where( CONDITION ).find()` + test('find contents with tag', async () => { + const content = await $fetch('/features/query-content?prefix=&where={"tags": { "$contains": "mdc" } }') + + expect(content).includes(':mdc-props-inline.md') + expect(content).includes(':mdc-props.md') + }) + + // test `queryContent().where( CONDITION ).findOne()` + test('find contents with tag', async () => { + const content = await $fetch('/features/query-content?prefix=&where={"tags": { "$contains": "mdc" } }&findOne=1') + + expect(content).includes(':mdc-props-inline.md') + expect(content).not.includes(':mdc-props.md') + }) }) } diff --git a/test/fixtures/basic/content/_partial/mdc-props-inline.md b/test/fixtures/basic/content/_partial/mdc-props-inline.md index a4aba1b33..c26aeb023 100644 --- a/test/fixtures/basic/content/_partial/mdc-props-inline.md +++ b/test/fixtures/basic/content/_partial/mdc-props-inline.md @@ -1,4 +1,6 @@ --- +tags: +- mdc categories: - Art - History diff --git a/test/fixtures/basic/content/_partial/mdc-props.md b/test/fixtures/basic/content/_partial/mdc-props.md index 5bf80fa16..4d1af97e2 100644 --- a/test/fixtures/basic/content/_partial/mdc-props.md +++ b/test/fixtures/basic/content/_partial/mdc-props.md @@ -1,4 +1,6 @@ --- +tags: +- mdc categories: - Art - History diff --git a/test/fixtures/basic/pages/features/query-content.vue b/test/fixtures/basic/pages/features/query-content.vue index 3b937d13f..b985bdcf3 100644 --- a/test/fixtures/basic/pages/features/query-content.vue +++ b/test/fixtures/basic/pages/features/query-content.vue @@ -6,15 +6,21 @@ From 4dfd01bfb71b5c188e1fad3cf8129e0f5f898005 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Tue, 18 Oct 2022 17:12:44 +0200 Subject: [PATCH 2/2] chore: remove test page --- playground/basic/pages/features/[slug].vue | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 playground/basic/pages/features/[slug].vue diff --git a/playground/basic/pages/features/[slug].vue b/playground/basic/pages/features/[slug].vue deleted file mode 100644 index 8f6540976..000000000 --- a/playground/basic/pages/features/[slug].vue +++ /dev/null @@ -1,21 +0,0 @@ - - - - -