You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
819 B
TypeScript
34 lines
819 B
TypeScript
4 years ago
|
import _ from 'lodash';
|
||
|
|
||
3 years ago
|
type Substitution = string | { templateRaw: true, html: string };
|
||
|
|
||
|
export default function tpl(pieces: TemplateStringsArray, ...substitutions: Substitution[]) {
|
||
4 years ago
|
let result = pieces[0];
|
||
|
for (let i = 0; i < substitutions.length; ++i) {
|
||
|
const subst = substitutions[i];
|
||
3 years ago
|
let substHtml: string;
|
||
4 years ago
|
if (typeof subst === 'object' && subst.templateRaw) {
|
||
|
substHtml = subst.html;
|
||
|
} else substHtml = _.escape(String(subst));
|
||
|
result += substHtml + pieces[i + 1];
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
3 years ago
|
tpl.typoMsg = function (msg: string, raw = false) {
|
||
3 years ago
|
return tpl`
|
||
|
<div class="typo">
|
||
3 years ago
|
<p>${raw ? { html: msg, templateRaw: true } : msg}</p>
|
||
3 years ago
|
</div>
|
||
|
`;
|
||
|
};
|
||
|
|
||
3 years ago
|
export function rawHtml(html: string) {
|
||
4 years ago
|
return {
|
||
|
templateRaw: true,
|
||
|
html,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
global.Hydro.utils.tpl = tpl;
|