feat(nunjucks): Add link filter to nunjucks operator.

This commit is contained in:
StephanieJKS 2024-10-31 14:48:19 +02:00
parent 5587322036
commit 156fa7f2eb
No known key found for this signature in database
5 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'@lowdefy/nunjucks': minor
---
Add 'link' filter to the nunjucks operator.

View File

@ -117,3 +117,17 @@ _ref:
<p>India</p>
</div>
```
###### Make use of the _nunjucks link filter:
```yaml
_nunjucks:
template: {{ url | link(urlQuery) | safe }}
on:
url: www.lowdefy.com
urlQuery:
id: 1234
type: example
```
Returns: `"www.lowdefy.com?id=1234&type=example"`

View File

@ -18,12 +18,14 @@ import nunjucks from 'nunjucks';
import { type } from '@lowdefy/helpers';
import dateFilter from './dateFilter.js';
import uniqueFilter from './uniqueFilter.js';
import linkFilter from './linkFilter.js';
// dateFilter.setDefaultFormat('YYYY-MM-DD');
export const nunjucksEnv = new nunjucks.Environment();
nunjucksEnv.addFilter('date', dateFilter);
nunjucksEnv.addFilter('unique', uniqueFilter);
nunjucksEnv.addFilter('link', linkFilter);
const nunjucksTemplates = {};
// slow

View File

@ -0,0 +1,29 @@
/*
Copyright 2020-2024 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { type, urlQuery as urlQueryFn } from '@lowdefy/helpers';
const linkFilter = (url, urlQuery) => {
const query = type.isNone(urlQuery) ? '' : `${urlQueryFn.stringify(urlQuery)}`;
if (type.isString(url)) {
return `${url}${query ? `?${query}` : ''}`;
}
return undefined;
};
export default linkFilter;

View File

@ -0,0 +1,95 @@
/*
Copyright 2020-2024 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import nunjucks from 'nunjucks';
import linkFilter from './linkFilter.js';
export const nunjucksEnv = new nunjucks.Environment();
nunjucksEnv.addFilter('link', linkFilter);
describe('linkFilter', () => {
test('should return only url', () => {
const input = {
url: 'test',
};
const output = linkFilter(input.url);
expect(output).toEqual('test');
});
test('should return url with query parameters', () => {
const input = {
url: 'test',
urlQuery: {
a: 1,
b: 'b',
},
};
const output = linkFilter(input.url, input.urlQuery);
expect(output).toEqual('test?a=1&b=b');
});
test('should return undefined when no url provided', () => {
const output = linkFilter();
expect(output).toEqual(undefined);
});
test('should return undefined when null url provided', () => {
const input = {
url: null,
urlQuery: {
a: 1,
b: 'b',
},
};
const output = linkFilter(input.url, input.urlQuery);
expect(output).toEqual(undefined);
});
test('should install link filter in nunjucks and return url', () => {
const templateString = '{{ url | link }}';
const input = {
url: 'test',
};
const output = nunjucksEnv.renderString(templateString, input);
expect(output).toBe('test');
});
test('should install link filter in nunjucks and return url with query parameters separated by &', () => {
const templateString = '{{ url | link(urlQuery) | safe }}';
const input = {
url: 'test',
urlQuery: {
a: 1,
b: 'b',
},
};
const output = nunjucksEnv.renderString(templateString, input);
expect(output).toBe('test?a=1&b=b');
});
test('should install link filter in nunjucks and return url with query parameters separated by &amp;', () => {
const templateString = '{{ url | link(urlQuery) }}';
const input = {
url: 'test',
urlQuery: {
a: 1,
b: 'b',
},
};
const output = nunjucksEnv.renderString(templateString, input);
expect(output).toBe('test?a=1&amp;b=b');
});
});