Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions modules/home/code-line.tsx
Original file line number Diff line number Diff line change
@@ -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 <><span dangerouslySetInnerHTML={{ __html: highlightCode(parts[0]) }} /><span className="text-slate-500 italic">{'//' + parts[1]}</span></>;
export const CodeLine = ({ line }: { line: string }) => {
// Handle comments
if (line.trim().startsWith("//")) {
return <span className="text-slate-500 italic">{line}</span>;
}

const parts = line.split(/(\s+)/);

return (
<>
{parts.map((part, index) => {
if (KEYWORDS.includes(part)) {
return (
<span
key={index}
className="text-red-500 dark:text-red-400 font-semibold"
>
{part}
</span>
);
}

return <span dangerouslySetInnerHTML={{ __html: highlightCode(highlighted) }} />;
};

const highlightCode = (code: string) => {
return code
.replace(/import|from|export|default|return|const|new/g, '<span class="text-red-500 dark:text-red-400 font-semibold">$&</span>')
.replace(/'[^']*'/g, '<span class="text-amber-600 dark:text-amber-400">$&</span>')
.replace(/"[^"]*"/g, '<span class="text-amber-600 dark:text-amber-400">$&</span>')
.replace(/Editron|console|editor/g, '<span class="text-rose-600 dark:text-rose-400">$&</span>');
}

const [highlighted, setHighlighted] = React.useState<React.ReactNode>(line);
if (
(part.startsWith("'") && part.endsWith("'")) ||
(part.startsWith('"') && part.endsWith('"'))
) {
return (
<span key={index} className="text-amber-600 dark:text-amber-400">
{part}
</span>
);
}

React.useEffect(() => {
setHighlighted(highlight(line));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [line]);
if (["Editron", "editor", "console"].includes(part)) {
return (
<span key={index} className="text-rose-600 dark:text-rose-400">
{part}
</span>
);
}
Comment thread
HimasreeKolathur24 marked this conversation as resolved.

return highlighted;
return <span key={index}>{part}</span>;
})}
</>
);
};
2 changes: 1 addition & 1 deletion modules/home/hero-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const editor = new Editron({
mode: 'pro',
ai: {
enabled: true,
model: 'claude-3-opus'
model: 'deepseek-chat'
},
theme: 'midnight-monochrome'
});
Expand Down
Loading