Skip to content

Commit

Permalink
Merge 0746eea into 1b67350
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Aug 9, 2021
2 parents 1b67350 + 0746eea commit fa9e7f6
Show file tree
Hide file tree
Showing 9 changed files with 1,348 additions and 1,509 deletions.
15 changes: 15 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# [1.8.0](https://github.com/posthtml/posthtml-expressions/compare/v1.7.1...v1.8.0) (2021-08-09)


### Bug Fixes

* after update parser/render ([e0bafbe](https://github.com/posthtml/posthtml-expressions/commit/e0bafbeec23b18ae59c40c43dd132fc01bfbc73d))
* lost posthtml-match-helper ([350c3fd](https://github.com/posthtml/posthtml-expressions/commit/350c3fdf167ee638deb9b3c5cf5ffc98527406b8))


### Features

* locals data from script tag, close [#117](https://github.com/posthtml/posthtml-expressions/issues/117) ([e730454](https://github.com/posthtml/posthtml-expressions/commit/e73045458dc90afdb4046a93c394d75800818b8d))



## [1.7.1](https://github.com/posthtml/posthtml-expressions/compare/v1.7.0...v1.7.1) (2020-12-02)


Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict'

const vm = require('vm')
const parser = require('posthtml-parser')
const render = require('posthtml-render')
const { parser } = require('posthtml-parser')
const { render } = require('posthtml-render')

const getNextTag = require('./tags')
const parseLoopStatement = require('./loops')
const escapeRegexpString = require('./escape')
const makeLocalsBackup = require('./backup').make
const revertBackupedLocals = require('./backup').revert
const placeholders = require('./placeholders')
const scriptDataLocals = require('./locals')

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

// kick off the parsing
return function (tree) {
return normalizeTree(clearRawTag(walk({ locals: options.locals, strictMode: options.strictMode }, tree)), tree.options)
const { locals } = scriptDataLocals(tree, options)

return normalizeTree(clearRawTag(walk({ locals: { ...options.locals, ...locals }, strictMode: options.strictMode }, tree)), tree.options)
}
}

Expand Down
44 changes: 44 additions & 0 deletions lib/locals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const vm = require('vm')
const matchHelper = require('posthtml-match-helper')
const { render } = require('posthtml-render')

// 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.
*
* @method scriptDataLocals
*
* @param {Array} tree Nodes
*
* @return {Object} {} Locals
*/
function scriptDataLocals (tree, options) {
const locals = {}
const localsAttr = options.localsAttr || 'locals'

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

try {
const local = vm.runInContext(code, ctx)

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

return node
})

return {
locals
}
}

module.exports = scriptDataLocals

0 comments on commit fa9e7f6

Please sign in to comment.