-
Notifications
You must be signed in to change notification settings - Fork 113
fix: resolve hero code demo syntax highlighting issue #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HimasreeKolathur24
wants to merge
1
commit into
piyushdotcomm:main
Choose a base branch
from
HimasreeKolathur24:fix-hero-code-demo-highlighting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+53
−32
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| ); | ||
| } | ||
|
|
||
| return highlighted; | ||
| return <span key={index}>{part}</span>; | ||
| })} | ||
| </> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tokenization currently breaks highlighting for real snippet tokens.
On Line 22, splitting by whitespace causes punctuation-attached tokens (
Editron({,editor.launch();,'@editron/core';) and multi-word strings ("Ready to build something amazing.") to miss the checks on Lines 38-55. This regresses expected syntax highlighting inHeroCodeDemo.Suggested fix (tokenize strings/identifiers/punctuation before classification)
🤖 Prompt for AI Agents