(props: CodePanelProps)
| 26 | } |
| 27 | |
| 28 | const CodePanel = (props: CodePanelProps) => { |
| 29 | const [syntaxHovered, setSyntaxHovered] = useState(false); |
| 30 | const [isExpanded, setIsExpanded] = useState(false); |
| 31 | const initialLinesToShow = 25; |
| 32 | const { |
| 33 | code, |
| 34 | preferenceOptions, |
| 35 | selectPreferenceOptions, |
| 36 | selectedFramework, |
| 37 | settings, |
| 38 | onPreferenceChanged, |
| 39 | } = props; |
| 40 | const isCodeEmpty = code === ""; |
| 41 | |
| 42 | // Helper function to add the prefix before every class (or className) in the code. |
| 43 | // It finds every occurrence of class="..." or className="..." and, for each class, |
| 44 | // prepends the custom prefix. |
| 45 | const applyPrefixToClasses = ( |
| 46 | codeString: string, |
| 47 | prefix: string | undefined, |
| 48 | ) => { |
| 49 | if (!prefix) { |
| 50 | return codeString; |
| 51 | } |
| 52 | |
| 53 | return codeString.replace( |
| 54 | /(class(?:Name)?)="([^"]*)"/g, |
| 55 | (match, attr, classes) => { |
| 56 | const prefixedClasses = classes |
| 57 | .split(/\s+/) |
| 58 | .filter(Boolean) |
| 59 | .map((cls: string) => prefix + cls) |
| 60 | .join(" "); |
| 61 | return `${attr}="${prefixedClasses}"`; |
| 62 | }, |
| 63 | ); |
| 64 | }; |
| 65 | |
| 66 | // Function to truncate code to a specific number of lines |
| 67 | const truncateCode = (codeString: string, lines: number) => { |
| 68 | const codeLines = codeString.split("\n"); |
| 69 | if (codeLines.length <= lines) { |
| 70 | return codeString; |
| 71 | } |
| 72 | return codeLines.slice(0, lines).join("\n") + "\n..."; |
| 73 | }; |
| 74 | |
| 75 | // If the selected framework is Tailwind and a prefix is provided then transform the code. |
| 76 | const prefixedCode = |
| 77 | selectedFramework === "Tailwind" && |
| 78 | settings?.customTailwindPrefix?.trim() !== "" |
| 79 | ? applyPrefixToClasses(code, settings?.customTailwindPrefix) |
| 80 | : code; |
| 81 | |
| 82 | // Memoize the line count calculation to improve performance for large code blocks |
| 83 | const lineCount = useMemo( |
| 84 | () => prefixedCode.split("\n").length, |
| 85 | [prefixedCode], |
nothing calls this directly
no test coverage detected