Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Highlight2.js

100 lines2.4 KB

1import * as React from 'react';
2import Highlight, { defaultProps } from 'prism-react-renderer';
3// Original: https://raw.githubusercontent.com/PrismJS/prism-themes/master/themes/prism-ghcolors.css
4​
5/*:: import type { PrismTheme } from '../src/types' */
6const theme
7/*: PrismTheme */
8= {
9 plain: {
10 color: '#293742',
11 borderRadius: 12,
12 fontFamily: `SFMono-Regular,Consolas,Liberation Mono,Menlo,Courier,monospace`,
13 fontSize: 14,
14 lineHeight: '1.5'
15 },
16 styles: [{
17 types: ['comment', 'prolog', 'doctype', 'cdata'],
18 style: {
19 color: '#A7B6C2',
20 fontStyle: 'italic'
21 }
22 }, {
23 types: ['namespace'],
24 style: {
25 opacity: 0.7
26 }
27 }, {
28 types: ['string', 'attr-value'],
29 style: {
30 color: '#DB2C6F'
31 }
32 }, {
33 types: ['punctuation', 'operator'],
34 style: {
35 color: '#394B59'
36 }
37 }, {
38 types: ['entity', 'url', 'symbol', 'number', 'boolean', 'variable', 'constant', 'property', 'regex', 'inserted'],
39 style: {
40 color: '#36acaa'
41 }
42 }, {
43 types: ['atrule', 'keyword', 'attr-name', 'selector'],
44 style: {
45 color: '#00B3A4'
46 }
47 }, {
48 types: ['function', 'deleted', 'tag'],
49 style: {
50 color: '#DB2C6F'
51 }
52 }, {
53 types: ['function-variable'],
54 style: {
55 color: '#634DBF'
56 }
57 }, {
58 types: ['tag', 'selector', 'keyword'],
59 style: {
60 color: '#1a56db'
61 }
62 }]
63};
64​
65const Code = ({
66 children,
67 codeString,
68 className = 'language-js',
69 ...props
70}) => {
71 const language = className.replace(/language-/, '');
72 return <Highlight {...defaultProps} code={children.trim()} language={language} theme={theme}>
73 {({
74 className,
75 style,
76 tokens,
77 getLineProps,
78 getTokenProps
79 }) => <pre className={className + ' bg-gray-50 pb-4 pt-4 pr-4 overflow-scroll'} style={{ ...style,
80 border: '1px solid #eee',
81 fontSize: 13,
82 lineHeight: '1.5'
83 }}>
84 {tokens.map((line, i) => <div key={i} {...getLineProps({
85 line,
86 key: i
87 })}>
88 {tokens.length > 1 ? <span aria-hidden="true" className="select-none text-gray-300 text-right w-5 inline-block mx-2">
89 {i + 1}
90 </span> : <span className="mx-2 w-5" />}{' '}
91 {line.map((token, key) => <span key={key} {...getTokenProps({
92 token,
93 key
94 })} />)}
95 </div>)}
96 </pre>}
97 </Highlight>;
98};
99​
100export default Code;