From 8771c656b27743bcc46565e7e4164ee8f4deca26 Mon Sep 17 00:00:00 2001 From: chocolateboy Date: Mon, 24 Sep 2018 18:54:01 +0100 Subject: [PATCH] docfix: improve the synopsis steal the example from https://github.com/mozilla/nunjucks/issues/1153 --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 991acd6..6fe21ac 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,72 @@ nunjucks-parser - extract dependencies and other metadata from nunjucks template # SYNOPSIS +### layout.html + +```xml +{% include "components/header.html" %} +

Body

+{% include "components/footer.html" %} +``` + +### header.html + +```xml +

Header

+``` + +### footer.html + +```xml +

Footer

+{% include "./copyright.html" %} +``` + +### copyright.html + +```xml +Copyright ⓒ example.com 2018 +``` + +### test.js + ```javascript import nunjucks from 'nunjucks' import { parseFile } from 'nunjucks-parser' -const env = nunjucks.configure('src/html') -const data = { foo: 42 } -const { content, template, dependencies } = await parseFile(env, 'index.html.njk', { data }) +const env = nunjucks.configure('example') +const { content, template, dependencies } = await parseFile(env, 'layout.html') console.log(dependencies) ``` +### result + +```javascript +[ + { + name: "layout.html", + path: "/home/user/example/layout.html", + parent: null + }, + { + name: "components/header.html", + path: "/home/user/example/components/header.html", + parent: "/home/user/example/layout.html" + }, + { + name: "components/footer.html", + path: "/home/user/example/components/footer.html", + parent: "/home/user/example/layout.html" + }, + { + name: "./copyright.html", + path: "/home/user/example/components/copyright.html", + parent: "/home/user/example/components/footer.html" + } +] +``` + # DESCRIPTION This module exports a pair of [nunjucks](https://mozilla.github.io/nunjucks/) helper functions, @@ -66,12 +121,12 @@ also return more data than the `render` methods i.e.: Each dependency object contains the following fields: - name (string): the template's name as it appears in the source (may have been resolved to an absolute path/URI if it's relative) -- parent (string?): null if the dependency has no parent file; otherwise the resolved path of its parent file +- parent (string|null): null if the dependency has no parent file; otherwise the resolved path of its parent file - path (string): the resolved path of this dependency (child template) ## Why? -Bundlers such as [parcel](https://parceljs.org/) provide the ability to track the direct and transitive dependencies of +Bundlers such as [Parcel](https://parceljs.org/) provide the ability to track the direct and transitive dependencies of assets so that changes to those dependencies trigger updates in their dependents. nunjucks doesn't provide a built-in way to do this, so this functionality must currently be done in a non-optimal way e.g. by monitoring every file in a directory that has an `.njk` extension.