Skip to content

Commit 29cfd43

Browse files
Add type for code blocks with no line number and make language dynamic
1 parent 603fc36 commit 29cfd43

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

studio/schemas/codeNoLineNumber.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
title: 'Code No Line Number',
3+
name: 'code_no_line_number',
4+
type: 'object',
5+
fields: [
6+
{
7+
title: 'Code',
8+
name: 'code',
9+
type: 'code',
10+
}
11+
]
12+
}

studio/schemas/post.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
}]
2727
},
2828
{ type: 'code' },
29+
{ type: 'code_no_line_number' },
2930
// TODO: update these type names:
3031
{ type: 'post_aside' },
3132
{ type: 'post_aside_note' },

studio/schemas/schema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import postAside from './postAside'
1111
import postAsideNote from './postAsideNote'
1212
import postAsideWithCode from './postAsideWithCode'
1313
import inlineCode from './inlineCode'
14+
import codeNoLineNumber from './codeNoLineNumber'
1415

1516
// Then we give our schema to the builder and provide the result to Sanity
1617
export default createSchema({
@@ -25,6 +26,7 @@ export default createSchema({
2526
postAside,
2627
postAsideNote,
2728
postAsideWithCode,
28-
inlineCode
29+
inlineCode,
30+
codeNoLineNumber
2931
])
3032
})

web/components/PostImg.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const PostImgContainer = styled.div`
77
`
88
const PostImg = styled.img`
99
position: relative;
10-
top: 0;
11-
left: 0;
10+
/* top: 0;
11+
left: 0; */
1212
display: block;
1313
width: 100%;
1414
transition: opacity 400ms ease 0ms;

web/pages/posts/[slug].js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function Post(props) {
281281
return asideWithCode(content, _key, true)
282282
}
283283

284-
function prismafyCodeBlock(content, _key) {
284+
function prismafyCodeBlock(content, _key, lang, showLineNo) {
285285
return (
286286
<Highlight
287287
{...defaultProps}
@@ -291,14 +291,15 @@ function Post(props) {
291291
: themes.dark.syntax
292292
}
293293
code={content}
294-
language="jsx"
294+
// TODO: make language dynamic
295+
language={lang}
295296
key={_key}
296297
>
297298
{({ className, style, tokens, getLineProps, getTokenProps }) => (
298-
<Pre className={className} style={style}>
299+
<Pre className={className}>
299300
{tokens.map((line, i) => (
300301
<div {...getLineProps({ line, key: i })}>
301-
<LineNo>{i + 1}</LineNo>
302+
{showLineNo && <LineNo>{i + 1}</LineNo>}
302303
{line.map((token, key) => {
303304
return (
304305
<span {...getTokenProps({ token, key })} />
@@ -314,6 +315,7 @@ function Post(props) {
314315
// TODO: test if list rendering works correctly with multiple lists in post
315316
let list = []
316317
props.body && props.body.forEach(section => {
318+
console.log('section: ', section)
317319
if (section.listItem) {
318320
list.push(
319321
<ListItem key={section._key}>{formatListItem(section.children)}</ListItem>
@@ -334,7 +336,22 @@ function Post(props) {
334336
break
335337
case 'code':
336338
postContent.push(
337-
prismafyCodeBlock(section.code, section._key)
339+
prismafyCodeBlock(
340+
section.code,
341+
section._key,
342+
section.language,
343+
true
344+
)
345+
)
346+
break
347+
case 'code_no_line_number':
348+
postContent.push(
349+
prismafyCodeBlock(
350+
section.code.code,
351+
section._key,
352+
section.code.language,
353+
false
354+
)
338355
)
339356
break
340357
case 'post_aside':

0 commit comments

Comments
 (0)