-
-
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
8 changed files
with
1,331 additions
and
1,507 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
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 @@ | ||
<script locals=""> | ||
module.exports = { | ||
name: 'Scrum' | ||
} | ||
</script> | ||
|
||
<div>My name: Scrum</div> |
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,7 @@ | ||
<script locals> | ||
module.exports = { | ||
name: 'Scrum' | ||
} | ||
</script> | ||
|
||
<div>My name: {{name}}</div> |
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,36 @@ | ||
const test = require('ava') | ||
|
||
const join = require('path').join | ||
const readSync = require('fs').readFileSync | ||
|
||
const posthtml = require('posthtml') | ||
const expressions = require('../lib') | ||
|
||
const fixture = (file) => { | ||
return readSync(join(__dirname, 'fixtures', `${file}.html`), 'utf8') | ||
} | ||
|
||
const expect = (file) => { | ||
return readSync(join(__dirname, 'expect', `${file}.html`), 'utf8') | ||
} | ||
|
||
function process (t, name, options, log = false, plugins = [expressions(options)], processOptions = {}) { | ||
return posthtml(plugins) | ||
.process(fixture(name), processOptions) | ||
.then((result) => { | ||
log && console.log(result.html) | ||
|
||
return clean(result.html) | ||
}) | ||
.then((html) => { | ||
t.is(html, expect(name).trim()) | ||
}) | ||
} | ||
|
||
function clean (html) { | ||
return html.replace(/[^\S\r\n]+$/gm, '').trim() | ||
} | ||
|
||
test('Basic', (t) => { | ||
return process(t, 'script-locals') | ||
}) |