-
Notifications
You must be signed in to change notification settings - Fork 113
fix: address review comments on PR #447 #480
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,39 +2,33 @@ | |||||||||||||||||||||||||||||
| "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 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></>; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| const escapeHtml = (text: string) => { | ||||||||||||||||||||||||||||||
| return text | ||||||||||||||||||||||||||||||
| .replace(/&/g, "&") | ||||||||||||||||||||||||||||||
| .replace(/</g, "<") | ||||||||||||||||||||||||||||||
| .replace(/>/g, ">") | ||||||||||||||||||||||||||||||
| .replace(/"/g, """) | ||||||||||||||||||||||||||||||
| .replace(/'/g, "'"); | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return <span dangerouslySetInnerHTML={{ __html: highlightCode(highlighted) }} />; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| const highlightCode = (code: string) => { | ||||||||||||||||||||||||||||||
| const escaped = escapeHtml(code); | ||||||||||||||||||||||||||||||
| return escaped | ||||||||||||||||||||||||||||||
| .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 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 highlight = (text: string) => { | ||||||||||||||||||||||||||||||
| if (text.includes('//')) { | ||||||||||||||||||||||||||||||
| const parts = text.split('//'); | ||||||||||||||||||||||||||||||
| return <><span dangerouslySetInnerHTML={{ __html: highlightCode(parts[0]) }} /><span className="text-slate-500 italic">{'//' + parts[1]}</span></>; | ||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Locate and display the relevant portion of the file
FILE="modules/home/code-line.tsx"
if [ ! -f "$FILE" ]; then
echo "Missing file: $FILE"
exit 1
fi
echo "---- $FILE (lines 1-120) ----"
nl -ba "$FILE" | sed -n '1,120p'
echo "---- $FILE (lines 18-40) ----"
nl -ba "$FILE" | sed -n '18,40p'
echo "---- $FILE: find split('//') usage ----"
rg -n "split\(['\"]//['\"]\)" "$FILE" || trueRepository: piyushdotcomm/Editron Length of output: 178 🏁 Script executed: #!/bin/bash
set -euo pipefail
FILE="modules/home/code-line.tsx"
if [ ! -f "$FILE" ]; then
echo "Missing file: $FILE"
exit 1
fi
echo "---- $FILE (lines 1-140) ----"
awk 'NR>=1 && NR<=140 { printf "%6d\t%s\n", NR, $0 }' "$FILE"
echo "---- $FILE: find split('//') usage ----"
rg -n "split\(['\"]//['\"]\)" "$FILE" || true
echo "---- $FILE: find text.split usages ----"
rg -n "text\.split\(" "$FILE" || trueRepository: piyushdotcomm/Editron Length of output: 1994 Fix
Proposed fix const highlight = (text: string) => {
if (text.includes('//')) {
- const parts = text.split('//');
- return <><span dangerouslySetInnerHTML={{ __html: highlightCode(parts[0]) }} /><span className="text-slate-500 italic">{'//' + parts[1]}</span></>;
+ const commentStart = text.indexOf('//');
+ const codePart = text.slice(0, commentStart);
+ const commentPart = text.slice(commentStart);
+ return (
+ <>
+ <span dangerouslySetInnerHTML={{ __html: highlightCode(codePart) }} />
+ <span className="text-slate-500 italic">{commentPart}</span>
+ </>
+ );
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.43.0)[warning] 25-25: Usage of dangerouslySetInnerHTML detected. This bypasses React's built-in XSS protection. Always sanitize HTML content using libraries like DOMPurify before injecting it into the DOM to prevent XSS attacks. (react-unsafe-html-injection) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return <span dangerouslySetInnerHTML={{ __html: highlightCode(text) }} />; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const [highlighted, setHighlighted] = React.useState<React.ReactNode>(line); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| React.useEffect(() => { | ||||||||||||||||||||||||||||||
| setHighlighted(highlight(line)); | ||||||||||||||||||||||||||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||||||||||||||||||||||||||
| }, [line]); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const CodeLine = ({ line }: { line: string }) => { | ||||||||||||||||||||||||||||||
| const highlighted = React.useMemo(() => highlight(line), [line]); | ||||||||||||||||||||||||||||||
| return highlighted; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: piyushdotcomm/Editron
Length of output: 258
🏁 Script executed:
Repository: piyushdotcomm/Editron
Length of output: 11559
Quote escaping breaks string token highlighting.
highlightCodeescapes the input viaescapeHtml(turning"into"and'into') before applying the string regexes (/'[^']*'/gand /"[^"]*"/g`), so quoted literals never match and don’t get highlighted.Proposed fix
const escapeHtml = (text: string) => { return text .replace(/&/g, "&") .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, """) - .replace(/'/g, "&`#039`;"); + .replace(/>/g, ">"); };Also applies to: 18-19
🤖 Prompt for AI Agents