Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

code-lines.ts

16 lines653 Bytes

1/**
2 * Tags each line sugar-high renders (`<span class="sh__line">`) with its
3 * indentation, as `--indent: <n>ch`. With that, CSS can soft-wrap long lines
4 * on small screens and hang the continuation under the line's own start
5 * (see `.code-wrap` in styles.css). Tabs count as two spaces.
6 */
7export function withLineIndents(html: string, code: string): string {
8 const indents = code
9 .split("\n")
10 .map((line) => (/^[ \t]*/.exec(line)?.[0] ?? "").replaceAll("\t", " ").length);
11 let index = 0;
12 return html.replaceAll(
13 '<span class="sh__line">',
14 () => `<span class="sh__line" style="--indent:${indents[index++] ?? 0}ch">`,
15 );