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 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..6e7b173ce 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 = 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}`) 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 @@ + + +