multiFormat
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
multiFormat: easily create outputs of multiple formats (e.g. text/html for Email) using tagged templates
import { format } from "https://esm.town/v/postpostscript/multiFormat";
console.log(format.Plain`
${format.Heading1`Some Heading`}
Lorem ipsum dolor ${format.Strong`sit`} amet
`);
{ "text": "\nSome Heading\n\nLorem ipsum dolor sit amet\n", "html": "\n<br><h1>Some Heading</h1>\n<br>\n<br>Lorem ipsum dolor <strong>sit</strong> amet\n<br>" }
import { createFormatMethod, wrapHTMLTag, type MultiFormatWithHTML } from "https://esm.town/v/postpostscript/multiFormat";
export const Red = createFormatMethod<MultiFormatWithHTML>((value) => {
return wrapHTMLTag("span", value, {
style: "color: red;",
});
});
console.log(Red`red text!`)
{ "text": "red text!", "html": "<span style=\"color: red;\">red text!</span>" }
Text is automatically sanitized when converted to HTML:
console.log(format.Trim`
${format.Heading1`Some Heading`}
<script>alert(1)</script>
`.html.toString())
<h1>Some Heading</h1> <br> <br><script>alert(1)</script>
Migrated from folder: HTML/multiFormat