Back
Version 13
2/25/2024
import { html, RawHTML } from "https://esm.town/v/postpostscript/html";
export type TextAlts = { text: string } & Partial<Record<string, string>>;
export type TextAltsWithHTML = { text: string; html?: string } & Partial<Record<string, string>>;
export function textAlts<R extends TextAlts = { text: string }>(
strings: TemplateStringsArray,
...replacements: (string | R)[]
) {
return strings.reduce((res, string, index) => {
let toAdd = { text: string } as R;
if (replacements.length >= index + 1) {
const replacement = replacements[index];
toAdd = addTextAlts([
toAdd,
typeof replacement === "string"
? { text: replacement }
: replacement,
]);
}
return addTextAlts([res, toAdd]);
}, {
text: "",
} as R);
}
type Transformers<R> = Partial<
{
[key in keyof R]: (value: string) => string;
}
>;
const DEFAULT_TRANSFORMERS: Transformers<TextAltsWithHTML> = {
html(value: string) {
return html`${value}` as string;
},
Updated: February 26, 2024