Skip to content

Commit 5199e14

Browse files
Format hrefs in list items and fix bug causing end-of-post lists to not render
1 parent 690c113 commit 5199e14

File tree

2 files changed

+67
-37
lines changed

2 files changed

+67
-37
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ My personal blog, made with the following technologies:
99
- Sanity.io headless CMS
1010

1111
## TODOs
12+
- [ ] add internal links between posts
13+
- [ ] fix tags associated with unpublished posts to not be displayed
14+
- [ ] prevent top-of-post list bullets from being rendered where post image gets rendered prior to image being rendered
1215
- [ ] store theme preference in local storage
1316
- [ ] add logic that checks if manual published date is set before sorting
14-
- [ ] fix tags associated with unpublished posts to not be displayed
1517
- [ ] change how aside is serialized from asterisk
1618
- [ ] setup code block rendering from within asides
17-
- [ ] add internal links between posts
1819
- [ ] optimize loading data from Sanity
1920
- [ ] figure out what's going on with blank spans around inline code?

web/pages/posts/[slug].js

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,44 @@ function Post(props) {
141141
})
142142
}
143143

144-
function formatListItem(listItemParts) {
144+
function formatExternalHref(sectionChild, sectionMarkDefs) {
145+
const subBlockContent = []
146+
const hrefTarget = matchExtLinkMarkDef(
147+
sectionChild,
148+
sectionMarkDefs
149+
)
150+
hrefTarget && (
151+
subBlockContent.push(
152+
<ExternalLink
153+
target="_blank"
154+
href={hrefTarget.href}
155+
key={hrefTarget._key}
156+
>
157+
{hrefTarget.text}
158+
</ExternalLink>
159+
)
160+
)
161+
return subBlockContent
162+
}
163+
164+
function formatListItem(sectionChildren, markDefs) {
165+
// console.log(`sectionChildren: `, sectionChildren)
145166
const listItem = []
146-
for (let i = 0; i < listItemParts.length; i++) {
147-
if (listItemParts[i]._type === 'inline_code') {
167+
for (let i = 0; i < sectionChildren.length; i++) {
168+
if (sectionChildren[i].marks && sectionChildren[i].marks.length > 0) {
169+
listItem.push(
170+
formatExternalHref(sectionChildren[i], markDefs)
171+
)
172+
}
173+
else if (sectionChildren[i]._type === 'inline_code') {
148174
listItem.push(
149-
<InlineCodeMain key={listItemParts[i]._key}>
150-
{listItemParts[i].str_content_inline}
175+
<InlineCodeMain key={sectionChildren[i]._key}>
176+
{sectionChildren[i].str_content_inline}
151177
</InlineCodeMain>
152178
)
153-
} else {
154-
listItem.push(listItemParts[i].text)
179+
}
180+
else {
181+
listItem.push(sectionChildren[i].text)
155182
}
156183
}
157184
return listItem
@@ -175,29 +202,25 @@ function Post(props) {
175202
}
176203
// returns href of external link that matches href mark with actual href info
177204
else if (section.children[i].marks.length > 0) {
178-
const hrefTarget = matchExtLinkMarkDef(
179-
section.children[i],
180-
section.markDefs
181-
)
182-
hrefTarget && (
183-
blockContent.push(
184-
<ExternalLink
185-
target="_blank"
186-
href={hrefTarget.href}
187-
key={hrefTarget._key}
188-
>
189-
{hrefTarget.text}
190-
</ExternalLink>
191-
)
205+
blockContent.push(
206+
formatExternalHref(section.children[i], section.markDefs)
192207
)
193208
}
194209
else if (section.style === 'h3') {
195-
blockContent.push(<H3 key={section._key}>{section.children[0].text}</H3>)
210+
blockContent.push(
211+
<H3 key={section._key}>
212+
{section.children[0].text}
213+
</H3>
214+
)
196215
}
197216
else if (section.style === 'h4') {
198-
blockContent.push(<H4 key={section._key}>{section.children[0].text}</H4>)
217+
blockContent.push(
218+
<H4 key={section._key}>
219+
{section.children[0].text}
220+
</H4>
221+
)
199222
}
200-
// unformatted text block
223+
// Unformatted text block:
201224
else {
202225
blockContent.push(section.children[i].text)
203226
}
@@ -316,21 +339,27 @@ function Post(props) {
316339
let list = []
317340
let listGroupKey = ''
318341
props.body && props.body.forEach(section => {
319-
// console.log('section: ', section)
342+
// console.log('section >>> ', section)
320343
if (section.listItem) {
321344
list.push(
322-
<ListItem key={section._key}>{formatListItem(section.children)}</ListItem>
345+
<ListItem key={section._key}>
346+
{formatListItem(section.children, section.markDefs)}
347+
</ListItem>
323348
)
324349
listGroupKey += section._key
325-
} else {
326-
if (list.length > 0) {
327-
postContent.push(
328-
// TODO: why is key needed on this UL here?
329-
<UL key={listGroupKey}>{list}</UL>
330-
)
331-
list = []
332-
listGroupKey = ''
333-
}
350+
}
351+
352+
if (list.length > 0) {
353+
// console.log('list: ', list)
354+
postContent.push(
355+
// TODO: why is key needed on this UL here?
356+
<UL key={listGroupKey}>{list}</UL>
357+
)
358+
list = []
359+
listGroupKey = ''
360+
}
361+
362+
else {
334363
switch(section._type) {
335364
case 'block':
336365
postContent.push(

0 commit comments

Comments
 (0)