Skip to content

Commit 44bfcab

Browse files
committed
fix: render children of custom components
1 parent fbb6a9e commit 44bfcab

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

src/runtime/components/sanity-content.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,12 @@ function renderStyle ({ style, listItem }: Block, serializers: Required<Serializ
116116
}
117117

118118
function renderInSerializer (item: Block, serializers: Required<Serializers>) {
119-
if (item._type !== 'block') return render(serializers, item)
120-
121119
return render(serializers, item, () => (item.children || []).map((child) => {
122120
if (isSpan(child)) {
123121
return renderMarks(child.text, child.marks, serializers, item.markDefs)
124122
}
125123
return render(serializers, child, () => renderMarks(child.text, child.marks, serializers, item.markDefs))
126-
}),
127-
)
124+
}))
128125
}
129126

130127
function renderMarks (

test/unit/__snapshots__/sanity-content.test.ts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ exports[`SanityContent > should render blockquote blocks 1`] = `"<blockquote>Bui
1717
1818
exports[`SanityContent > should render customBlockTypes blocks 1`] = `"<p>An example of <i>custom</i> block types.</p>"`;
1919
20+
exports[`SanityContent > should render customComponent blocks 1`] = `
21+
"<div>propValuetext content</div>
22+
<div>propValuetext content</div>"
23+
`;
24+
2025
exports[`SanityContent > should render evenMoreNestedList blocks 1`] = `
2126
"<ol>
2227
<li>1. Top level</li>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export const customComponent = [
2+
{
3+
_key: '1',
4+
_type: 'customComponent1',
5+
exampleProp: 'propValue',
6+
children: [
7+
{
8+
_key: 'd810da8ac8450',
9+
_type: 'span',
10+
marks: [],
11+
text: 'text content',
12+
},
13+
],
14+
},
15+
{
16+
_key: '2',
17+
_type: 'customComponent2',
18+
exampleProp: 'propValue',
19+
children: [
20+
{
21+
_key: 'd810da8ac8450',
22+
_type: 'span',
23+
marks: [],
24+
text: 'text content',
25+
},
26+
],
27+
},
28+
{
29+
_key: '3',
30+
_type: 'customComponent3',
31+
exampleProp: 'propValue',
32+
children: [
33+
{
34+
_key: 'd810da8ac8450',
35+
_type: 'span',
36+
marks: [],
37+
text: 'text content',
38+
},
39+
],
40+
},
41+
]

test/unit/fixture/portable-text/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './adjacent-list'
22
export * from './blockquote'
3+
export * from './custom-components'
34
export * from './custom-block-types'
45
export * from './list'
56
export * from './mark-defs'

test/unit/sanity-content.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { expect, describe, it } from 'vitest'
22
import { mount } from '@vue/test-utils'
3+
import { defineAsyncComponent, defineComponent, h, markRaw } from 'vue'
34
import SanityContent from '../../src/runtime/components/sanity-content'
45

56
import * as exampleBlocks from './fixture/portable-text'
67

8+
const CustomBlockComponent = defineComponent({
9+
props: { exampleProp: String },
10+
setup: (props, { slots }) => () => h('div', {}, {
11+
default: () => [
12+
props.exampleProp,
13+
slots.default?.(),
14+
],
15+
}),
16+
})
17+
718
describe('SanityContent', () => {
819
it('should render with no props', () => {
920
const wrapper = mount(SanityContent)
@@ -15,9 +26,20 @@ describe('SanityContent', () => {
1526
const wrapper = mount(SanityContent as any, {
1627
props: {
1728
blocks: Array.isArray(block) ? block : [block],
18-
serializers: {
19-
types: { customIcon: 'i' },
20-
},
29+
serializers: markRaw({
30+
types: {
31+
customIcon: 'i',
32+
// This is how to access a component registered by `@nuxt/components`
33+
customComponent1: CustomBlockComponent,
34+
// A directly imported component
35+
customComponent2: CustomBlockComponent,
36+
// Example of a more complex async component
37+
customComponent3: defineAsyncComponent({
38+
loadingComponent: () => h('div', 'Loading...'),
39+
loader: () => Promise.resolve(CustomBlockComponent),
40+
}),
41+
},
42+
}),
2143
},
2244
})
2345
expect(wrapper.html()).toMatchSnapshot()

0 commit comments

Comments
 (0)