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

prettier-plugin-html-template-literals

v1.0.5

Published

Formats HTML within tagged template literals in Prettier which is useful for hyperHTML, lit-html, choo, hyperx, nanohtml, snabby, yo-yo, and others.

Downloads

45

Readme

prettier-plugin-html-template-literals

Build Status

Formats HTML within tagged template literals in Prettier which is useful for hyperHTML, lit-html, choo, hyperx, nanohtml, snabby, yo-yo, and others. For example:

Input:

html`<div id="foo" class="foo bar baz" data-foo="foo" data-bar="bar" data-baz="baz"><span>Foobar</span></div>`;

Output:

html`
  <div
    id="foo"
    class="foo bar baz"
    data-foo="foo"
    data-bar="bar"
    data-baz="baz"
  >
    <span>Foobar</span>
  </div>
`;

Why

Declaring HTML templates using tagged template literals is a nice alternative to JSX. It relies only on standard ES6+ and doesn't require a transpiling/preprocessing toolset, at least during development. hyperHTML and lit-html are nice lightweight libraries built on this idea and, more than that, provide the React-like experience without the bloat of Virtual DOM. There are also Virtual DOM-based libraries that consume HTML from tagged template literals: choo, hyperx, nanohtml, snabby, yo-yo, and others.

Warning

As HTML tagged template literals get more attention, no doubts Prettier will support them. You can track an open issue for this request. The problem is that the HTML parser within Prettier is not stable yet, and they haven't decided which tags should identify HTML templates. Also, at the moment, there is no API in Prettier to provide additional embedded languages from plugins. There is a feature request for this. This plugin provides a temporary 'hacky' solution until it will be implemented within Prettier. It uses Prettier's JSX parser and formats any tagged template literal (but not regular template literal) if it contains </...> or <.../>. It uses some dirty tricks under the hood, so be warned and report issues/PR.

Usage

Install prettier and prettier-plugin-html-template-literals using yarn:

yarn add prettier prettier-plugin-html-template-literals

Or using npm:

npm install prettier prettier-plugin-html-template-literals

Usage from CLI

prettier-plugin-html-template-literals replaces the prettier executable with its wrapped version. The original prettier executable will be restored if you delete this plugin. Use it as the regular prettier executable:

echo 'html`<div><span>foo</span></div>`' | ./node_modules/.bin/prettier --stdin-filepath=test.js

Usage from API

Unfortunately, this plugin is actually ignored by the API exposed with the prettier module at the moment. Instead of require('prettier').format() you need to use the wrapper module with the same API:

require('prettier-plugin-html-template-literals/prettier').format(
  'html`<div><span>foo</span></div>`',
  { parser: 'babylon' },
);

Limitations

Because of this plugin relies on the JSX parser it has some limitations. Some of them may be addressed in future if it will be possible to find a workaround.

  • Attribute values without quotes are not supported. Raises an exception: <div id=foo>
  • Contents of <style> elements is ignored and not prettified. Style is not formatted: <style>.foo{color:red}</style>
  • It's impossible to reliably eliminate whitespace between adjacent elements: May be wrapped on not to multiple lines: <span></span><span></span>
  • All empty elements are converted to self-closing (void) ones: <div />
  • Whitespace is not preserved within elements sensitive to it like pre or textarea. Contents will be collapsed to a single space: <pre>\n</pre>