Skip to content

Commit

Permalink
refactor(ContentRenderer): simplify conditions (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
huynl-96 committed Nov 30, 2022
1 parent 67e1f53 commit 5dfa181
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/runtime/components/ContentRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default defineComponent({
const { value, excerpt, tag } = ctx
if ((!value || !value?.body?.children?.length) && slots?.empty) {
if (!value?.body?.children?.length && slots?.empty) {
// Fallback on `empty` slot.
return slots.empty({ value, excerpt, tag, ...this.$attrs })
}
Expand All @@ -70,7 +70,7 @@ export default defineComponent({
}
// Use built-in ContentRendererMarkdown
if (value && value?._type === 'markdown' && value?.body?.children?.length) {
if (value?._type === 'markdown' && value?.body?.children?.length) {
return h(
ContentRendererMarkdown,
{
Expand Down
3 changes: 2 additions & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ describe('Basic usage', async () => {

test('Empty slot', async () => {
const html = await $fetch('/features/empty-slot')
expect(html).contains('Empty!!!')
expect(html).contains('Nullish Document!!!')
expect(html).contains('Empty Child!!!')
})

test('<ContentDoc> head management (if same path)', async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/custom-api-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ describe('Custom api baseURL', async () => {

test('Empty slot', async () => {
const html = await $fetch('/features/empty-slot')
expect(html).contains('Empty!!!')
expect(html).contains('Nullish Document!!!')
expect(html).contains('Empty Child!!!')
})

test('<ContentDoc> head management (if same path)', async () => {
Expand Down
22 changes: 16 additions & 6 deletions test/fixtures/basic/pages/features/empty-slot.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<template>
<ContentRenderer :value="emptyDocument">
<template #empty>
Empty!!!
</template>
</ContentRenderer>
<div>
<ContentRenderer :value="nullishDocument">
<template #empty>
Nullish Document!!!
</template>
</ContentRenderer>

<ContentRenderer :value="emptyChild">
<template #empty>
Empty Child!!!
</template>
</ContentRenderer>
</div>
</template>

<script setup>
const emptyDocument = {
const nullishDocument = null
const emptyChild = {
_path: '/_markdown',
_dir: '',
_draft: false,
Expand Down

0 comments on commit 5dfa181

Please sign in to comment.