3.0.2 • Published 2 years ago

@static-pages/nunjucks-writer v3.0.2

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
2 years ago

Static Pages / Nunjucks Writer

Renders page data via Nunjucks templates.

Uses the Nunjucks package under the hood. Everything provided by Nunjucks is also exported from this package (for advanced configuration).

This package is part of the StaticPagesJs project, see:

Options

OptionTypeDefault valueDescription
viewstring \| (d: Data) => stringmain.htmlTemplate to render. If it's a function it gets evaluated on each render call.
viewsDirstring \| string[]viewsOne or more directory path where the templates are found.
outDirstringdistDirectory where the rendered output is saved.
outFilestring \| (d: Data) => stringsee outFile defaults sectionPath of the rendered output relative to outDir.
onOverwrite(d: string) => voidconsole.warn(...)Callback function that gets executed when a file name collision occurs.
onInvalidPath(d: string) => voidconsole.warn(...)Callback function that gets executed when a file name contains invalid characters.
globalsobject{}Additional properties loaded to the nunjucks environment as globals.
functionsRecord<string, Function>{}Functions in an object that gets loaded to the nunjucks environment.
filtersRecord<string, Function>{}Filters in an object that gets loaded to the nunjucks environment.
advanced(env: TwingEnvironment) => void() => undefinedAllows advanced configuration via access to the env nunjucks environment.
showdownEnabledbooleantrueRegister a markdown filter; uses showdown.
showdownOptionsshowdown.ConverterOptionssee showdownOptions sectionCustom options for the showdown markdown renderer.

Example for filters and functions:

import { runtime as nunjucksRuntime } from '@static-pages/nunjucks-writer';
export const myFiltersOrFunctions = {
	asset(asset: string) {
		return new URL(asset, '/site/assets/').toString();
	},
	json_formatted: d => new nunjucksRuntime.SafeString(JSON.stringify(d, null, 4)),
};

outFile defaults

The default behaviour is to guess file path by a few possible properties of the data:

  • if data.url is defined, append .html and use that.
  • if data.header.path is defined, replace extension to .html and use that.
  • if nothing matches call the onInvalidPath handler with undefined file name.

showdownOptions defaults

This package uses a sligthly modified defaults compared to the official Showdown defaults:

{
	simpleLineBreaks: true,
	ghCompatibleHeaderId: true,
	customizedHeaderId: true,
	tables: true,
}