From 8129de156d1c0c53f54834a1e7001b5782f4092b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 26 Mar 2022 08:19:17 +0000 Subject: [PATCH 1/6] build(deps): bump minimist from 1.2.5 to 1.2.6 Bumps [minimist](https://github.com/substack/minimist) from 1.2.5 to 1.2.6. - [Release notes](https://github.com/substack/minimist/releases) - [Commits](https://github.com/substack/minimist/compare/1.2.5...1.2.6) --- updated-dependencies: - dependency-name: minimist dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8d786be..35e91e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { From 04f599d15f5e8f83be843b9909a5aae8d69f4e7f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 26 Apr 2022 10:54:16 +0000 Subject: [PATCH 2/6] build(deps): bump trim-off-newlines from 1.0.1 to 1.0.3 Bumps [trim-off-newlines](https://github.com/stevemao/trim-off-newlines) from 1.0.1 to 1.0.3. - [Release notes](https://github.com/stevemao/trim-off-newlines/releases) - [Commits](https://github.com/stevemao/trim-off-newlines/compare/v1.0.1...v1.0.3) --- updated-dependencies: - dependency-name: trim-off-newlines dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35e91e3..70e99ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { From 03884bed84a513f3e64f195369d7bdd807298fbd Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 26 Apr 2022 10:54:21 +0000 Subject: [PATCH 3/6] build(deps): bump ansi-regex from 4.1.0 to 4.1.1 Bumps [ansi-regex](https://github.com/chalk/ansi-regex) from 4.1.0 to 4.1.1. - [Release notes](https://github.com/chalk/ansi-regex/releases) - [Commits](https://github.com/chalk/ansi-regex/compare/v4.1.0...v4.1.1) --- updated-dependencies: - dependency-name: ansi-regex dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35e91e3..f4e2fe6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { @@ -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": { From a5678f5b37082b26b5f23d7ec43de4f0e3b3ec5c Mon Sep 17 00:00:00 2001 From: Konnng Date: Fri, 23 Sep 2022 11:15:52 -0300 Subject: [PATCH 4/6] feat: allow global script locals --- lib/locals.js | 7 +++---- test/expect/script-locals-global-not-informed.html | 9 +++++++++ test/expect/script-locals-global.html | 9 +++++++++ test/fixtures/script-locals-global-not-informed.html | 9 +++++++++ test/fixtures/script-locals-global.html | 9 +++++++++ test/test-locals.js | 8 ++++++++ 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/expect/script-locals-global-not-informed.html create mode 100644 test/expect/script-locals-global.html create mode 100644 test/fixtures/script-locals-global-not-informed.html create mode 100644 test/fixtures/script-locals-global.html 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/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 }) }) From 78f71af8d609613c6d69bf1f8e1369dc3e9df8f4 Mon Sep 17 00:00:00 2001 From: Konnng Date: Fri, 23 Sep 2022 11:26:39 -0300 Subject: [PATCH 5/6] chore: README updated --- readme.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) 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' } } } From 6c3c7a70598145d2e45f0210f68a3b74836f635a Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Mon, 26 Sep 2022 10:03:00 +0300 Subject: [PATCH 6/6] 1.10.0 --- changelog.md | 9 +++++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) 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/package-lock.json b/package-lock.json index 83446df..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": { 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"