Readme

htmlAsync: async fork of @postpostscript/html supporting Promises as replacements

Examples

Create valimport { delay } from "https://deno.land/x/delay@v0.2.0/mod.ts"; import { html, htmlResponseAsync } from "https://esm.town/v/postpostscript/htmlAsync"; export default function(req: Request) { return htmlResponseAsync` ${(async () => { await delay(1000); return `<script>console.log("sanitized")</script>`; })()} ${(async () => { await delay(1000); return html`<script>console.log("unsanitized")</script>`; })()} `; }

Tests: @postpostscript/htmlAsyncTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { htmlEscape, RawHTML } from "https://esm.town/v/postpostscript/html";
import { hybridTaggedTemplateMethodAsync } from "https://esm.town/v/postpostscript/hybridTaggedTemplateMethod";
import type { MaybePromise } from "https://esm.town/v/postpostscript/typeUtils";
import { html as createHTMLResponse } from "https://esm.town/v/stevekrouse/html";
export { html, htmlEscape, htmlResponse, RawHTML, rawHtml } from "https://esm.town/v/postpostscript/html";
export const htmlAsync = hybridTaggedTemplateMethodAsync({
async transformReplacement(replacement: MaybePromise<unknown>) {
const _replacement = await replacement;
return _replacement instanceof Array
? (await Promise.all(_replacement.map(async (part) => htmlEscape(await part)))).join("")
: htmlEscape(_replacement);
},
async transformResult(result: Promise<string>) {
return new RawHTML(await result);
},
});
export const rawHtmlAsync = hybridTaggedTemplateMethodAsync({
async transformReplacement(replacement: MaybePromise<unknown>) {
const _replacement = await replacement;
return _replacement instanceof Array
? (await Promise.all(_replacement)).join("")
: _replacement;
},
async transformResult(result: Promise<string>) {
return new RawHTML(await result);
},
});
export const htmlResponseAsync = hybridTaggedTemplateMethodAsync({
async transformReplacement(replacement: MaybePromise<unknown>) {
const _replacement = await replacement;
return _replacement instanceof Array
? (await Promise.all(_replacement.map(async (part) => htmlEscape(await part)))).join("")
: htmlEscape(_replacement);
},
async transformResult(result: Promise<string>) {
return createHTMLResponse(await result);
},
});
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.