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

Commit 39f6d4b

Browse files
committed
v bump to 0.21.6, Added ability to escape the brackets. Wrote test for it
1 parent 42b70c7 commit 39f6d4b

6 files changed

Lines changed: 36 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.1.6
4+
- **MAJOR CHANGE**
5+
Added ability to escape the brackets with a slash('\')
6+
37
## v0.1.5
48
- Support for expression after `require` (e.g. `require('module1').someProperty`)
59
- 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))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "abell-renderer",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "A wrapper arround Mustache that adds some additional features and some syntatic sugar required for abell",
55
"main": "dist/index.js",
66
"bin": {

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ const execRegexOnAll = (regex, template) => {
4444
*/
4545
function render(abellTemplate, sandbox, options = {basePath: ''}) {
4646
// Finds all the JS expressions to be executed.
47-
const {matches, input} = execRegexOnAll(/{{(.*?)}}/gs, abellTemplate);
47+
const {matches, input} = execRegexOnAll(/\\?{{(.+?)}}/gs, abellTemplate);
4848
let renderedHTML = '';
4949
let lastIndex = 0;
5050

5151
for (const match of matches) { // Loops Through JavaScript blocks inside '{{' and '}}'
5252
let value = '';
53-
if (match[1].includes('require(')) {
53+
if (match[0].startsWith('\\{{')) {
54+
// Ignore the match that starts with slash '\' and return the same value without slash
55+
value = match[0].slice(1);
56+
} else if (match[1].includes('require(')) {
5457
// the js block is trying to require (e.g const module1 = require('module1'))
5558
const lines = match[1]
5659
.trim()

tests/index.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ describe('render() - renders abellTemplate into HTML Text', () => {
8383
).trim()
8484
).to.equal('<div>8 hi/hello hi/hello</div>');
8585
});
86+
87+
it('should not throw error and return same value if blank brackets passed', () => {
88+
expect(
89+
abellRenderer.render(
90+
'{{}}',
91+
{}
92+
)
93+
).to.equal('{{}}');
94+
});
95+
96+
it('should ignore the brackets when slash is added before the bracket', () => {
97+
expect(
98+
abellRenderer.render(
99+
'\\{{ This is ignored }}',
100+
{}
101+
)
102+
).to.equal('{{ This is ignored }}');
103+
});
86104
});
87105

88106

tests/resources/if-input.abell

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
when {{numberFromJS}} is added to {{numberFromJSON}}, we get {{ numberFromJS + numberFromJSON }}
2424
<h1>{{ globalMeta.siteName }}</h1>
2525
<div class="header-bio">
26-
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>.
26+
Abell Minima is a starter blog for AbellJS.
27+
<br/>This template is designed by <b>{{ globalMeta.name }}</b>
28+
You can follow him on <a href="https://twitter.com/{{ globalMeta.twitter }}">Twitter @{{ globalMeta.twitter }}</a>.
29+
\{{This text is ignored}}
2730
</div>
2831
</header>
2932
<main>

tests/resources/should-output.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
when 35 is added to 34, we get 69
1212
<h1>This is my siteName</h1>
1313
<div class="header-bio">
14-
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>.
14+
Abell Minima is a starter blog for AbellJS.
15+
<br/>This template is designed by <b>This is my name</b>
16+
You can follow him on <a href="https://twitter.com/saurabhcodes">Twitter @saurabhcodes</a>.
17+
{{This text is ignored}}
1518
</div>
1619
</header>
1720
<main>

0 commit comments

Comments
 (0)