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

handlebars-helper-prettify

v0.2.1

Published

{{prettify}} handlebars helper for formatting (beautifying) HTML, JavaScript and CSS.

Downloads

159

Readme

{{handlebars_helper_prettify}} NPM version

{{prettify}} handlebars helper for formatting (beautifying) HTML, JavaScript and CSS.

This helper depends on and extends js-beautify. Please visit and star that project to show your support.

Also see examples →

Getting Started

In the root of the project in which you plan to use the helper, in the command line run:

npm i handlebars-helper-prettify --save

Use within your application with the following line of JavaScript:

var helpers = require('handlebars_helper_prettify');

Now your handlebars instance will have access to the {{handlebars_helper_prettify}} helper.

Options

setting options

All options from js-beautify are available in this helper, as well as a few custom options that were specially created for this helper. The helper comes with some sensible defaults (in the humble opinion of the helper creator), but it's easy to customize them if you need to. Here are are two (convenient) ways to set options:

  • options hash: this is an easy way to set options on the helper, and it gives you the most granular control over how the helper renders content.
  • Assemble task options: if you use both Grunt and Assemble, you can define options in your project's Gruntfile.

options hash

By design, options define here will override options defined anywhere else.

{{#handlebars_helper_prettify indent=4}}
  {{> body }}
{{/handlebars_helper_prettify}}

"assemble" task options

The helper can be used without Grunt or Assemble. But if you happen to use these two awesome tools you can define options for the helper in your Gruntfile in the handlebars_helper_prettify sub-options for Assemble:

grunt.initConfig({
  assemble: {
    options: {
      handlebars_helper_prettify: {
        condense: true,
        padcomments: true,
        indent: 4
      }
    },
    ...
  }
});

Options defined in the Assemble task can be viewed as custom "global" defaults, which can be overridden by options defined in the options hash.

custom options

In addition to the options available from js-beautify, the following are custom options created specially for this helper.

mode

Type: String Default value: html (other options: js|css)

If you are formatting HTML, this does not need to be defined, but if you wish to format CSS or JavaScript you must specify either js or css respectively.

{{#handlebars_helper_prettify mode="js" indent=4}}
function foo(str) {return str;}
{{/handlebars_helper_prettify}}

Note that when you change the mode, the available and allowed options change as well. If you specify an option for the wrong mode, the helper may or may not throw an error, so be cautious. This can be a bit tricky if you're building a project that is using the {{prettify}} helper in several places with different modes. It's easy to forget that you have a layout wrapped like this:

{{#prettify indent=2}}
  {{> body }}
{{/prettify}}

and then do this on one of the pages that uses that layout:

{{#handlebars_helper_prettify mode="js" indent=4}}
function foo(str) {return str;}
{{/handlebars_helper_prettify}}

This won't throw an error, but the JavaScript inside the "js" block will be re-formatted by the outter instance of the helper. So based on this example the JavaScript in the "js" block will be indented to 2 spaces.

option defaults

Here are the available options and defaults for each mode.

"html" mode

These options are available by default.

{
  "indent_inner_html": false,   // Indent <head> and <body> sections
  "indent_size": 2,             // Indentation size
  "indent_char": " ",           // Indentation character. Can be an actual tab or space
  "brace_style": "expand",      // collapse|expand|end-expand
  "indent-scripts": "normal"    // keep|separate|normal
  "wrap_line_length": 78,       // Maximum characters per line (0 disables this)
  "unformatted": ["a", "sub", "sup", "b", "i", "u"], // List of tags that should not be reformatted (inline elements included by default)
  "preserve_newlines": true,    // Preserve existing line-breaks
  "max_preserve_newlines": 5,   // Maximum number of line-breaks to be preserved in one chunk

  // custom options made for this helper
  "indent": 2,          // convenience alias for indent_size
  "contense": false,    // remove extra newlines missed by js-beautify.
  "padcomments": false  // add an extra newline above each HTML code comment
}
"js" mode

When mode is set to js, the following options are available:

{
  "indent_size": 2,
  "indent_char": " ",
  "indent_level": 0,
  "indent_with_tabs": false,
  "preserve_newlines": true,
  "max_preserve_newlines": 10,
  "jslint_happy": false,
  "brace_style": "collapse",
  "keep_array_indentation": false,
  "keep_function_indentation": false,
  "space_before_conditional": true,
  "break_chained_methods": false,
  "eval_code": false,
  "unescape_strings": false,
  "wrap_line_length": 0
}
"css" mode

When mode is set to css, the following options are available:

{
  "indent_size": 2,
  "indent_char": " "
}

indent

Alias for indent_size. Type: Number Default value: 2

Number of spaces or tabs to indent the generated code. This option is an alias for indent_size.

condense

Type: Boolean Default value: true

Removes extra newlines and retains indenting:

padcomments

Type: Boolean Default value: True

Add a newline above each code comment:

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- code comment -->
    <meta charset="UTF-8">
    <title>Document</title>
  </head>
  <body>
    <h1>My Blog</h1>
    <h2>Post of the day</h2>

    <!-- scripts -->
    <a href="#">Read more...</a>
  </body>
</html>

Usage Examples

Usage with Assemble

Use this helper in a "parent" layout:

{{#prettify}}
  {{> body }}
{{/prettify}}

See nested layouts.

Indent Example

Before

Using the indent option:

Template: index.hbs

{{#prettify indent="2"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>My Blog</h1>
<h2>Post of the day</h2>
<p>
Vestibulum posuere, quam sed bibendum posuere
Pellentesque habitant morbi tristique senectus
Pellentesque nulla augue, volutpat vitae
</p>
<a href="#">Read more...</a>
</body>
</html>
{{/prettify}}

After

Renders to:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
  </head>
  <body>
    <h1>My Blog</h1>
    <h2>Post of the day</h2>
    <p>
      Vestibulum posuere, quam sed bibendum posuere
      Pellentesque habitant morbi tristique senectus
      Pellentesque nulla augue, volutpat vitae
    </p>
    <a href="#">Read more...</a>
  </body>
</html>

Condense Example

Before

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- code comment -->
    <meta charset="UTF-8">
    <title>Document</title>


  </head>


  <body>
    <h1>My Blog</h1>
    <h2>Post of the day</h2>


    <!-- scripts -->
    <a href="#">Read more...</a>


  </body>
</html>

Example output with condensed: true:

After

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- code comment -->
    <meta charset="UTF-8">
    <title>Document</title>
  </head>
  <body>
    <h1>My Blog</h1>
    <h2>Post of the day</h2>
    <!-- scripts -->
    <a href="#">Read more...</a>
  </body>
</html>

Newlines

When used with condense, defining newlines: true will result in something like this:

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- code comment -->
    <meta charset="UTF-8">
    <title>Document</title>
  </head>
  <body>
    <h1>My Blog</h1>
    <h2>Post of the day</h2>

    <!-- scripts -->
    <a href="#">Read more...</a>
  </body>
</html>

Contributing

Please see the Contributing to Assemble guide for information on contributing to this project.

Author

Related Projects and Links

License

Copyright (c) 2013 Jon Schlinkert, contributors. Released under the MIT license


This file was generated on Mon Oct 21 2013 17:58:28.