-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
375 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@code-hike/lighter": patch | ||
--- | ||
|
||
Add terminal highlighting |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
|
@@ -180,6 +180,7 @@ export const LANG_NAMES = [ | |
"system-verilog", | ||
"tasl", | ||
"tcl", | ||
"terminal", | ||
"tex", | ||
"text", | ||
"toml", | ||
|
This file contains 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,80 @@ | ||
import { FinalTheme } from "./theme"; | ||
import { Token } from "./annotations"; | ||
import { Color, createAnsiSequenceParser } from "ansi-sequence-parser"; | ||
import { getColor } from "./theme-colors"; | ||
|
||
export function highlightTerminal(code: string, theme: FinalTheme): Token[][] { | ||
const parser = createAnsiSequenceParser(); | ||
const lines = code | ||
.split(/\r?\n|\r/g) | ||
.map((line) => highlightLine(parser, line, theme)); | ||
|
||
return lines; | ||
} | ||
|
||
function highlightLine( | ||
parser: ReturnType<typeof createAnsiSequenceParser>, | ||
line: string, | ||
theme: FinalTheme | ||
) { | ||
const ansiLine = parser.parse(line); | ||
const tokens = ansiLine.map( | ||
({ value, foreground, background, decorations }) => { | ||
const color = getAnsiColor(foreground, theme); | ||
const style = {}; | ||
if (color) { | ||
style["color"] = color; | ||
} | ||
const backgroundColor = getAnsiColor(background, theme); | ||
if (backgroundColor) { | ||
style["background"] = backgroundColor; | ||
} | ||
if (decorations.has("bold")) { | ||
style["fontWeight"] = "bold"; | ||
} | ||
if (decorations.has("italic")) { | ||
style["fontStyle"] = "italic"; | ||
} | ||
if (decorations.has("underline")) { | ||
style["textDecoration"] = "underline"; | ||
} | ||
if (decorations.has("strikethrough")) { | ||
style["textDecoration"] = "line-through"; | ||
} | ||
if (decorations.has("reverse")) { | ||
style["color"] = backgroundColor; | ||
style["background"] = color; | ||
} | ||
if (decorations.has("dim")) { | ||
style["opacity"] = 0.5; | ||
} | ||
|
||
return { | ||
content: value, | ||
style, | ||
}; | ||
} | ||
); | ||
return tokens; | ||
} | ||
|
||
function getAnsiColor(color: Color | null, theme: FinalTheme) { | ||
if (!color) return undefined; | ||
if (color.type === "named") { | ||
// capitalize name | ||
const name = color.name[0].toUpperCase() + color.name.slice(1); | ||
return getColor(theme, "terminal.ansi" + name); | ||
} | ||
if (color.type === "rgb") { | ||
const [r, g, b] = color.rgb; | ||
return `rgb(${r}, ${g}, ${b})`; | ||
} | ||
return undefined; | ||
} | ||
|
||
export function getTerminalStyle(theme: FinalTheme) { | ||
return { | ||
color: getColor(theme, "terminal.foreground"), | ||
background: getColor(theme, "terminal.background"), | ||
}; | ||
} |
This file contains 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 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 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
Oops, something went wrong.
8ceee96
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.
Successfully deployed to the following URLs:
lighter – ./
lighter-codehike.vercel.app
lighter.codehike.org
lighter-git-main-codehike.vercel.app