`
32 |
33 | | Property | Type | Required |
34 | | ---------- | --------------------------------------------------------------------------------------------- | -------- |
35 | | `code` | `String` | Yes |
36 | | `language` | `"css" \| "html" \| "markdown" \| "javascript" \| "json" \| "jsx" \| "php" \| "text" \| "sh"` | Yes |
37 |
38 | ```jsx
39 | import { Code } from 'sanity-web-react-components'
40 |
41 | const vdom =
42 | ```
43 |
44 | ## `20 |22 |21 |
35 |37 | ) 38 | } 39 | 40 | export default Code 41 | -------------------------------------------------------------------------------- /src/code/Lowlight.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import * as ReactLowlight from 'react-lowlight' 3 | import { decodeHTMLEntities } from '../lib/stringUtils' 4 | 5 | // Import languages 6 | import * as cssLang from 'highlight.js/lib/languages/css' 7 | import * as bashLang from 'highlight.js/lib/languages/bash' 8 | import * as xmlLang from 'highlight.js/lib/languages/xml' 9 | import * as markdownLang from 'highlight.js/lib/languages/markdown' 10 | import * as jsLang from 'highlight.js/lib/languages/javascript' 11 | import * as jsonLang from 'highlight.js/lib/languages/json' 12 | import * as phpLang from 'highlight.js/lib/languages/php' 13 | import groqLang from './languages/groq' 14 | 15 | ReactLowlight.registerLanguage('css', cssLang) 16 | ReactLowlight.registerLanguage('groq', groqLang) 17 | ReactLowlight.registerLanguage('html', xmlLang) 18 | ReactLowlight.registerLanguage('markdown', markdownLang) 19 | ReactLowlight.registerLanguage('javascript', jsLang) 20 | ReactLowlight.registerLanguage('json', jsonLang) 21 | ReactLowlight.registerLanguage('jsx', jsLang) 22 | ReactLowlight.registerLanguage('php', phpLang) 23 | ReactLowlight.registerLanguage('sh', bashLang) 24 | 25 | interface Props { 26 | className?: string 27 | code: string 28 | inline?: boolean 29 | language?: 30 | | 'css' 31 | | 'groq' 32 | | 'html' 33 | | 'markdown' 34 | | 'javascript' 35 | | 'json' 36 | | 'jsx' 37 | | 'php' 38 | | 'text' 39 | | 'sh' 40 | } 41 | 42 | const supportedLangs = ['css', 'groq', 'html', 'markdown', 'javascript', 'json', 'jsx', 'php', 'sh'] 43 | 44 | function Lowlight(props: Props) { 45 | const code = decodeHTMLEntities(props.code || '') 46 | 47 | if (supportedLangs.indexOf(props.language as any) === -1) { 48 | if (props.language !== 'text') { 49 | console.warn(`Code language not supported: ${props.language}`) 50 | } 51 | if (props.inline) { 52 | return36 |
{code}
53 | }
54 | return (
55 |
56 | {code}
57 |
58 | )
59 | }
60 |
61 | return (
62 |
31 |
48 | 97 | Or create an account 98 |
99 | )} 100 |{description}
22 |