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

nunjucks-to-html

v1.1.0

Published

Parse Nunjucks templates to HTML directly from the console.

Downloads

14

Readme

Nunjucks to HTML

Cross OS CI npm version

Parse Nunjucks templates to HTML easily. You don't need Gulp nor Grunt to do it.

Keep your HTML clean and modularized by using a templating engine, like Nunjucks. Then, convert those files into HTML during your development process or even during production. This way, you can split your files, replace variables, etc... Why Nunjucks? Because it's heavily inspired by Jinja2 but written for Javascript environments.

Installation

Via npm:

npm i nunjucks-to-html

Via yarn:

yarn add nunjucks-to-html

Usage

Using this tool is as simple as installing it, and executing the following:

nunjucks-to-html

However, it's expected to be used in your package.json (in the "scripts" section):

"scripts": {
  "build": "npm run build:html",
  "build:html": "nunjucks-to-html"
},
"devDependencies": {
  "nunjucks-to-html": "*"
}

Or importing it server side using:

const nunjucksToHTML = require('nunjucks-to-html');
// Or: import nunjucksToHTML from 'nunjucks-to-html';

CLI usage


Usage: nunjucks-to-html [sources] [flags...]

  --baseDir <path>        Base directory for the source files. Defaults to "".
  --config <filepath>     Filepath to the config file. Relative to cwd. Defaults to "nunjucks.config.js".
  --dest <path>           Path to the destination directory. Relative to cwd. Defaults to "public"
  --ext <string> 	  Extension for the destination file. Defaults to .html
  --cwd <path>            The path for the current working directory. Defaults to process.cwd().
  --flatten <boolean>     If present, flatten the source file name under the destination path. If absent, use the full source file name under the destination path. Defaults to false.

CLI examples

To parse all .njk files and save them in public/:

nunjucks-to-html

To parse all .njk files in static/ and save them in public/:

nunjucks-to-html --baseDir static

To parse .njk and .html files under the static/ directory and save them in public/static:

nunjucks-to-html static/**/*.{njk,html}

To configure the destination path, use the --dest flag:

nunjucks-to-html --dest my-destination

To configure Nunjucks using a config. file:

nunjucks-to-html
// nunjucks.config.js
module.exports = {
  "options": {
    /**
     * A path to the file containing data for the template.
     * If you want to pass an object, use "render.context" instead.
     */
    "data": "some/path/on/cwd.js",
    /**
     * A hook that's called before calling nunjucks.render()
     * but after nunjucks.configure().
     *
     * Return false to skip rendering (and writing).
     */
    beforeRender (nunjucksEnv, renderName, renderData) { let nunjucks = this; },
    /**
     * A hook that's called after calling nunjucks.render()
     * but before writing to a file.
     *
     * Return false to skip writing.
     */
    beforeWrite (destinationFilepath, renderResult) { let nunjucks = this; }
  },

  /**
   * The following keys are members of Nunjucks.
   * To modify any parameter or see possible values,
   * plese check https://mozilla.github.io/nunjucks/api.html
   */

  // Executes nunjucks.configure([path], [options]).
  "configure": {
    "path": undefined,
    "options": {
      "autoescape": true,
      "throwOnUndefined": false,
      // ...
    }
  },

  // Executes nunjucks.render(name, [context], [callback]).
  "render": {
    "name": undefined, // You shouldn't change this.
    /**
     * An object literal containing the data for the template.
     * If you need to load data from a file, use "options.data" instead.
     * If you decide to use "options.data" too, this property will be assigned to it.
     */
    "context": {},
    "callback": () => {} // Not modificable.
  }

};

To configure the name of the config file, use the --config flag:

nunjucks-to-html --config njk.js

To call multiple jobs with different options, export an array of tasks:

nunjucks-to-html
// nunjucks.config.js
module.exports = [{
  "configure": {},
  "render": {}
}, {
  "configure": {},
  "render": {}
}, /* ... */];

To change the file extension of the destination file, use the --ext flag.

nunjucks-to-html --ext .html

To flatten the source file name under the destination path, use the --flatten flag.

  nunjucks-to-html --flatten

To change the current working directory, use the --cwd flag:

nunjucks-to-html --cwd /var/www

Nodejs usage

The following are the default/supported parameters for this module:

const nunjucksToHtml = require('nunjucks-to-html');

nunjucksToHtml(['**/*.njk'], {
  'config': 'nunjucks.config.js',
  'dest': 'public',
  'ext': '.html',
  'baseDir': '',
  'cwd': process.cwd(),
  'flatten': false
}).then((results) => {})
  .catch((error) => {});

// Produces the same result as calling:
// nunjucksToHtml().then((results) => {}).catch((error) => {});