Skip to content

Commit c96e085

Browse files
authored
Merge pull request #118 from posthtml/milestone-1.8.0
Milestone 1.8.0
2 parents 1b67350 + 0746eea commit c96e085

File tree

9 files changed

+1348
-1509
lines changed

9 files changed

+1348
-1509
lines changed

changelog.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# [1.8.0](https://github.com/posthtml/posthtml-expressions/compare/v1.7.1...v1.8.0) (2021-08-09)
2+
3+
4+
### Bug Fixes
5+
6+
* after update parser/render ([e0bafbe](https://github.com/posthtml/posthtml-expressions/commit/e0bafbeec23b18ae59c40c43dd132fc01bfbc73d))
7+
* lost posthtml-match-helper ([350c3fd](https://github.com/posthtml/posthtml-expressions/commit/350c3fdf167ee638deb9b3c5cf5ffc98527406b8))
8+
9+
10+
### Features
11+
12+
* locals data from script tag, close [#117](https://github.com/posthtml/posthtml-expressions/issues/117) ([e730454](https://github.com/posthtml/posthtml-expressions/commit/e73045458dc90afdb4046a93c394d75800818b8d))
13+
14+
15+
116
## [1.7.1](https://github.com/posthtml/posthtml-expressions/compare/v1.7.0...v1.7.1) (2020-12-02)
217

318

lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict'
22

33
const vm = require('vm')
4-
const parser = require('posthtml-parser')
5-
const render = require('posthtml-render')
4+
const { parser } = require('posthtml-parser')
5+
const { render } = require('posthtml-render')
66

77
const getNextTag = require('./tags')
88
const parseLoopStatement = require('./loops')
99
const escapeRegexpString = require('./escape')
1010
const makeLocalsBackup = require('./backup').make
1111
const revertBackupedLocals = require('./backup').revert
1212
const placeholders = require('./placeholders')
13+
const scriptDataLocals = require('./locals')
1314

1415
const delimitersSettings = []
1516
let conditionals, switches, loops, scopes, ignored, delimitersReplace, unescapeDelimitersReplace
@@ -156,7 +157,9 @@ module.exports = function postHTMLExpressions (options) {
156157

157158
// kick off the parsing
158159
return function (tree) {
159-
return normalizeTree(clearRawTag(walk({ locals: options.locals, strictMode: options.strictMode }, tree)), tree.options)
160+
const { locals } = scriptDataLocals(tree, options)
161+
162+
return normalizeTree(clearRawTag(walk({ locals: { ...options.locals, ...locals }, strictMode: options.strictMode }, tree)), tree.options)
160163
}
161164
}
162165

lib/locals.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const vm = require('vm')
4+
const matchHelper = require('posthtml-match-helper')
5+
const { render } = require('posthtml-render')
6+
7+
// const code = 'module.exports = {a: 1}';
8+
const ctx = vm.createContext({ module })
9+
10+
// const r = vm.runInContext(code, ctx)
11+
12+
/**
13+
* @description Get the script tag with locals attribute from a node list and return locals.
14+
*
15+
* @method scriptDataLocals
16+
*
17+
* @param {Array} tree Nodes
18+
*
19+
* @return {Object} {} Locals
20+
*/
21+
function scriptDataLocals (tree, options) {
22+
const locals = {}
23+
const localsAttr = options.localsAttr || 'locals'
24+
25+
tree.match(matchHelper(`script[${localsAttr}]`), node => {
26+
if (node.content) {
27+
const code = render(node.content)
28+
29+
try {
30+
const local = vm.runInContext(code, ctx)
31+
32+
Object.assign(locals, local)
33+
} catch {};
34+
}
35+
36+
return node
37+
})
38+
39+
return {
40+
locals
41+
}
42+
}
43+
44+
module.exports = scriptDataLocals

0 commit comments

Comments
 (0)