Skip to content

Commit 600b231

Browse files
Add images to post schema and update front-end query
1 parent 7eb048e commit 600b231

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

studio/schemas/post.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export default {
2020
name: 'body',
2121
type: 'array',
2222
of: [
23+
{
24+
type: 'image',
25+
fields: [
26+
{
27+
name: 'caption',
28+
type: 'string',
29+
title: 'Caption',
30+
},
31+
{
32+
name: 'alt_text',
33+
type: 'string',
34+
title: 'Alt Text',
35+
}
36+
]
37+
},
2338
{
2439
type: 'block',
2540
of: [{

web/pages/posts/[slug].js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ function Post(props) {
162162
return subBlockContent
163163
}
164164

165+
// TODO: figure out how to add text formatting (bold, italic) to list items
165166
function formatListItem(sectionChildren, markDefs) {
166167
// console.log(`sectionChildren: `, sectionChildren)
167168
const listItem = []
@@ -336,13 +337,17 @@ function Post(props) {
336337
</Highlight>
337338
)
338339
}
340+
341+
function postBodyImage(content) {
342+
console.log('content: ', content)
343+
}
344+
339345
// TODO: test if list rendering works correctly with multiple lists in post
340346
let list = []
341347
let listGroupKey = ''
342348
props.body && props.body.forEach((section, i, arr) => {
343-
349+
// console.log('section: ', section, '----\n')
344350
if (section.listItem) {
345-
// console.log('section.listItem', section)
346351
list.push(
347352
<ListItem key={section._key}>
348353
{formatListItem(section.children, section.markDefs)}
@@ -367,6 +372,11 @@ function Post(props) {
367372

368373
if (!section.listItem) {
369374
switch(section._type) {
375+
case 'image':
376+
postContent.push(
377+
postBodyImage(section)
378+
)
379+
break
370380
case 'block':
371381
postContent.push(
372382
paragraphBlock(section)
@@ -480,6 +490,10 @@ export async function getStaticProps(context) {
480490
const post = await client.fetch(`
481491
*[_type == "post" && slug.current == $slug][0]{
482492
...,
493+
body[]{
494+
...,
495+
asset->
496+
},
483497
"postImg": image.asset->,
484498
}
485499
`, { slug } )

0 commit comments

Comments
 (0)