Skip to content

Commit ea429d8

Browse files
authored
Merge pull request #133 from Konnng/master
feat: allow script locals to use global locals
2 parents 260a048 + 78f71af commit ea429d8

File tree

7 files changed

+80
-5
lines changed

7 files changed

+80
-5
lines changed

lib/locals.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ const matchHelper = require('posthtml-match-helper')
55
const { render } = require('posthtml-render')
66
const { match } = require('posthtml/lib/api')
77

8-
// const code = 'module.exports = {a: 1}';
98
const ctx = vm.createContext({ module })
109

11-
// const r = vm.runInContext(code, ctx)
12-
1310
/**
1411
* @description Get the script tag with locals attribute from a node list and return locals.
1512
*
@@ -22,13 +19,15 @@ const ctx = vm.createContext({ module })
2219
function scriptDataLocals (tree, options) {
2320
const locals = {}
2421
const localsAttr = options.localsAttr
22+
const localsContext = options.locals || {}
2523

2624
match.call(tree, matchHelper(`script[${localsAttr}]`), node => {
2725
if (node.content) {
2826
const code = render(node.content)
2927

3028
try {
31-
const local = vm.runInContext(code, ctx)
29+
const parsedContext = vm.createContext({ ...ctx, locals: localsContext })
30+
const local = vm.runInContext(code, parsedContext)
3231

3332
Object.assign(locals, local)
3433
} catch {};

readme.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ You can also use the script tag with the attribute `locals` or you custome attri
9393
<div>My name: Scrum</div>
9494
```
9595

96+
In addition, the use of script tag allow you to use `locals` defined globally to assign data to variables.
97+
98+
```js
99+
posthtml(expressions({ locals: { foo: 'bar' } }))
100+
.process(readFileSync('index.html', 'utf8'))
101+
.then((result) => console.log(result.html))
102+
```
103+
104+
```html
105+
<script locals>
106+
module.exports = {
107+
name: 'Scrum',
108+
foo: locals.foo || 'empty'
109+
}
110+
</script>
111+
112+
<div>My name: {{name}}</div>
113+
<div>Foo: {{foo}}</div>
114+
```
115+
116+
```html
117+
<script locals>
118+
module.exports = {
119+
name: 'Scrum',
120+
foo: locals.foo || 'empty'
121+
}
122+
</script>
123+
124+
<div>My name: {{name}}</div>
125+
<div>Foo: bar</div>
126+
```
127+
96128
### Unescaped Locals
97129

98130
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:
444476

445477
```js
446478
var opts = {
447-
ignoredTag: 'verbatim',
479+
ignoredTag: 'verbatim',
448480
locals: { foo: 'bar' } }
449481
}
450482

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script locals="">
2+
module.exports = {
3+
name: 'Scrum',
4+
age: locals.displayAge ? '25' : 'not informed'
5+
}
6+
</script>
7+
8+
<div>My name: Scrum</div>
9+
<div>My age: not informed</div>

test/expect/script-locals-global.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script locals="">
2+
module.exports = {
3+
name: 'Scrum',
4+
age: locals.displayAge ? '25' : 'not informed'
5+
}
6+
</script>
7+
8+
<div>My name: Scrum</div>
9+
<div>My age: 25</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script locals>
2+
module.exports = {
3+
name: 'Scrum',
4+
age: locals.displayAge ? '25' : 'not informed'
5+
}
6+
</script>
7+
8+
<div>My name: {{name}}</div>
9+
<div>My age: {{ age }}</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script locals>
2+
module.exports = {
3+
name: 'Scrum',
4+
age: locals.displayAge ? '25' : 'not informed'
5+
}
6+
</script>
7+
8+
<div>My name: {{name}}</div>
9+
<div>My age: {{ age }}</div>

test/test-locals.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ test('Basic', (t) => {
3535
return process(t, 'script-locals')
3636
})
3737

38+
test('Global Locals - setting global locals', (t) => {
39+
return process(t, 'script-locals-global', { locals: { displayAge: true } })
40+
})
41+
42+
test('Global Locals - no global locals informed', (t) => {
43+
return process(t, 'script-locals-global-not-informed')
44+
})
45+
3846
test('Remove script locals', (t) => {
3947
return process(t, 'script-locals-remove', { removeScriptLocals: true })
4048
})

0 commit comments

Comments
 (0)