|
| 1 | +import Prism from 'prismjs'; |
| 2 | +import { type JSX, useLayoutEffect, useRef } from 'react'; |
| 3 | + |
| 4 | +import { IconButton } from './IconButton'; |
| 5 | + |
| 6 | +// Import languages based on what you need |
| 7 | +import 'prismjs/components/prism-apex'; |
| 8 | +import 'prismjs/components/prism-bash'; |
| 9 | +import 'prismjs/components/prism-brightscript'; |
| 10 | +import 'prismjs/components/prism-c'; |
| 11 | +import 'prismjs/components/prism-clike'; |
| 12 | +import 'prismjs/components/prism-cpp'; |
| 13 | +import 'prismjs/components/prism-csharp'; |
| 14 | +import 'prismjs/components/prism-erlang'; |
| 15 | +import 'prismjs/components/prism-go'; |
| 16 | +import 'prismjs/components/prism-gradle'; |
| 17 | +import 'prismjs/components/prism-haskell'; |
| 18 | +import 'prismjs/components/prism-java'; |
| 19 | +import 'prismjs/components/prism-javascript'; |
| 20 | +import 'prismjs/components/prism-json'; |
| 21 | +import 'prismjs/components/prism-jsx'; |
| 22 | +import 'prismjs/components/prism-kotlin'; |
| 23 | +import 'prismjs/components/prism-lua'; |
| 24 | +import 'prismjs/components/prism-makefile'; |
| 25 | +import 'prismjs/components/prism-markup'; |
| 26 | +import 'prismjs/components/prism-markup-templating'; |
| 27 | +import 'prismjs/components/prism-objectivec'; |
| 28 | +import 'prismjs/components/prism-php'; |
| 29 | +import 'prismjs/components/prism-powershell'; |
| 30 | +import 'prismjs/components/prism-python'; |
| 31 | +import 'prismjs/components/prism-ruby'; |
| 32 | +import 'prismjs/components/prism-rust'; |
| 33 | +import 'prismjs/components/prism-sql'; |
| 34 | +import 'prismjs/components/prism-swift'; |
| 35 | +import 'prismjs/components/prism-tsx'; |
| 36 | +import 'prismjs/components/prism-typescript'; |
| 37 | +import 'prismjs/components/prism-yaml'; |
| 38 | +// Import plugins |
| 39 | +import 'prismjs/plugins/keep-markup/prism-keep-markup'; |
| 40 | +import 'prismjs/plugins/line-highlight/prism-line-highlight'; |
| 41 | +import 'prismjs/plugins/line-numbers/prism-line-numbers'; |
| 42 | + |
| 43 | +import { CopyToClipboard } from './CopyToClipboard'; |
| 44 | + |
| 45 | +import 'prismjs/plugins/line-numbers/prism-line-numbers.css'; |
| 46 | + |
| 47 | +import styles from './styles/Snippet.module.css'; |
| 48 | + |
| 49 | +export const languages = [ |
| 50 | + 'bash', |
| 51 | + 'shell', |
| 52 | + 'json', |
| 53 | + 'html', |
| 54 | + 'xml', |
| 55 | + 'js', |
| 56 | + 'javascript', |
| 57 | + 'lua', |
| 58 | + 'ts', |
| 59 | + 'typescript', |
| 60 | + 'php', |
| 61 | + 'java', |
| 62 | + 'ruby', |
| 63 | + 'python', |
| 64 | + 'go', |
| 65 | + 'csharp', |
| 66 | + 'c', |
| 67 | + 'cpp', |
| 68 | + 'objectivec', |
| 69 | + 'swift', |
| 70 | + 'makefile', |
| 71 | + 'haskell', |
| 72 | + 'brightscript', |
| 73 | + 'dart', |
| 74 | + 'rust', |
| 75 | + 'tsx', |
| 76 | + 'gradle', |
| 77 | + 'powershell', |
| 78 | + 'kotlin', |
| 79 | + 'erlang', |
| 80 | + 'yaml', |
| 81 | + 'apex', |
| 82 | + // text and empty string are not a recognized languages by prism, we use it here as a default option for when you don't want styling. |
| 83 | + 'text', |
| 84 | + '', |
| 85 | +] as const; |
| 86 | + |
| 87 | +export type SnippetLang = (typeof languages)[number]; |
| 88 | + |
| 89 | +type SnippetProps = { |
| 90 | + children: string | JSX.Element; |
| 91 | + className?: string; |
| 92 | + highlightRange?: string; |
| 93 | + highlightOffset?: number; |
| 94 | + lang: SnippetLang; |
| 95 | + label?: string; |
| 96 | + withHeader?: boolean; |
| 97 | + withLineNumbers?: boolean; |
| 98 | + useDefaultHighlighting?: boolean; |
| 99 | + withCopyButton?: boolean; |
| 100 | + trackAnalyticsOnClick?: () => void; |
| 101 | +}; |
| 102 | + |
| 103 | +// Example usage: |
| 104 | +// |
| 105 | +// const json = JSON.stringify({ |
| 106 | + |
| 107 | +// 'ip': '192.168.0.1', |
| 108 | +// 'custom': { |
| 109 | +// 'customer_ranking': 10004 |
| 110 | +// } |
| 111 | +// }, null, 2); |
| 112 | +// |
| 113 | +// <Snippet withCopyButton={true} lang="json">{json}</Snippet> |
| 114 | +export function Snippet({ |
| 115 | + children, |
| 116 | + className, |
| 117 | + highlightRange, |
| 118 | + highlightOffset, |
| 119 | + lang, |
| 120 | + label, |
| 121 | + withHeader, |
| 122 | + withLineNumbers, |
| 123 | + useDefaultHighlighting = false, |
| 124 | + withCopyButton, |
| 125 | + trackAnalyticsOnClick, |
| 126 | +}: SnippetProps) { |
| 127 | + const codeEl = useRef<HTMLElement>(null); |
| 128 | + |
| 129 | + // biome-ignore lint/correctness/useExhaustiveDependencies: children and lang are intentionally included to re-highlight when they change |
| 130 | + useLayoutEffect(() => { |
| 131 | + const element = codeEl.current; |
| 132 | + if (!element) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // Use requestAnimationFrame to ensure that the element is mounted |
| 137 | + // before highlighting it. |
| 138 | + const frame = requestAnimationFrame(() => { |
| 139 | + Prism.highlightElement(element); |
| 140 | + }); |
| 141 | + |
| 142 | + // Cancel the animation frame when the component unmounts. |
| 143 | + return () => cancelAnimationFrame(frame); |
| 144 | + }, [children, lang]); |
| 145 | + |
| 146 | + return ( |
| 147 | + <> |
| 148 | + {withHeader && ( |
| 149 | + <div className={styles.header}> |
| 150 | + {label && <span>{label}</span>} |
| 151 | + {lang && <span>{lang}</span>} |
| 152 | + </div> |
| 153 | + )} |
| 154 | + <div |
| 155 | + className={`${styles.snippet} ${className ?? ''} ${withCopyButton ? styles.copyable : ''} ${useDefaultHighlighting ? styles.useDefaultHighlighting : ''}`} |
| 156 | + > |
| 157 | + <pre |
| 158 | + className={withLineNumbers ? styles['line-numbers'] : ''} |
| 159 | + data-start={1} |
| 160 | + data-line-offset={highlightOffset ? highlightOffset.toString() : ''} |
| 161 | + data-line={highlightRange} |
| 162 | + > |
| 163 | + <code className={`language-${lang}`} ref={codeEl}> |
| 164 | + {children} |
| 165 | + </code> |
| 166 | + {withCopyButton && ( |
| 167 | + <CopyToClipboard text={children as string} showTooltip={false}> |
| 168 | + <IconButton |
| 169 | + className={styles.copyButton} |
| 170 | + aria-label="Copy code snippet" |
| 171 | + variant="minimal" |
| 172 | + icon="copy-code" |
| 173 | + onPress={trackAnalyticsOnClick} |
| 174 | + /> |
| 175 | + </CopyToClipboard> |
| 176 | + )} |
| 177 | + </pre> |
| 178 | + </div> |
| 179 | + </> |
| 180 | + ); |
| 181 | +} |
0 commit comments