npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@byte-this/email-templator

v1.0.6

Published

Typescript API to facilitate email markup generation and help deal with bad email clients.

Downloads

2

Readme

email-templator

Coverage lines Coverage functions Coverage branches Coverage statements

For details and info on how to use: https://bytethisstore.com/articles/pg/email-templating

An api which makes email markup generation a bit easier to deal with. Creating email html is a but cumbersome, as email clients haved mixed support for certain styling attributes, especially more modern ones.

This email-templator:

  • interpolates values based on input dictionary, similar to Angular interpolation syntax
  • process and interpolate css style declarations to inline them for each element
  • give console warnings for potentially problematic css style rules

Install

Library with typescript definitions available as npm package.

npm install @byte-this/email-templator

How To Use

You will need two or three of the following:

  1. Email html template
  2. Dictionary of styles
  3. Dictionary of other interpolation values

The styles and interpolation values will be denoted as {{ }}, similar to Angular interpolation (note that this is not an Angular library and does not support directives, templates, etc).

The following is an example:

/**
 * Here is some basic markup to be sent as a greeting to a new user.
 * 
 * Notice that we have some interpolation going on:
 * --> #main-header, personFullName, .excited-greeting, #websiteUrl
 */ 
const markup = `
<h1 {{#main-header}}>Hello, {{personFullName}}</h1>
<p {{.excited-greeting}}>
    Thanks for joining our system!
</p>
<p>
    We look forward to seeing you on our website at {{websiteUrl}}
</p>
<p {{.excited-greeting}}>
    See you soon!
</p>
`;

/**
 * This is a key value representation of the style names vs rules
 * By convention, it looks very similar to an actual css style definition
 */ 
const styles = {
    '#main-header': `
        color: purple;
        font-weight: bold;
    `,
    '.excited-greeting': `
        width: 700px;
        border-color: 1px sold black;
    `
};

//a simple dictionary of values to insert
const values = {
    personFullName: "Brandon Dixon",
    websiteUrl: "www.bytethisstore.com"
};

//Now let's create the full body template
const emailTemplator = new EmailTemplator();
const generatedMarkup = emailTemplator.generateMarkup(markup, styles, values);

The response will look something like this:

<h1  style="color: purple; font-weight: bold;" >Hello, Brandon Dixon</h1>
<p  style="width: 700px; border-color: 1px sold black;" width="700" >
    Thanks for joining our system!
</p>
<p>
    We look forward to seeing you on our website at www.bytethisstore.com
</p>
<p  style="width: 700px; border-color: 1px sold black;" width="700" >
    See you soon!
</p>

Diving into the Details

In the javascript example above, I've created a styles object where the key/value pairs look very similar to how css is normally declared. I've also added "." and "#" in front of what would normally be a class vs an identifier. That is not enforced by the API, but I consider it to be a useful convention and recommend anyone who uses this library to follow that convention. The values are a simple key/value pair representing values which need to be interpolated.

The output contains all interpolations of values, plus styles wrapped in a "style=" tag. Width and height are also extracted into their own properties when they are represented in terms of "px".

Notes on Styles

When using the style interpolation, any element which will have this style interpolation cannot have any additional "style" tags declared in the markup. It can have other class specifications / styles which are declared in a separate part of the html template. A single html element can only have one style interpolation for a similar reason.

Note on Security

This library does not offer any HTML sanitation. If you are inserting any dynamic markup / data which has not been cleansed, be sure to pass the data through some library before passing it in here.