diff --git a/modules/home/code-line.tsx b/modules/home/code-line.tsx index 06da7478..4f485a2f 100644 --- a/modules/home/code-line.tsx +++ b/modules/home/code-line.tsx @@ -1,40 +1,61 @@ - "use client"; -import React from 'react'; - -export const CodeLine = ({ line }: { line: string }) => { - // Basic replacements to simulate syntax highlighting - // Note: This is a very simplistic implementation and should be replaced with a proper library like prismjs or shiki in production - const highlight = (text: string) => { - // We use a series of replacements. Order matters to avoid replacing inside already replaced spans. - // A better approach for robust highlighting is tokenization. +const KEYWORDS = [ + "import", + "from", + "export", + "default", + "return", + "const", + "new", + "function", + "true", + "false", +]; - const highlighted = text; - - // Comments (simple // for now) - if (highlighted.includes('//')) { - const parts = highlighted.split('//'); - return <>{'//' + parts[1]}; +export const CodeLine = ({ line }: { line: string }) => { + // Handle comments + if (line.trim().startsWith("//")) { + return {line}; + } + + const parts = line.split(/(\s+)/); + + return ( + <> + {parts.map((part, index) => { + if (KEYWORDS.includes(part)) { + return ( + + {part} + + ); } - return ; - }; - - const highlightCode = (code: string) => { - return code - .replace(/import|from|export|default|return|const|new/g, '$&') - .replace(/'[^']*'/g, '$&') - .replace(/"[^"]*"/g, '$&') - .replace(/Editron|console|editor/g, '$&'); - } - - const [highlighted, setHighlighted] = React.useState(line); + if ( + (part.startsWith("'") && part.endsWith("'")) || + (part.startsWith('"') && part.endsWith('"')) + ) { + return ( + + {part} + + ); + } - React.useEffect(() => { - setHighlighted(highlight(line)); -// eslint-disable-next-line react-hooks/exhaustive-deps - }, [line]); + if (["Editron", "editor", "console"].includes(part)) { + return ( + + {part} + + ); + } - return highlighted; + return {part}; + })} + + ); };