Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mohitsinghs/njk

Render nunjucks templates with markdown and front-matter
https://github.com/mohitsinghs/njk

cli frontmatter html markdown njk nunjucks template zero-configuration

Last synced: about 2 months ago
JSON representation

Render nunjucks templates with markdown and front-matter

Awesome Lists containing this project

README

        

njk



GitHub Workflow Status
npm version
node-current
GitHub



Render nunjucks templates with markdown and front-matter

generate ( and minify ) html based on nunjucks templates, markdown, front-matter and json/yml data


## About

**njk is no longer maintained**. It was created in 2017 as a command line tool to help me migrate from Jekyll.
It served it's purpose well for me, but since then, I've migrated almost all of my sites to use other
tools. And for all those repositories using it as a library along with nunjucks and express, 🫡

## Install

Install with [npm](https://npm.im/njk)

```sh
npm install --dev njk
```

or with [yarn](https://yarn.pm/njk)

```sh
yarn add --dev njk
```

## Usage

```sh
njk [options]
```

### Command Line Flags

- **`-V`** prints version
- **`-h or --help`** prints help text
- **`-v or --verbose`** includes additional logging
- **`-b or --block`** wraps a content block by default. This is convenient when you you want to extend just one block. This helps you avoid writing extends tag in child template
- **`-c or --clean`** uses clean urls (urls with forward slash) for output files.
- **`-q or --quiet`** silences the output until any error occurs.
- **`-w or --watch`** runs everything in watch mode. HTML is not minified in this mode.

### Command Line Options

- **`-d or --data`** pass either json file path of yaml directory path containing data.
- **`-t or --template`** pass template directories (nunjucks searchPaths). Multiple template directories can be passed, separated by comma `,`
- **`-o or --out`** pass output directory

### File Specific Options

Following options can be configured through front-matter of individual files.

- **`layout`** parent layout/template to use for rendering a file. This inserts a `extends` tag automatically.
- **`block`** Wraps a content block around a page. If enabled, an empty content block is required in parent template where content will be inserted.
- **`clean`** Uses clean urls while writing files. For example `file.html` will be written as `file/index.html`

## Contributing

You can help improving njk in following ways -

- Found a bug, create an issue with relevant information.
- Want a feature to be added, A pull request is always welcome.

Some Examples


1. Rendering a template using block flag and layout option in front matter


We can avoid wrapping `extends` tags and overriding `block` tags, If we need to inject single block in parent template.

**Step 1**

Consider we have a nunjucks template with single block.

_`default.njk`_

```nunjucks


Page Title

{% block content %}{% endblock %}

```

and a simple html page

_`index.html`_

```nunjucks
---
layout: default
---

On Laughing

A laugh draws a lot of painful lines.

Copyright © Creator Inc.

```

**Step 2**

Now, Let's run njk.

```bash
njk index.html -b
```

> The current directory will be used for templates.

**Result**

The result will be something like

_`dist/index.html`_

```html



Page Title



On Laughing




A laugh draws a lot of painful lines.




Copyright © Creator Inc.

```


2. Rendering a template using layout option in front matter


Wrapping `extends` tag in each of our file isn't super cool,

and we can avoid this by using `layout` option in front-matter.

### Example

**Step 1**

Consider we have a nunjucks template with 3 blocks.

_`default.njk`_

```nunjucks


Page Title


{% block header %}{% endblock %}


{% block main %}{% endblock %}


{% block footer %}{% endblock %}

```

and a simple html page with content for these 3 blocks

_`index.html`_

```nunjucks
---
layout: default
---
{% block header %}

On Laughing


{% endblock %}
{% block main %}

A laugh draws a lot of painful lines.


{% endblock %}
{% block footer %}
Copyright © Creator Inc.
{% endblock %}
```

**Step 2**

Now, Let's run njk.

```bash
njk index.html
```

> The current directory will be used for templates.

**Result**

The result will be something like

_`dist/index.html`_

```html



Page Title


On Laughing


A laugh draws a lot of painful lines.


Copyright © Creator Inc.

```


Extra : Configuring layout through data passed


We can go one step further and configure layout it in the data passed with `-d` or `--data`

**Step 1**

Remove front-matter from _`index.html`_

_`index.html`_

```nunjucks
{% block header %}

On Laughing


{% endblock %}
{% block main %}

A laugh draws a lot of painful lines.


{% endblock %}
{% block footer %}
Copyright © Creator Inc.
{% endblock %}
```

**Step 2 ( using yml )**

_`data/page.yml`_

```yml
layout: default
```

We need to run

```bash
njk index.html -d data
```

Note that file name is important here, as we need `page.layout` property.

**Step 2 ( using json )**

We can pass a single json file instead of `data` folder

_`data.json`_

```json
{
"page": {
"layout": "default"
}
}
```

We need to run

```bash
njk index.html -d data.json
```

**Result**

The result will be same as our previous run (Example 2).