diff --git a/changelog.md b/changelog.md index b4cd351..534a457 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,12 @@ +# [1.10.0](https://github.com/posthtml/posthtml-expressions/compare/v1.9.0...v1.10.0) (2022-09-26) + + +### Features + +* allow global script locals ([a5678f5](https://github.com/posthtml/posthtml-expressions/commit/a5678f5b37082b26b5f23d7ec43de4f0e3b3ec5c)) + + + # [1.9.0](https://github.com/posthtml/posthtml-expressions/compare/v1.8.1...v1.9.0) (2021-08-23) diff --git a/lib/locals.js b/lib/locals.js index 2e631c2..480cf41 100644 --- a/lib/locals.js +++ b/lib/locals.js @@ -5,11 +5,8 @@ const matchHelper = require('posthtml-match-helper') const { render } = require('posthtml-render') const { match } = require('posthtml/lib/api') -// 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. * @@ -22,13 +19,15 @@ const ctx = vm.createContext({ module }) function scriptDataLocals (tree, options) { const locals = {} const localsAttr = options.localsAttr + const localsContext = options.locals || {} match.call(tree, matchHelper(`script[${localsAttr}]`), node => { if (node.content) { const code = render(node.content) try { - const local = vm.runInContext(code, ctx) + const parsedContext = vm.createContext({ ...ctx, locals: localsContext }) + const local = vm.runInContext(code, parsedContext) Object.assign(locals, local) } catch {}; diff --git a/package-lock.json b/package-lock.json index 8d786be..2bd83e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "posthtml-expressions", - "version": "1.9.0", + "version": "1.10.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -558,9 +558,9 @@ }, "dependencies": { "ansi-regex": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", - "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.1.tgz", + "integrity": "sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==", "dev": true }, "emoji-regex": { @@ -621,9 +621,9 @@ } }, "ansi-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", - "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "dev": true }, "ansi-styles": { @@ -4121,9 +4121,9 @@ } }, "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==", "dev": true }, "minimist-options": { @@ -5814,9 +5814,9 @@ }, "dependencies": { "ansi-regex": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", - "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.1.tgz", + "integrity": "sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==", "dev": true }, "ansi-styles": { @@ -6039,9 +6039,9 @@ "dev": true }, "trim-off-newlines": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz", - "integrity": "sha1-n5up2e+odkw4dpi8v+sshI8RrbM=", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/trim-off-newlines/-/trim-off-newlines-1.0.3.tgz", + "integrity": "sha512-kh6Tu6GbeSNMGfrrZh6Bb/4ZEHV1QlB4xNDBeog8Y9/QwFlKTRyWvY3Fs9tRDAMZliVUwieMgEdIeL/FtqjkJg==", "dev": true }, "tsconfig-paths": { diff --git a/package.json b/package.json index 6c72249..1d4ecea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "posthtml-expressions", - "version": "1.9.0", + "version": "1.10.0", "description": "Expressions Plugin for PostHTML", "engines": { "node": ">=10" diff --git a/readme.md b/readme.md index af6f7de..b3f80d4 100644 --- a/readme.md +++ b/readme.md @@ -93,6 +93,38 @@ You can also use the script tag with the attribute `locals` or you custome attri
My name: Scrum
``` +In addition, the use of script tag allow you to use `locals` defined globally to assign data to variables. + +```js +posthtml(expressions({ locals: { foo: 'bar' } })) + .process(readFileSync('index.html', 'utf8')) + .then((result) => console.log(result.html)) +``` + +```html + + +
My name: {{name}}
+
Foo: {{foo}}
+``` + +```html + + +
My name: {{name}}
+
Foo: bar
+``` + ### Unescaped Locals 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: ```js var opts = { - ignoredTag: 'verbatim', + ignoredTag: 'verbatim', locals: { foo: 'bar' } } } diff --git a/test/expect/script-locals-global-not-informed.html b/test/expect/script-locals-global-not-informed.html new file mode 100644 index 0000000..8ad249c --- /dev/null +++ b/test/expect/script-locals-global-not-informed.html @@ -0,0 +1,9 @@ + + +
My name: Scrum
+
My age: not informed
diff --git a/test/expect/script-locals-global.html b/test/expect/script-locals-global.html new file mode 100644 index 0000000..6864213 --- /dev/null +++ b/test/expect/script-locals-global.html @@ -0,0 +1,9 @@ + + +
My name: Scrum
+
My age: 25
diff --git a/test/fixtures/script-locals-global-not-informed.html b/test/fixtures/script-locals-global-not-informed.html new file mode 100644 index 0000000..558e5ae --- /dev/null +++ b/test/fixtures/script-locals-global-not-informed.html @@ -0,0 +1,9 @@ + + +
My name: {{name}}
+
My age: {{ age }}
diff --git a/test/fixtures/script-locals-global.html b/test/fixtures/script-locals-global.html new file mode 100644 index 0000000..558e5ae --- /dev/null +++ b/test/fixtures/script-locals-global.html @@ -0,0 +1,9 @@ + + +
My name: {{name}}
+
My age: {{ age }}
diff --git a/test/test-locals.js b/test/test-locals.js index c599b64..fb5bf4e 100644 --- a/test/test-locals.js +++ b/test/test-locals.js @@ -35,6 +35,14 @@ test('Basic', (t) => { return process(t, 'script-locals') }) +test('Global Locals - setting global locals', (t) => { + return process(t, 'script-locals-global', { locals: { displayAge: true } }) +}) + +test('Global Locals - no global locals informed', (t) => { + return process(t, 'script-locals-global-not-informed') +}) + test('Remove script locals', (t) => { return process(t, 'script-locals-remove', { removeScriptLocals: true }) })