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

stringml

v1.0.2

Published

An HTML templating library that turn JavaScript functions into HTML strings

Downloads

1

Readme

StringML

Build Status Coverage Status

StringML is a Javascript templating library that is written in vanilla JS and outputs HTML strings. There's really nothing too special going on here: while similar to Mustache, Handlebars, and underscore templates where the output is a string of HTML, the StringML syntax just uses Javascript functions. The functions break down into

  • HTML tag functions
  • A converter function that takes the output of the HTML functions and creates an HTML string

HTML Tag Functions

The StringML HTML tag functions support

  • Nesting
  • Componentization
  • Arbitrary argument order
  • Full support of HTML5
  • HTML attributes using native naming
  • HTML-like appearance
  • Comments

Here's a basic example

import {
  header,
  h1,
  span
} from 'stringml';

const pageHeader = 
  header(
    h1(
      span('Page Header Text')
    )
  )
;

You can easily add HTML attributes to this, and change the whitespace to your liking.

const pageHeader = 
  header({id: 'pageHeader', class: 'hero xs-span12'},
    h1(
      span({class: 'special-font-adapter'}, 'Page Header Text')
    )
  )
;

Note that HTML attributes are simply key names in an object passed to the function, using the HTML attribute's name. In other words, simply supply {class: 'some-class} in order to get class="some-class" in the output. Further, arguments can be in any order, and there can be as many sibling elements as desired.

const pageContent =
  article(
    p('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
    p('Nullam hendrerit sapien non placerat eleifend.'),
    p('Nulla ullamcorper viverra eros, sit amet pharetra libero commodo vitae.', {class: 'last'})
  )
;

Collections

Collections are simple to accomplish. Rather than using special syntax like Handlebars requires, StringML allows you to use any JS iterator, though a map will likely be the most terse and legible. You'll also need to use the spread operator to turn the array into a list of arguments.

const listData = ['item 1', 'item 2', 'item 3'];
const listItems = listData.map( (item) => li(item) );
const orderedItemList = ol(
  ...listItems
);

The ES5 variation is less clean in terms of emulating HTML, but works just fine

var orderedItemList = ol.apply(null, listItems);

Comments

There is a dedicated comment function, called comment.

const pageContent =
  article(
    p('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
    p('Nullam hendrerit sapien non placerat eleifend.'),
    p('Nulla ullamcorper viverra eros, sit amet pharetra libero commodo vitae.', {class: 'last'}),
    comment('Weird that I\'d need a comment in my HTML output, but, hey, here we are.')
  )
;

Converter Function

The stringML function converts the output of the HTML tag functions into an HTML string:

// assuming all the imports and declarations from above
import {
  ...,
  stringML
} from 'stringml';

const pageHTML = [
  pageHeader,
  pageContent,
  orderedItemList
];

const rendered = pageHTML.map((node) => stringML(node)).join('\n');

becomes

<header id="pageHeader" class="hero xs-span12"><h1><span class="special-font-adapter">Page Header Text</span></h1></header>
<article><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p>Nullam hendrerit sapien non placerat eleifend.</p><p class="last">Nulla ullamcorper viverra eros, sit amet pharetra libero commodo vitae.</p><!-- Weird that I'd need a comment in my HTML output, but, hey, here we are. --></article>
<ol><li>item 1</li><li>item 2</li><li>item 3</li></ol>