---
import { highlightTS } from "./highlightTS";
interface Props {
filename?: string;
code: string;
compact?: boolean;
caption?: string;
/** 1-based, inclusive line ranges to highlight. */
highlight?: [number, number][];
}
const {
filename = "alchemy.run.ts",
code,
compact,
caption,
highlight,
} = Astro.props;
// With highlights, each line is highlighted on its own (snippets that use
// this keep strings and comments on one line) and wrapped in a row.
const html = highlight
? code
.split("\n")
.map((line, i) => {
const lit = highlight.some(([a, b]) => i + 1 >= a && i + 1 <= b);
return `<span class="alc-code-block__line${lit ? " is-lit" : ""}">${highlightTS(line) || " "}</span>`;
})
.join("")
: highlightTS(code);
---
{
caption ? (
<div class="alc-code-block__wrap">
<div class:list={["alc-code-block", compact && "alc-code-block--compact"]}>
<div class="alc-code-block__header">
<span
class="alc-code-block__dot"
style="background:var(--alc-danger);"
/>
<span
class="alc-code-block__dot"
style="background:var(--alc-warn);"
/>
<span
class="alc-code-block__dot"
style="background:var(--alc-accent-bright);"
/>
<span class="alc-code-block__filename">{filename}</span>
</div>
<pre
class="alc-code-block__pre"
tabindex="0"
aria-label={`${filename} source`}
set:html={html}
/>
</div>
<div class="alc-code-block__caption">{caption}</div>
</div>
) : (
<div class:list={["alc-code-block", compact && "alc-code-block--compact"]}>
<div class="alc-code-block__header">
<span
class="alc-code-block__dot"
style="background:var(--alc-danger);"
/>
<span class="alc-code-block__dot" style="background:var(--alc-warn);" />
<span
class="alc-code-block__dot"
style="background:var(--alc-accent-bright);"
/>
<span class="alc-code-block__filename">{filename}</span>
</div>
<pre
class="alc-code-block__pre"
tabindex="0"
aria-label={`${filename} source`}
set:html={html}
/>
</div>
)
}