-
Notifications
You must be signed in to change notification settings - Fork 159
Add GLSL syntax highlighting in code examples #455
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import "prismjs/components/prism-typescript"; | |
| import "prismjs/components/prism-jsx"; | ||
| import "prismjs/components/prism-tsx"; | ||
| import "prismjs/themes/prism-tomorrow.css"; | ||
| import "../utils/prismGlslTemplate"; | ||
|
||
| import { examples } from "../examples"; | ||
| import { ControlsPanel, getDefaults } from "../controls"; | ||
| import { Breadcrumb } from "../components/Breadcrumb"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import Prism from "prismjs"; | ||
| import "prismjs/components/prism-c"; | ||
| import "prismjs/components/prism-glsl"; | ||
|
|
||
| // Highlight GLSL inside tagged template literals (GLSL`...`) | ||
| Prism.hooks.add("after-tokenize", (env) => { | ||
| if (env.language !== "tsx" && env.language !== "jsx") return; | ||
| walkTokens(env.tokens); | ||
| }); | ||
|
|
||
| function walkTokens(tokens: (string | Prism.Token)[]) { | ||
| for (let i = 0; i < tokens.length; i++) { | ||
| const token = tokens[i]; | ||
| if (typeof token === "string") continue; | ||
| if (token.type === "template-string") { | ||
| tryHighlightGlsl(token); | ||
| } | ||
| if (Array.isArray(token.content)) { | ||
| walkTokens(token.content); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function tryHighlightGlsl(token: Prism.Token) { | ||
| const content = token.content; | ||
| if (!Array.isArray(content)) return; | ||
|
|
||
| // Find the string part inside the template-string | ||
| for (let i = 0; i < content.length; i++) { | ||
| const part = content[i]; | ||
| if (typeof part !== "string" && part.type === "string" && typeof part.content === "string") { | ||
| // Heuristic: check if it looks like GLSL | ||
| if (!/void\s+main\s*\(|gl_FragColor|precision\s+\w+p\s+float|out\s+vec4/.test(part.content)) continue; | ||
| // Re-tokenize as GLSL and mark the token | ||
| part.content = Prism.tokenize(part.content, Prism.languages.glsl); | ||
| part.type = "glsl-block"; | ||
|
Comment on lines
+15
to
+36
|
||
| } | ||
| } | ||
| } | ||
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.
The
margin: 0 -1rem/padding: 0.25rem 1remstyling hard-codes an assumed code-block horizontal padding of 1rem. This is brittle if code blocks are ever rendered with different padding (it can cause misalignment or unexpected horizontal overflow). Consider scoping this rule to the specific code container that usesp-4, or using a CSS custom property so the background “full-width” effect tracks the actual container padding.