Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
v bump to 0.21.6, Added ability to escape the brackets. Wrote test fo…
Browse files Browse the repository at this point in the history
…r it
  • Loading branch information
saurabhdaware committed May 21, 2020
1 parent 42b70c7 commit 39f6d4b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ describe('render() - renders abellTemplate into HTML Text', () => {
).trim()
).to.equal('<div>8 hi/hello hi/hello</div>');
});

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 }}');
});
});


Expand Down
5 changes: 4 additions & 1 deletion tests/resources/if-input.abell
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
when {{numberFromJS}} is added to {{numberFromJSON}}, we get {{ numberFromJS + numberFromJSON }}
<h1>{{ globalMeta.siteName }}</h1>
<div class="header-bio">
Abell Minima is a starter blog for AbellJS. <br/>This template is designed by <b>{{ globalMeta.name }}</b> you can follow him on <a href="https://twitter.com/{{ globalMeta.twitter }}">Twitter @{{ globalMeta.twitter }}</a>.
Abell Minima is a starter blog for AbellJS.
<br/>This template is designed by <b>{{ globalMeta.name }}</b>
You can follow him on <a href="https://twitter.com/{{ globalMeta.twitter }}">Twitter @{{ globalMeta.twitter }}</a>.
\{{This text is ignored}}
</div>
</header>
<main>
Expand Down
5 changes: 4 additions & 1 deletion tests/resources/should-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
when 35 is added to 34, we get 69
<h1>This is my siteName</h1>
<div class="header-bio">
Abell Minima is a starter blog for AbellJS. <br/>This template is designed by <b>This is my name</b> you can follow him on <a href="https://twitter.com/saurabhcodes">Twitter @saurabhcodes</a>.
Abell Minima is a starter blog for AbellJS.
<br/>This template is designed by <b>This is my name</b>
You can follow him on <a href="https://twitter.com/saurabhcodes">Twitter @saurabhcodes</a>.
{{This text is ignored}}
</div>
</header>
<main>
Expand Down

0 comments on commit 39f6d4b

Please sign in to comment.