@sanity/code-input
NOTE
This is the Sanity Studio v3 version of @sanity/code-input.
For the v2 version, please refer to the v2-branch.
What is it?
Code input for Sanity.
Currently only a subset of languages and features are exposed, over time we will implement a richer set of options.

Click lines to highlight them.
Installation
npm install --save @sanity/code-input@studio-v3
or
yarn add @sanity/code-input@studio-v3
Usage
Add it as a plugin in sanity.config.ts (or .js):
import { codeInput } from "@sanity/code-input";
export default defineConfig({
plugins: [
codeInput(),
]
})
Now you can use the code
type in your schema types:
{
fields: [
{
name: 'exampleUsage',
title: 'Example usage',
type: 'code',
},
]
}
Options
language
- Default language for this code fieldlanguageAlternatives
- Array of languages that should be available (se its format in the example below)theme
- Name of the theme to use.
- Possible values include:
['github', 'monokai', 'terminal', 'tomorrow']
. - For the full list and a live playground, refer to the react-ace page.
- Default value: 'tomorrow'
darkTheme
- Name of the theme to use when Studio is using dark mode. See theme
options for supported values.
withFilename
- Boolean option to display input field for filename
{
name: 'exampleUsage',
title: 'Code with all options',
type: 'code',
options: {
theme: 'github',
darkTheme: 'terminal',
language: 'js',
languageAlternatives: [
{title: 'Javascript', value: 'js'},
{title: 'HTML', value: 'html'},
{title: 'CSS', value: 'css'},
{title: 'Rust', value: 'rust', mode:'rust'},
{title: 'SASS', value: 'sass'},
]
}
}

Add support for more languages
Only a subset of languages are supported by default (see full list here). You can add support for other languages by importing the ace mode yourself, and specifying mode
for the languageAlternatives
schema config.
Example: Add support for the Rust Programming Language
import 'ace-builds/src-noconflict/mode-rust'
{
name: 'exampleUsage',
title: 'Example usage',
type: 'code',
options: {
languageAlternatives: [
{title: 'Javascript', value: 'js'},
{
title: 'Rust',
value: 'rust',
mode:'rust'
},
]
}
}
Data model
{
_type: 'code',
language: 'js',
highlightedLines: [1, 2],
code: 'const foo = "bar"\nconsole.log(foo.toUpperCase())\n// BAR',
filename: 'available when enabled'
}
Example usage in frontend (React)
You can use any syntax highlighter you want - but not all of them might support highlighted lines or the syntax you've defined.
As outlined above, the actual code is stored in a code
property, so if your schema has a field called codeExample
of type code
, the property you'd want to pass to the highlighter would be codeExample.code
.
Here's an example using react-refractor:
import React from 'react'
import Refractor from 'react-refractor'
import js from 'refractor/lang/javascript'
Refractor.registerLanguage(js)
export function Code(props) {
return (
<Refractor
// In this example, `props` is the value of a `code` field
language={props.language}
value={props.code}
markers={props.highlightedLines}
/>
)
}
Other syntax highlighters include:
License
MIT-licensed. See LICENSE.
Develop & test
This plugin uses @sanity/plugin-kit
with default configuration for build & watch scripts.
See Testing a plugin in Sanity Studio
on how to run this plugin with hotreload in the studio.
Release new version
Run "CI & Release" workflow.
Make sure to select the main branch and check "Release new version".
Semantic release will only release on configured branches, so it is safe to run release on any branch.