-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,348 additions
and
1,509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.