From 39f6d4b81de920abcda40283bac02402d904a9c1 Mon Sep 17 00:00:00 2001 From: Saurabh Daware Date: Thu, 21 May 2020 11:05:55 +0530 Subject: [PATCH] v bump to 0.21.6, Added ability to escape the brackets. Wrote test for it --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/index.js | 7 +++++-- tests/index.spec.js | 18 ++++++++++++++++++ tests/resources/if-input.abell | 5 ++++- tests/resources/should-output.html | 5 ++++- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c208e6..9e32361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.1.6 +- **MAJOR CHANGE** + Added ability to escape the brackets with a slash('\') + ## v0.1.5 - Support for expression after `require` (e.g. `require('module1').someProperty`) - Build a folder with CLI (Issue: [#6](https://github.com/abelljs/abell-renderer/issues/6), PR: [#8](https://github.com/abelljs/abell-renderer/pull/8), Thanks to [@Pika1998](https://github.com/Pika1998)) diff --git a/package.json b/package.json index 8d38e48..74f0ea4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "abell-renderer", - "version": "0.1.5", + "version": "0.1.6", "description": "A wrapper arround Mustache that adds some additional features and some syntatic sugar required for abell", "main": "dist/index.js", "bin": { diff --git a/src/index.js b/src/index.js index e9492ec..e8debc1 100644 --- a/src/index.js +++ b/src/index.js @@ -44,13 +44,16 @@ const execRegexOnAll = (regex, template) => { */ function render(abellTemplate, sandbox, options = {basePath: ''}) { // Finds all the JS expressions to be executed. - const {matches, input} = execRegexOnAll(/{{(.*?)}}/gs, abellTemplate); + const {matches, input} = execRegexOnAll(/\\?{{(.+?)}}/gs, abellTemplate); let renderedHTML = ''; let lastIndex = 0; for (const match of matches) { // Loops Through JavaScript blocks inside '{{' and '}}' let value = ''; - if (match[1].includes('require(')) { + if (match[0].startsWith('\\{{')) { + // Ignore the match that starts with slash '\' and return the same value without slash + value = match[0].slice(1); + } else if (match[1].includes('require(')) { // the js block is trying to require (e.g const module1 = require('module1')) const lines = match[1] .trim() diff --git a/tests/index.spec.js b/tests/index.spec.js index f6b68a9..b029e87 100644 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -83,6 +83,24 @@ describe('render() - renders abellTemplate into HTML Text', () => { ).trim() ).to.equal('
8 hi/hello hi/hello
'); }); + + it('should not throw error and return same value if blank brackets passed', () => { + expect( + abellRenderer.render( + '{{}}', + {} + ) + ).to.equal('{{}}'); + }); + + it('should ignore the brackets when slash is added before the bracket', () => { + expect( + abellRenderer.render( + '\\{{ This is ignored }}', + {} + ) + ).to.equal('{{ This is ignored }}'); + }); }); diff --git a/tests/resources/if-input.abell b/tests/resources/if-input.abell index aad1e5f..611d266 100644 --- a/tests/resources/if-input.abell +++ b/tests/resources/if-input.abell @@ -23,7 +23,10 @@ when {{numberFromJS}} is added to {{numberFromJSON}}, we get {{ numberFromJS + numberFromJSON }}

{{ globalMeta.siteName }}

- Abell Minima is a starter blog for AbellJS.
This template is designed by {{ globalMeta.name }} you can follow him on Twitter @{{ globalMeta.twitter }}. + Abell Minima is a starter blog for AbellJS. +
This template is designed by {{ globalMeta.name }} + You can follow him on Twitter @{{ globalMeta.twitter }}. + \{{This text is ignored}}
diff --git a/tests/resources/should-output.html b/tests/resources/should-output.html index 1ad9712..d262dae 100644 --- a/tests/resources/should-output.html +++ b/tests/resources/should-output.html @@ -11,7 +11,10 @@ when 35 is added to 34, we get 69

This is my siteName

- Abell Minima is a starter blog for AbellJS.
This template is designed by This is my name you can follow him on Twitter @saurabhcodes. + Abell Minima is a starter blog for AbellJS. +
This template is designed by This is my name + You can follow him on Twitter @saurabhcodes. + {{This text is ignored}}