The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | import { highlightTS } from "./highlightTS"; |
| 3 | |
| 4 | interface Props { |
| 5 | filename?: string; |
| 6 | code: string; |
| 7 | compact?: boolean; |
| 8 | caption?: string; |
| 9 | /** 1-based, inclusive line ranges to highlight. */ |
| 10 | highlight?: [number, number][]; |
| 11 | } |
| 12 | |
| 13 | const { |
| 14 | filename = "alchemy.run.ts", |
| 15 | code, |
| 16 | compact, |
| 17 | caption, |
| 18 | highlight, |
| 19 | } = Astro.props; |
| 20 | // With highlights, each line is highlighted on its own (snippets that use |
| 21 | // this keep strings and comments on one line) and wrapped in a row. |
| 22 | const html = highlight |
| 23 | ? code |
| 24 | .split("\n") |
| 25 | .map((line, i) => { |
| 26 | const lit = highlight.some(([a, b]) => i + 1 >= a && i + 1 <= b); |
| 27 | return `<span class="alc-code-block__line${lit ? " is-lit" : ""}">${highlightTS(line) || " "}</span>`; |
| 28 | }) |
| 29 | .join("") |
| 30 | : highlightTS(code); |
| 31 | --- |
| 32 | |
| 33 | { |
| 34 | caption ? ( |
| 35 | <div class="alc-code-block__wrap"> |
| 36 | <div class:list={["alc-code-block", compact && "alc-code-block--compact"]}> |
| 37 | <div class="alc-code-block__header"> |
| 38 | <span |
| 39 | class="alc-code-block__dot" |
| 40 | style="background:var(--alc-danger);" |
| 41 | /> |
| 42 | <span |
| 43 | class="alc-code-block__dot" |
| 44 | style="background:var(--alc-warn);" |
| 45 | /> |
| 46 | <span |
| 47 | class="alc-code-block__dot" |
| 48 | style="background:var(--alc-accent-bright);" |
| 49 | /> |
| 50 | <span class="alc-code-block__filename">{filename}</span> |
| 51 | </div> |
| 52 | <pre |
| 53 | class="alc-code-block__pre" |
| 54 | tabindex="0" |
| 55 | aria-label={`${filename} source`} |
| 56 | set:html={html} |
| 57 | /> |
| 58 | </div> |
| 59 | <div class="alc-code-block__caption">{caption}</div> |
| 60 | </div> |
| 61 | ) : ( |
| 62 | <div class:list={["alc-code-block", compact && "alc-code-block--compact"]}> |
| 63 | <div class="alc-code-block__header"> |
| 64 | <span |
| 65 | class="alc-code-block__dot" |
| 66 | style="background:var(--alc-danger);" |
| 67 | /> |
| 68 | <span class="alc-code-block__dot" style="background:var(--alc-warn);" /> |
| 69 | <span |
| 70 | class="alc-code-block__dot" |
| 71 | style="background:var(--alc-accent-bright);" |
| 72 | /> |
| 73 | <span class="alc-code-block__filename">{filename}</span> |
| 74 | </div> |
| 75 | <pre |
| 76 | class="alc-code-block__pre" |
| 77 | tabindex="0" |
| 78 | aria-label={`${filename} source`} |
| 79 | set:html={html} |
| 80 | /> |
| 81 | </div> |
| 82 | ) |
| 83 | } |