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

horseless.decoder

v1.1.3

Published

decoder for html-like template strings

Downloads

12

Readme

horseless.decoder

HTML templates with interpolated values.

Why?

HTML is great at one thing: describing how to build a web page. JavaScript is great at lots of things... but it's horrible at describing how to build a web page. This project turns an HTML template language into a JavaScript-accessible description of how to build a web page. It's then fairly easy to use JavaScript to follow the description and create elements matching the description

How does it work?

Horseless.decoder exposes one tag: h.

h

h is a tag for literal strings. It accepts an HTML-like language and returns an array of descriptions of the described nodes as JavaScript objects.

h`<w a="b">c</w>x<y/>z`

becomes

[
  {"type":"node","tag":"w","attributes":{"a":["b"]},"children":[
    {"type":"textnode","value":"c"}
  ],"xmlns":"http://www.w3.org/1999/xhtml"},
  {"type":"textnode","value":"x"},
  {"type":"node","tag":"y","attributes":{},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"},
  {"type":"textnode","value":"z"}
]

Note that attribute values are represented as arrays. This is to allow for data interpolation within attribute values.

value interpolation

text nodes
let b = h`b`
h`a${b}c`

becomes

[
  {"type":"textnode","value":"a"},
  {"type":"textnode","value":"b"},
  {"type":"textnode","value":"c"}
]
tags
let tag = 'span'
h`<${tag} />`

becomes

[
  {"type":"node","tag":"span","attributes":{},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"}
]
attributes
let x = '1'
let yz = { y: '2', z: '3' }
h`<a x=${x} ${yz}>`

becomes

[
  {"type":"node","tag":"a","attributes":{"x":"1","y":"2","z":"3"},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"}
]
children
let wx = h`<w/><x/>`
h`<a>${wx[1]}<y/><z/></a>`

becomes

[
  {"type":"node","tag":"a","attributes":{},"children":[
    {"type":"node","tag":"x","attributes":{},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"},
    {"type":"node","tag":"y","attributes":{},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"},
    {"type":"node","tag":"z","attributes":{},"children":[],"xmlns":"http://www.w3.org/1999/xhtml"}
  ],"xmlns":"http://www.w3.org/1999/xhtml"}
]

overriding default xmlns

If h is called as h(xmlns) the resulting xmlns attributes will default to the passed in value instead of http://www.w3.org/1999/xhtml

xmlns inheritance

If the xmlns property is set in the HTML then the output node will use that property value for it's xmlns attribute. The xmlns of it's children will also be set to that property

h`<svg xmlns="http://www.w3.org/2000/svg"><p/></svg>`

becomes

[
  {"type":"node","tag":"svg","attributes":{"xmlns":["http://www.w3.org/2000/svg"]},"children":[
    {"type":"node","tag":"p","attributes":{},"children":[],"xmlns":"http://www.w3.org/2000/svg"}
  ],"xmlns":"http://www.w3.org/2000/svg"}
]

"HTML-like"?

It doesn't check every box in the standard. It's close enough that you can paste any sane code and get what you're expecting with little-to-no manual cleanup. It handles void elements, boolean attributes, and unquoted attribute values (contributions welcome).

TODO

Handle closing tags better. Currently all closing tags are treated the same, closing whichever node was most recently opened. Mismatched nodes are handled with obliviosness. The bad HTML but obvious intention of <i>this part is italic <b>this part is bold and italic </i> this part is just bold</b> should be handled as intended (or it should error but that's not very HTML'y)

Better errors. Maybe give a hint as to where the error occurred?