Adding code formatting plugin to BlockContent field type in Sanity.io
22 replies
Last updated: Jan 26, 2021
P
Are there any plugins/block-content addons to add
code formattinglike here in
Slack
Jan 26, 2021, 3:56 PM
P
I'm using @sanity/code-input and it's working pretty good
Jan 26, 2021, 3:59 PM
P
Thanks
user L
, but just to confirm - isnโt that plug-in an entirely separate field in the Sanity? I am wondering if there is a way to add that code formatting specifically inside the BlockContent field type.Jan 26, 2021, 4:05 PM
P
Does it work there?
Jan 26, 2021, 4:05 PM
P
Yep, I installed it and a
Jan 26, 2021, 4:06 PM
P
I'm using it this way
Jan 26, 2021, 4:06 PM
P
{ name: 'content', title: 'Content', type: 'array', of: [ { type: 'block' }, { type: 'image' }, { type: 'table' }, { type: 'code' }, ], },
Jan 26, 2021, 4:07 PM
P
And when I wanna add a code block it shows like this:
Jan 26, 2021, 4:08 PM
P
And this is how the code block is shown in the content
Jan 26, 2021, 4:10 PM
P
I'd also want to use this for my project, thanks for sharing!
Jan 26, 2021, 5:00 PM
P
Awwwwwww snap, you are a fantastic person
user L
- thank you!Jan 26, 2021, 5:07 PM
P
user L
I have another question regarding this plugin. I'm currently using block-content-to-react, what would be the serializer to allow code blocks?Jan 26, 2021, 5:45 PM
P
*serializer config
Jan 26, 2021, 5:45 PM
P
Did you try using something like this?
const serializers = { types: { code: props => ( <pre data-language={props.node.language}> <code>{props.node.code}</code> </pre> ) } }
Jan 26, 2021, 5:49 PM
P
No I didn't, I'll try that out
Jan 26, 2021, 5:49 PM
P
Ok, let me know if it helps
Jan 26, 2021, 5:49 PM
P
It does work, but I am having an issue with linting errors and I'm not sure how to solve this
Jan 26, 2021, 9:41 PM
P
create a component outside with this
something like this
then add the component like this
It should work
๐ค
props => ( <pre data-language={props.node.language}> <code>{props.node.code}</code> </pre> )
const MyFancyCodeSerializer = props => ( <pre data-language={props.node.language}> <code>{props.node.code}</code> </pre> )
const serializers = { types: { code: MyFancyCodeSerializer } }
๐ค
Jan 26, 2021, 9:58 PM
P
I'll try it out ๐. I'm also using typescript, so I'll fill the types in myself
Jan 26, 2021, 10:04 PM
P
Great, let me know it this helps with eslint.
Jan 26, 2021, 10:09 PM
P
Worked perfectly
Jan 26, 2021, 10:27 PM
P
Here's my TS code for reference
type CodeProps = { node: { language: string; code: string; }; }; export function CodeSerializer(props: CodeProps) { return ( <pre data-language={props.node.language}> <code>{props.node.code}</code> </pre> ); } const serializers = { types: { code: CodeSerializer } };
Jan 26, 2021, 10:27 PM
P
Nice, I'm glad it worked ๐. The issue was that eslint was rejecting the component as it was a anonymous function
Jan 26, 2021, 10:36 PM
Sanityโ build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.