Skip to content

Commit

Permalink
feat: support custom block types (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
aadgrant authored Aug 3, 2021
1 parent 6a22420 commit 88330cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/sanity-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ function renderBlocks (
block,
block._type === 'block'
? (block.children || []).map(child =>
wrapMarks(h, child.text, child.marks, serializers, block.markDefs),
child._type !== 'span'
? h(findSerializer(child as CustomBlock, serializers) || 'span', getProps(child), [wrapMarks(h, child.text, child.marks, serializers, block.markDefs)])
: wrapMarks(h, child.text, child.marks, serializers, block.markDefs),
)
: [],
serializers,
Expand Down
6 changes: 6 additions & 0 deletions test/unit/__snapshots__/sanity-content.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ exports[`SanityContent should render blockquote blocks 1`] = `
</div>
`;

exports[`SanityContent should render customBlockTypes blocks 1`] = `
<div>
<p>An example of <i>custom</i> block types.</p>
</div>
`;

exports[`SanityContent should render exampleMarkDefs blocks 1`] = `
<div>
<ul>
Expand Down
29 changes: 29 additions & 0 deletions test/unit/sanity-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ const exampleBlocks: Record<string, ExampleBlock | ExampleBlock[]> = {
],
style: 'blockquote',
},
customBlockTypes: {
_key: 'd810da8ac845',
_type: 'block',
children: [
{
_key: 'd810da8ac8450',
_type: 'span',
marks: [],
text: 'An example of ',
},
{
_key: '7dc51c030a2f',
_type: 'customIcon',
icon: 'cog',
text: 'custom',
},
{
_key: '230498a',
_type: 'span',
marks: [],
text: ' block types.',
},
],
markDefs: [],
style: 'normal',
},
strong: {
_key: 'd810da8ac845',
_type: 'block',
Expand Down Expand Up @@ -281,6 +307,9 @@ describe('SanityContent', () => {
const wrapper = mount(SanityContent, {
propsData: {
blocks: Array.isArray(block) ? block : [block],
serializers: {
types: { customIcon: 'i' },
},
},
})
expect(wrapper.html()).toMatchSnapshot()
Expand Down

0 comments on commit 88330cf

Please sign in to comment.