Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: documentDriven configuration #1378

Merged
merged 4 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/content/3.guide/1.writing/7.document-driven.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,40 @@ definePageMeta({
</script>
```

### Config content

You can pass custom path/query to `page` and `surround` options to config documentDriven content.

```html
<script setup lang="ts">
definePageMeta({
documentDriven: {
// Simple Path
page: '/foo',
// Rich Query
surround: {
_path: '/foo/bar'
}
}
})
</script>
```

::alert
If you change `page` option and leave `surround` unset, `surround` option will use same config and `page`.
```html
<script setup lang="ts">
definePageMeta({
documentDriven: {
page: {
_path: '/foo/bar'
},
// surround will use `{ _path: '/foo/bar' }`
}
})
</script>
```
::

## Example

Expand Down
16 changes: 16 additions & 0 deletions playground/document-driven/pages/home.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup>
definePageMeta({
documentDriven: {
page: '/'
}
})
const { page } = useContent()
useContentHead(page)
</script>

<template>
<NuxtLayout name="reversed">
<ContentRenderer :value="page" />
</NuxtLayout>
</template>
18 changes: 16 additions & 2 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -147,7 +154,7 @@ export default defineNuxtPlugin((nuxt) => {
}

return queryContent()
.where({ _path })
.where(where)
.findOne()
.catch(() => {
// eslint-disable-next-line no-console
Expand All @@ -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()

Expand All @@ -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}`)
Expand Down
21 changes: 20 additions & 1 deletion test/document-driven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,29 @@ describe('fixtures:document-driven', async () => {
expect(html).contains('<div>page: </div>')
})

test('disabled surround document driven', async () => {
test('disabled surround', async () => {
const html = await $fetch('/no-surround')

expect(html).contains('<div>surround: </div>')
expect(html).contains('<div>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')
})
})
5 changes: 5 additions & 0 deletions test/fixtures/document-driven/content/3.fm-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
score: 23.5
---

# FM Data
34 changes: 34 additions & 0 deletions test/fixtures/document-driven/pages/custom-search.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script setup>
definePageMeta({
documentDriven: {
page: {
score: {
$eq: 23.5
}
}
}
})
const { page, surround } = useContent()
useContentHead(page)
</script>

<template>
<div>
<ContentRenderer :value="page" />
<div>
<div v-if="surround[0]">
with previous link {{ surround[0]._path }}
</div>
<div v-else>
no previous link
</div>
<div v-if="surround[1]">
with next link {{ surround[1]._path }}
</div>
<div v-else>
no next link
</div>
</div>
</div>
</template>
31 changes: 31 additions & 0 deletions test/fixtures/document-driven/pages/home.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup>
definePageMeta({
documentDriven: {
page: '/',
surround: '/debug'
}
})
const { page, surround } = useContent()
useContentHead(page)
</script>

<template>
<div>
<ContentRenderer :value="page" />
<div>
<div v-if="surround[0]">
with previous link {{ surround[0]._path }}
</div>
<div v-else>
no previous link
</div>
<div v-if="surround[1]">
with next link {{ surround[1]._path }}
</div>
<div v-else>
no next link
</div>
</div>
</div>
</template>