From e68737eb5de7a54f4913d9a0ab9eaf41d824f66f Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 25 Jul 2022 13:56:50 +0300 Subject: [PATCH 1/4] feat: documentDriven configuration --- playground/document-driven/pages/home.vue | 16 ++++++++++++++++ src/runtime/plugins/documentDriven.ts | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 playground/document-driven/pages/home.vue diff --git a/playground/document-driven/pages/home.vue b/playground/document-driven/pages/home.vue new file mode 100644 index 000000000..ed3980f0d --- /dev/null +++ b/playground/document-driven/pages/home.vue @@ -0,0 +1,16 @@ + + + diff --git a/src/runtime/plugins/documentDriven.ts b/src/runtime/plugins/documentDriven.ts index 6ad1ff10a..76af75278 100644 --- a/src/runtime/plugins/documentDriven.ts +++ b/src/runtime/plugins/documentDriven.ts @@ -138,6 +138,13 @@ export default defineNuxtPlugin((nuxt) => { * `page` */ if (moduleOptions.page && routeConfig.page !== false) { + let where = { _path } + if (typeof routeConfig.page === 'string') { + where = { _path: routeConfig.page } + } + if (typeof routeConfig.page === 'object') { + where = routeConfig.page + } const pageQuery = () => { const { pages } = useContentState() @@ -147,7 +154,7 @@ export default defineNuxtPlugin((nuxt) => { } return queryContent() - .where({ _path }) + .where(where) .findOne() .catch(() => { // eslint-disable-next-line no-console @@ -164,6 +171,13 @@ export default defineNuxtPlugin((nuxt) => { * `surround` */ if (moduleOptions.surround && routeConfig.surround !== false) { + let surround: any = _path + if (['string', 'object'].includes(typeof routeConfig.page)) { + surround = { _path: routeConfig.page } + } + if (['string', 'object'].includes(typeof routeConfig.surround)) { + surround = routeConfig.surround + } const surroundQuery = () => { const { surrounds } = useContentState() @@ -179,7 +193,7 @@ export default defineNuxtPlugin((nuxt) => { }) // Exclude `body` for `surround` .without(['body']) - .findSurround(_path) + .findSurround(surround) .catch(() => { // eslint-disable-next-line no-console // console.log(`Could not find surrounding pages for: ${to.path}`) From fb91c2cab67a04f5046d3a40fa7715f17a1ea662 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 25 Jul 2022 14:44:43 +0300 Subject: [PATCH 2/4] fix: surround fallback --- src/runtime/plugins/documentDriven.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/plugins/documentDriven.ts b/src/runtime/plugins/documentDriven.ts index 76af75278..6e7b173ce 100644 --- a/src/runtime/plugins/documentDriven.ts +++ b/src/runtime/plugins/documentDriven.ts @@ -173,7 +173,7 @@ export default defineNuxtPlugin((nuxt) => { if (moduleOptions.surround && routeConfig.surround !== false) { let surround: any = _path if (['string', 'object'].includes(typeof routeConfig.page)) { - surround = { _path: routeConfig.page } + surround = routeConfig.page } if (['string', 'object'].includes(typeof routeConfig.surround)) { surround = routeConfig.surround From a7cc127d380ef1985f0db038623ff61c9f35eba0 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 25 Jul 2022 14:44:57 +0300 Subject: [PATCH 3/4] test: add tests --- test/document-driven.test.ts | 21 +++++++++++- .../document-driven/content/3.fm-data.md | 5 +++ .../document-driven/pages/custom-search.vue | 34 +++++++++++++++++++ test/fixtures/document-driven/pages/home.vue | 31 +++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/document-driven/content/3.fm-data.md create mode 100644 test/fixtures/document-driven/pages/custom-search.vue create mode 100644 test/fixtures/document-driven/pages/home.vue diff --git a/test/document-driven.test.ts b/test/document-driven.test.ts index 0f6d73a9a..0a9ebe16b 100644 --- a/test/document-driven.test.ts +++ b/test/document-driven.test.ts @@ -21,10 +21,29 @@ describe('fixtures:document-driven', async () => { expect(html).contains('
page:
') }) - test('disabled surround document driven', async () => { + test('disabled surround', async () => { const html = await $fetch('/no-surround') expect(html).contains('
surround:
') expect(html).contains('
page: {') }) + + test('custom content with path', async () => { + const html = await $fetch('/home') + + expect(html).contains('Home') + expect(html).contains('Hello World!') + + expect(html).contains('with previous link /') + expect(html).contains('with next link /layout') + }) + + test('custom content with condition', async () => { + const html = await $fetch('/custom-search') + + expect(html).contains('FM Data') + + expect(html).contains('with previous link /layout') + expect(html).contains('with next link /no-surround') + }) }) diff --git a/test/fixtures/document-driven/content/3.fm-data.md b/test/fixtures/document-driven/content/3.fm-data.md new file mode 100644 index 000000000..b653db9f9 --- /dev/null +++ b/test/fixtures/document-driven/content/3.fm-data.md @@ -0,0 +1,5 @@ +--- +score: 23.5 +--- + +# FM Data \ No newline at end of file diff --git a/test/fixtures/document-driven/pages/custom-search.vue b/test/fixtures/document-driven/pages/custom-search.vue new file mode 100644 index 000000000..810651678 --- /dev/null +++ b/test/fixtures/document-driven/pages/custom-search.vue @@ -0,0 +1,34 @@ + + + diff --git a/test/fixtures/document-driven/pages/home.vue b/test/fixtures/document-driven/pages/home.vue new file mode 100644 index 000000000..a14a994c7 --- /dev/null +++ b/test/fixtures/document-driven/pages/home.vue @@ -0,0 +1,31 @@ + + + From c1c224c33758428a326d84822bb10168e51db8c5 Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 25 Jul 2022 15:11:37 +0300 Subject: [PATCH 4/4] docs: add example --- .../3.guide/1.writing/7.document-driven.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/content/3.guide/1.writing/7.document-driven.md b/docs/content/3.guide/1.writing/7.document-driven.md index ac672ba75..1979908e0 100644 --- a/docs/content/3.guide/1.writing/7.document-driven.md +++ b/docs/content/3.guide/1.writing/7.document-driven.md @@ -195,6 +195,40 @@ definePageMeta({ ``` +### Config content + +You can pass custom path/query to `page` and `surround` options to config documentDriven content. + +```html + +``` + +::alert +If you change `page` option and leave `surround` unset, `surround` option will use same config and `page`. +```html + +``` +:: ## Example