Skip to content

Commit de09617

Browse files
committed
make 'globals' available to macros, rename previous 'globals' to 'context', closes #5
1 parent cd53c86 commit de09617

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import lodash from 'lodash'
44
import nunjucks from 'nunjucks'
55
import {
66
getPackageInfo,
7-
merge, normalizePath,
7+
merge,
88
pluginBundle,
99
pluginMiddleware,
1010
pluginReload,
@@ -23,7 +23,8 @@ const defaultOptions = {
2323
root: null,
2424
filters: {},
2525
extensions: {},
26-
globals: {
26+
globals: {},
27+
context: {
2728
format: 'njk'
2829
},
2930
data: ['src/data/**/*.json'],
@@ -39,8 +40,8 @@ const renderTemplate = async ({ filename, server, resolvedConfig }, content, opt
3940
? processData({
4041
paths: options.data,
4142
root: resolvedConfig.root
42-
}, options.globals)
43-
: options.globals
43+
}, options.context)
44+
: options.context
4445

4546
if (initialFilename.endsWith('.json')) {
4647
lodash.merge(context, JSON.parse(content))
@@ -80,6 +81,10 @@ const renderTemplate = async ({ filename, server, resolvedConfig }, content, opt
8081
nunjucksEnvironment.addFilter(name, options.filters[name])
8182
})
8283

84+
Object.keys(options.globals).forEach(name => {
85+
nunjucksEnvironment.addGlobal(name, options.globals[name])
86+
})
87+
8388
Object.keys(options.extensions).forEach(name => {
8489
if (typeof options.extensions[name] !== 'function') {
8590
throw new TypeError(`${name} needs to be an function!`)

types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export interface PluginUserConfig {
3030
root?: string
3131
filters?: Object
3232
extensions?: Object
33-
globals?: Object
33+
globals?: Object,
34+
context?: Object,
3435
data?: string | string[]
3536
formats?: string[]
3637
ignoredPaths?: string[]

0 commit comments

Comments
 (0)