Skip to content

fix: resolve hero code demo syntax highlighting issue#486

Open
HimasreeKolathur24 wants to merge 1 commit into
piyushdotcomm:mainfrom
HimasreeKolathur24:fix-hero-code-demo-highlighting
Open

fix: resolve hero code demo syntax highlighting issue#486
HimasreeKolathur24 wants to merge 1 commit into
piyushdotcomm:mainfrom
HimasreeKolathur24:fix-hero-code-demo-highlighting

Conversation

@HimasreeKolathur24

@HimasreeKolathur24 HimasreeKolathur24 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixed a rendering issue in the Hero Code Demo where Tailwind utility classes were displayed as visible text inside the code block.

Changes Made

  • Replaced HTML-string based syntax highlighting with React element rendering.
  • Removed the use of dangerouslySetInnerHTML.
  • Preserved syntax highlighting for keywords, strings, comments, and identifiers.
  • Improved code readability and maintainability.

Type of change

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Test or CI improvement
  • Starter template change

Related issue

Closes #485

Validation

  • Manual verification

List any additional manual verification you performed:

  • Verified the Hero Code Demo renders correctly.
  • Confirmed Tailwind utility classes no longer appear in the UI.
  • Verified syntax highlighting for keywords, strings, and comments.
  • Tested the homepage after rebuilding.

Screenshots or recordings

#Before
Screenshot 2026-06-09 110646

#After Fix
Screenshot 2026-06-09 122008

Checklist

  • I kept this PR focused on one primary change
  • I did not commit secrets, local logs, or scratch files
  • I am requesting review on the correct scope

Summary by CodeRabbit

  • Refactor
    • Improved code syntax highlighting implementation with more reliable rendering
    • Enhanced detection and proper styling of comment lines
    • Better tokenization of code segments for improved display accuracy

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@github-actions github-actions Bot added bug Something isn't working gssoc26 level:intermediate labels Jun 9, 2026
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

👋 Thanks for opening a PR, @HimasreeKolathur24!

Your PR has entered the 🚦 PR Review Pipeline.

Standard PR detected — your PR will follow the standard review pipeline.


What happens next

Stage Reviewer Checks
Stage 1 — Automated Validation 🤖 Bot DCO · Format · AI/Slop · Duplicate
Stage 2 — Human Review 👥 Maintainer Code + Quality Review
Stage 3 — PA / Maintainer Review 🔑 Project Admin Final Merge Decision

A pipeline status comment will appear below and update automatically as your PR progresses.


While you wait

  • Sign all commits (git commit -s)
  • Link your issue (Closes #123)
  • Use a feature branch (not main)
  • Avoid unrelated changes

This comment is posted only once.

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The CodeLine component replaces unsafe HTML injection with direct JSX tokenization. Code lines are split by whitespace, then tokens are checked against keyword/string/identifier patterns and wrapped in styled spans, eliminating React state and effects while fixing syntax highlighting.

Changes

Syntax Highlighting Refactor

Layer / File(s) Summary
CodeLine component refactoring: regex injection to tokenization
modules/home/code-line.tsx
Removes dangerouslySetInnerHTML, regex string substitutions, and React state/effect hooks. Implements direct tokenization via line.split(/(\s+)/) with conditional JSX wrapping for keywords, quoted strings, and identifiers (Editron, editor, console). Adds early return for lines starting with // comments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

ui-ux, area: ui, mentor:piyushdotcomm, quality:exceptional, gssoc-review

Suggested reviewers

  • piyushdotcomm

Poem

🐰 A token splits the line with care,
No HTML string floating bare,
Keywords sparkle, strings now glow,
JSX spans steal the show,
Safety wins where regexy'd go! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: fixing the hero code demo syntax highlighting issue.
Description check ✅ Passed The description includes all required template sections with comprehensive details, validation steps, and supporting screenshots.
Linked Issues check ✅ Passed The code changes fully address the objectives from issue #485 by replacing HTML-string syntax highlighting with React element rendering and removing dangerouslySetInnerHTML.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the Hero Code Demo component; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@modules/home/code-line.tsx`:
- Around line 22-55: The current tokenization uses line.split(/(\s+)/) which
keeps punctuation attached (e.g., "Editron({", "editor.launch();",
"'`@editron/core`';") so KEYWORDS, string checks and the special identifier check
(["Editron","editor","console"]) miss matches; update tokenization in the parts
creation (replace the line.split call) to use a regex that separately captures
quoted strings, identifiers, and punctuation OR normalize each part before
matching (trim surrounding punctuation like (){};.,) so comparisons against
KEYWORDS, the string-start/end checks, and the identifier list succeed; ensure
this change is applied where parts is defined and that parts.map logic (the
KEYWORDS check, the string literal check, and the ["Editron","editor","console"]
check) uses the normalized token for comparisons while still rendering the
original part so spacing/punctuation is preserved.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e1f74d2a-34ae-4142-86f1-488eddea8d72

📥 Commits

Reviewing files that changed from the base of the PR and between f12b223 and 9432190.

📒 Files selected for processing (1)
  • modules/home/code-line.tsx

Comment on lines +22 to +55
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>
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

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 in HeroCodeDemo.

Suggested fix (tokenize strings/identifiers/punctuation before classification)
-  const parts = line.split(/(\s+)/);
+  const parts =
+    line.match(
+      /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\s+|[A-Za-z_$][\w$]*|[^\s]/g
+    ) ?? [line];

   return (
     <>
       {parts.map((part, index) => {
         if (KEYWORDS.includes(part)) {
@@
-        if (
-          (part.startsWith("'") && part.endsWith("'")) ||
-          (part.startsWith('"') && part.endsWith('"'))
-        ) {
+        if (
+          (part.startsWith("'") && part.endsWith("'")) ||
+          (part.startsWith('"') && part.endsWith('"'))
+        ) {
           return (
             <span key={index} className="text-amber-600 dark:text-amber-400">
               {part}
             </span>
           );
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modules/home/code-line.tsx` around lines 22 - 55, The current tokenization
uses line.split(/(\s+)/) which keeps punctuation attached (e.g., "Editron({",
"editor.launch();", "'`@editron/core`';") so KEYWORDS, string checks and the
special identifier check (["Editron","editor","console"]) miss matches; update
tokenization in the parts creation (replace the line.split call) to use a regex
that separately captures quoted strings, identifiers, and punctuation OR
normalize each part before matching (trim surrounding punctuation like (){};.,)
so comparisons against KEYWORDS, the string-start/end checks, and the identifier
list succeed; ensure this change is applied where parts is defined and that
parts.map logic (the KEYWORDS check, the string literal check, and the
["Editron","editor","console"] check) uses the normalized token for comparisons
while still rendering the original part so spacing/punctuation is preserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Hero Code Demo displays Tailwind utility classes as visible text

1 participant