diff --git a/lib/locals.js b/lib/locals.js
index 2e631c2..480cf41 100644
--- a/lib/locals.js
+++ b/lib/locals.js
@@ -5,11 +5,8 @@ const matchHelper = require('posthtml-match-helper')
const { render } = require('posthtml-render')
const { match } = require('posthtml/lib/api')
-// const code = 'module.exports = {a: 1}';
const ctx = vm.createContext({ module })
-// const r = vm.runInContext(code, ctx)
-
/**
* @description Get the script tag with locals attribute from a node list and return locals.
*
@@ -22,13 +19,15 @@ const ctx = vm.createContext({ module })
function scriptDataLocals (tree, options) {
const locals = {}
const localsAttr = options.localsAttr
+ const localsContext = options.locals || {}
match.call(tree, matchHelper(`script[${localsAttr}]`), node => {
if (node.content) {
const code = render(node.content)
try {
- const local = vm.runInContext(code, ctx)
+ const parsedContext = vm.createContext({ ...ctx, locals: localsContext })
+ const local = vm.runInContext(code, parsedContext)
Object.assign(locals, local)
} catch {};
diff --git a/readme.md b/readme.md
index af6f7de..b3f80d4 100644
--- a/readme.md
+++ b/readme.md
@@ -93,6 +93,38 @@ You can also use the script tag with the attribute `locals` or you custome attri
My name: Scrum
```
+In addition, the use of script tag allow you to use `locals` defined globally to assign data to variables.
+
+```js
+posthtml(expressions({ locals: { foo: 'bar' } }))
+ .process(readFileSync('index.html', 'utf8'))
+ .then((result) => console.log(result.html))
+```
+
+```html
+
+
+My name: {{name}}
+Foo: {{foo}}
+```
+
+```html
+
+
+My name: {{name}}
+Foo: bar
+```
+
### Unescaped Locals
By default, special characters will be escaped so that they show up as text, rather than html code. For example, if you had a local containing valid html as such:
@@ -444,7 +476,7 @@ You can customize the name of the tag:
```js
var opts = {
- ignoredTag: 'verbatim',
+ ignoredTag: 'verbatim',
locals: { foo: 'bar' } }
}
diff --git a/test/expect/script-locals-global-not-informed.html b/test/expect/script-locals-global-not-informed.html
new file mode 100644
index 0000000..8ad249c
--- /dev/null
+++ b/test/expect/script-locals-global-not-informed.html
@@ -0,0 +1,9 @@
+
+
+My name: Scrum
+My age: not informed
diff --git a/test/expect/script-locals-global.html b/test/expect/script-locals-global.html
new file mode 100644
index 0000000..6864213
--- /dev/null
+++ b/test/expect/script-locals-global.html
@@ -0,0 +1,9 @@
+
+
+My name: Scrum
+My age: 25
diff --git a/test/fixtures/script-locals-global-not-informed.html b/test/fixtures/script-locals-global-not-informed.html
new file mode 100644
index 0000000..558e5ae
--- /dev/null
+++ b/test/fixtures/script-locals-global-not-informed.html
@@ -0,0 +1,9 @@
+
+
+My name: {{name}}
+My age: {{ age }}
diff --git a/test/fixtures/script-locals-global.html b/test/fixtures/script-locals-global.html
new file mode 100644
index 0000000..558e5ae
--- /dev/null
+++ b/test/fixtures/script-locals-global.html
@@ -0,0 +1,9 @@
+
+
+My name: {{name}}
+My age: {{ age }}
diff --git a/test/test-locals.js b/test/test-locals.js
index c599b64..fb5bf4e 100644
--- a/test/test-locals.js
+++ b/test/test-locals.js
@@ -35,6 +35,14 @@ test('Basic', (t) => {
return process(t, 'script-locals')
})
+test('Global Locals - setting global locals', (t) => {
+ return process(t, 'script-locals-global', { locals: { displayAge: true } })
+})
+
+test('Global Locals - no global locals informed', (t) => {
+ return process(t, 'script-locals-global-not-informed')
+})
+
test('Remove script locals', (t) => {
return process(t, 'script-locals-remove', { removeScriptLocals: true })
})