-
Notifications
You must be signed in to change notification settings - Fork 9
feat(address): add Vyper smart contract support and syntax highlighting #331
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
Merged
Merged
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { useMemo } from "react"; | ||
| import Prism from "prismjs"; | ||
| import "prismjs/components/prism-python"; | ||
| import "prismjs/components/prism-solidity"; | ||
|
|
||
| function detectLanguage(fileName?: string): string | undefined { | ||
| if (!fileName) return undefined; | ||
| const ext = fileName.split(".").pop()?.toLowerCase(); | ||
| if (ext === "sol") return "solidity"; | ||
| if (ext === "vy") return "python"; | ||
| if (ext === "json") return "json"; | ||
| return undefined; | ||
| } | ||
|
|
||
| interface CodeBlockProps { | ||
| code: string; | ||
| fileName?: string; | ||
| language?: string; | ||
| } | ||
|
|
||
| const CodeBlock: React.FC<CodeBlockProps> = ({ code, fileName, language }) => { | ||
| const lang = language ?? detectLanguage(fileName); | ||
| const grammar = lang ? Prism.languages[lang] : undefined; | ||
|
|
||
| const highlighted = useMemo(() => { | ||
| if (!grammar || !lang) return null; | ||
| return Prism.highlight(code, grammar, lang); | ||
| }, [code, grammar, lang]); | ||
|
|
||
| if (highlighted) { | ||
| return ( | ||
| <pre className="source-file-code"> | ||
| {/* biome-ignore lint/security/noDangerouslySetInnerHtml: Prism.highlight output is safe — it only tokenizes source code we control */} | ||
| <code className={`language-${lang}`} dangerouslySetInnerHTML={{ __html: highlighted }} /> | ||
| </pre> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <pre className="source-file-code"> | ||
| <code>{code}</code> | ||
| </pre> | ||
| ); | ||
| }; | ||
|
|
||
| export default CodeBlock; | ||
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* Prism.js syntax highlighting theme for OpenScan */ | ||
| .source-file-code code[class*="language-"] { | ||
| text-shadow: none; | ||
| } | ||
|
|
||
| .source-file-code .token.comment, | ||
| .source-file-code .token.prolog, | ||
| .source-file-code .token.doctype, | ||
| .source-file-code .token.cdata { | ||
| color: #6a9955; | ||
| } | ||
|
|
||
| .source-file-code .token.punctuation { | ||
| color: #d4d4d4; | ||
| } | ||
|
|
||
| .source-file-code .token.property, | ||
| .source-file-code .token.tag, | ||
| .source-file-code .token.boolean, | ||
| .source-file-code .token.number, | ||
| .source-file-code .token.constant, | ||
| .source-file-code .token.symbol { | ||
| color: #b5cea8; | ||
| } | ||
|
|
||
| .source-file-code .token.selector, | ||
| .source-file-code .token.attr-name, | ||
| .source-file-code .token.string, | ||
| .source-file-code .token.char, | ||
| .source-file-code .token.builtin { | ||
| color: #ce9178; | ||
| } | ||
|
|
||
| .source-file-code .token.operator, | ||
| .source-file-code .token.entity, | ||
| .source-file-code .token.url { | ||
| color: #d4d4d4; | ||
| } | ||
|
|
||
| .source-file-code .token.atrule, | ||
| .source-file-code .token.attr-value, | ||
| .source-file-code .token.keyword { | ||
| color: #569cd6; | ||
| } | ||
|
|
||
| .source-file-code .token.function, | ||
| .source-file-code .token.class-name { | ||
| color: #dcdcaa; | ||
| } | ||
|
|
||
| .source-file-code .token.regex, | ||
| .source-file-code .token.important, | ||
| .source-file-code .token.variable { | ||
| color: #d16969; | ||
| } | ||
|
|
||
| .source-file-code .token.decorator { | ||
| color: #dcdcaa; | ||
| } |
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.
Who are we trusting here?
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.
We're trusting Prism.js (the syntax highlighter library). Prism.highlight() takes plain text, tokenizes it using grammar rules, and
<script>alert('xss')</script>, Prism outputs <script>alert('xss')</script> wrapped in token spans. The source code itself (fromreturns HTML with wrappers. It HTML-escapes the input text before wrapping — so even if the source code contains
Sourcify/Etherscan) is never injected as raw HTML — only Prism's deterministic markup is.
The XSS risk with dangerouslySetInnerHTML is when you inject untrusted HTML. Prism's output is deterministic markup based on its grammar
rules — even if the source code contained <script> tags, Prism would escape them into <script> tokens. So this is a safe and standard
pattern for syntax highlighting in React.