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

js.html-builder

v1.1.3

Published

Javascript DSL for HTML

Downloads

66

Readme

js.html-builder

The js.html-builder library provides a domain specific language (DSL) to build HTML with pure javascript code.

Installation

Install with npm:

$ npm install --save js.html-builder

Examples

Hello World

This javascript code:

import { htmlBuilder } from "js.html-builder"

const code = () =>
    html(() => {
        head(() => title('Hello World'))
        body(() => {
            h1('Hello')
            p('World')
        })
    })

console.log(htmlBuilder(code).toHtml())

Generates the following HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello</h1>
    <p>World</p>
  </body>
</html>

HTML snippet

The following HTML snippet mirrors Bootstrap's alert example:

import { blockBuilder } from "js.html-builder"

let markup = () => div(() =>
    ['primary', 'secondary', 'success', 'danger', 'warning',
    'info', 'light', 'dark'].forEach(alert =>
        div({class: `alert alert-${alert}`, role: "alert"},
          `A simple ${alert} alert—check it out!`))
)
console.log(blockBuilder(markup).toHtml())

Generates the following

<div>
  <div class="alert alert-primary" role="alert">A simple primary alert—check it out!</div>
  <div class="alert alert-secondary" role="alert">A simple secondary alert—check it out!</div>
  <div class="alert alert-success" role="alert">A simple success alert—check it out!</div>
  <div class="alert alert-danger" role="alert">A simple danger alert—check it out!</div>
  <div class="alert alert-warning" role="alert">A simple warning alert—check it out!</div>
  <div class="alert alert-info" role="alert">A simple info alert—check it out!</div>
  <div class="alert alert-light" role="alert">A simple light alert—check it out!</div>
  <div class="alert alert-dark" role="alert">A simple dark alert—check it out!</div>
</div>

DIV with Link

In this example, a the text content of a div is mixed with a link element. So we need to explicitly call out the text nodes:

import { blockBuilder } from "js.html-builder"

let markup = () => div({class: "alert alert-primary", role: "alert"},
    () => {
        text("A simple primary ")
        a({href: "http://example.com"}, "alert with a link")
        text("Click if you like it.")
    })

console.log(blockBuilder(markup).toHtml())

HTML:

<div class="alert alert-primary" role="alert">A simple primary
  <a href="http://example.com">alert with a link</a>Click if you like it.
</div>

Usage

Both htmlBuilder and snippetBuilder accept a configuration closure which configures and the markup as desired. To emit a html tag, call a function with the same name (except for var), e.g.: to emit a div, call div(). Each tag function accepts (syntax permitting) up to three parameters which configure the tag's attributes, value and child tags, in that order:

  • Attributes: Tag attributes can be set by passing an object to the tag call, e.g.: span({class: 'special', role="alert"}) will emit <span class="special" role="alert"></span>.
  • Content: Tag content (value) is set by passing as string (or number | boolean), e.g.: p('para contents') will emit <p>para contents</p>.
  • Child tags: To emit child tags for the current tag, pass a configuration closure as the last argument. e.g.:
div( () =>
    span('span contents')
)

will emit:

<div>
    <span>span contents</span>
</div>

Note that the configuration closure passed as the root must have a single child element.