@fractality/nunjucks 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -0
- package/LICENSE +22 -0
- package/README.md +199 -0
- package/index.js +3 -0
- package/package.json +30 -0
- package/src/AsyncNunjucksEnvironment.js +33 -0
- package/src/adapter.js +143 -0
- package/src/extensions/context.js +37 -0
- package/src/extensions/index.js +13 -0
- package/src/extensions/render.js +54 -0
- package/src/extensions/view.js +41 -0
- package/src/filters/index.js +9 -0
- package/src/filters/path.js +15 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 2.2.0 (2024-09-20)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **engine:** update to remove deprecated flatten behaviour ([b89be05](https://github.com/frctl/fractal/commit/b89be05f091c9eb61b14c510b91eb3c548b849cd))
|
|
11
|
+
- fetch request context for README.md ([#13](https://github.com/frctl/fractal/issues/13)) ([dec6fea](https://github.com/frctl/fractal/commit/dec6feae727801f5a61291cd8cd1e167b37bee18))
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
- **partials:** support new handle-based import syntax ([2719df7](https://github.com/frctl/fractal/commit/2719df71a1e385b21a5c8d0cfb5be173e68269ad))
|
|
16
|
+
- replace bluebird with native promises ([1c8daa0](https://github.com/frctl/fractal/commit/1c8daa09a70962211ce550eff9a930ee3d9a9323))
|
|
17
|
+
|
|
18
|
+
# 2.1.0 (2024-03-19)
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
- **engine:** update to remove deprecated flatten behaviour ([b89be05](https://github.com/frctl/fractal/commit/b89be05f091c9eb61b14c510b91eb3c548b849cd))
|
|
23
|
+
- fetch request context for README.md ([#13](https://github.com/frctl/fractal/issues/13)) ([dec6fea](https://github.com/frctl/fractal/commit/dec6feae727801f5a61291cd8cd1e167b37bee18))
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
- **partials:** support new handle-based import syntax ([2719df7](https://github.com/frctl/fractal/commit/2719df71a1e385b21a5c8d0cfb5be173e68269ad))
|
|
28
|
+
- replace bluebird with native promises ([1c8daa0](https://github.com/frctl/fractal/commit/1c8daa09a70962211ce550eff9a930ee3d9a9323))
|
|
29
|
+
|
|
30
|
+
## [2.0.15](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.14...@frctl/nunjucks@2.0.15) (2022-08-17)
|
|
31
|
+
|
|
32
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
33
|
+
|
|
34
|
+
## [2.0.14](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.13...@frctl/nunjucks@2.0.14) (2022-01-26)
|
|
35
|
+
|
|
36
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
37
|
+
|
|
38
|
+
## [2.0.13](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.12...@frctl/nunjucks@2.0.13) (2021-07-20)
|
|
39
|
+
|
|
40
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
41
|
+
|
|
42
|
+
## [2.0.12](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.11...@frctl/nunjucks@2.0.12) (2021-07-19)
|
|
43
|
+
|
|
44
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
45
|
+
|
|
46
|
+
## [2.0.11](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.10...@frctl/nunjucks@2.0.11) (2021-05-20)
|
|
47
|
+
|
|
48
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
49
|
+
|
|
50
|
+
## [2.0.10](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.9...@frctl/nunjucks@2.0.10) (2021-03-23)
|
|
51
|
+
|
|
52
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
53
|
+
|
|
54
|
+
## [2.0.9](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.8...@frctl/nunjucks@2.0.9) (2021-03-20)
|
|
55
|
+
|
|
56
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
57
|
+
|
|
58
|
+
## [2.0.8](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.7...@frctl/nunjucks@2.0.8) (2021-02-14)
|
|
59
|
+
|
|
60
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
61
|
+
|
|
62
|
+
## [2.0.7](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.6...@frctl/nunjucks@2.0.7) (2021-02-07)
|
|
63
|
+
|
|
64
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
65
|
+
|
|
66
|
+
## [2.0.6](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.5...@frctl/nunjucks@2.0.6) (2020-12-22)
|
|
67
|
+
|
|
68
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
69
|
+
|
|
70
|
+
## [2.0.5](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.4...@frctl/nunjucks@2.0.5) (2020-11-03)
|
|
71
|
+
|
|
72
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
73
|
+
|
|
74
|
+
## [2.0.4](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.3...@frctl/nunjucks@2.0.4) (2020-10-19)
|
|
75
|
+
|
|
76
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
77
|
+
|
|
78
|
+
## [2.0.3](https://github.com/frctl/fractal/compare/@frctl/nunjucks@2.0.2...@frctl/nunjucks@2.0.3) (2020-10-15)
|
|
79
|
+
|
|
80
|
+
**Note:** Version bump only for package @frctl/nunjucks
|
|
81
|
+
|
|
82
|
+
## 2.0.2
|
|
83
|
+
|
|
84
|
+
### Bug Fixes
|
|
85
|
+
|
|
86
|
+
- fetch request context for component notes file (#13)
|
|
87
|
+
|
|
88
|
+
### Chores
|
|
89
|
+
|
|
90
|
+
- bump lodash from 4.17.11 to 4.17.13 (#14)
|
|
91
|
+
|
|
92
|
+
## 2.0.1
|
|
93
|
+
|
|
94
|
+
- Fix issue with `render`and `view` tag output being escaped
|
|
95
|
+
|
|
96
|
+
## 2.0
|
|
97
|
+
|
|
98
|
+
- Update Nunjucks to v3.x
|
|
99
|
+
- Prevent array merging in `render` tag context data
|
|
100
|
+
- Enable autoescaping by default
|
|
101
|
+
- Add ability to provide environment configuration options
|
|
102
|
+
|
|
103
|
+
## 1.0.2
|
|
104
|
+
|
|
105
|
+
- [FIX] Fix issue with path filter in components rendered using the `render` extension.
|
|
106
|
+
|
|
107
|
+
## 1.0.1
|
|
108
|
+
|
|
109
|
+
- Provide support for future changes to `_` ('special') variables setting
|
|
110
|
+
- Update Nunjucks 2.3.0 -> 2.4.2
|
|
111
|
+
|
|
112
|
+
## 1.0.0
|
|
113
|
+
|
|
114
|
+
Initial release.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Mark Perkins <mark@clearleft.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# @fractality/nunjucks
|
|
2
|
+
|
|
3
|
+
[Nunjucks](http://mozilla.github.io/nunjucks/) template engine adapter for [Fractal](http://github.com/fractality/fractality).
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@fractality/nunjucks)
|
|
6
|
+
|
|
7
|
+
## 🚨 Breaking changes in v2.0
|
|
8
|
+
|
|
9
|
+
- `render` tag - Arrays in context data are **no longer merged** with those in the default component context
|
|
10
|
+
- **Autoescaping** - this is now **disabled by default**. Preview templates will need to be updated to mark the `yield` output as safe using the Nunjucks `safe` filter: `{{ yield | safe }}`. Alternatively this can be enabled globally again using the environment options when [configuring the adapter](#customisation) (not recommended).
|
|
11
|
+
- **Nunjucks version** - bumped to 3.x
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i @fractality/nunjucks --save
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
fractal.components.engine('@fractality/nunjucks'); // register the Nunjucks adapter for your components
|
|
23
|
+
fractal.components.set('ext', '.njk'); // look for files with a .njk file extension
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Customisation
|
|
27
|
+
|
|
28
|
+
If you want to register custom [filters](https://mozilla.github.io/nunjucks/api.html#custom-filters), global variables or [extensions](https://mozilla.github.io/nunjucks/api.html#custom-tags) to the underlying Nunjucks engine then you can configure an instance as follows:
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import nunjucksAdapter from '@fractality/nunjucks';
|
|
32
|
+
const nunj = nunjucksAdapter({
|
|
33
|
+
env: {
|
|
34
|
+
// Nunjucks environment opts: https://mozilla.github.io/nunjucks/api.html#configure
|
|
35
|
+
},
|
|
36
|
+
filters: {
|
|
37
|
+
// filter-name: function filterFunc(){}
|
|
38
|
+
},
|
|
39
|
+
globals: {
|
|
40
|
+
// global-name: global-val
|
|
41
|
+
},
|
|
42
|
+
extensions: {
|
|
43
|
+
// extension-name: function extensionFunc(){}
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
fractal.components.engine(nunj); /* set as the default template engine for components */
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
For example, to register the 'shorten' filter example from the [Nujucks docs](https://mozilla.github.io/nunjucks/api.html#custom-filters):
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import nunjucksAdapter from '@fractality/nunjucks';
|
|
54
|
+
const nunj = nunjucksAdapter({
|
|
55
|
+
filters: {
|
|
56
|
+
shorten: function (str, count) {
|
|
57
|
+
return str.slice(0, count || 5);
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Which you could then use in your component or documentation views as follows:
|
|
64
|
+
|
|
65
|
+
```nunjucks
|
|
66
|
+
{# Show the first 5 characters #}
|
|
67
|
+
A message for you: {{ message|shorten }}
|
|
68
|
+
|
|
69
|
+
{# Show the first 20 characters #}
|
|
70
|
+
A message for you: {{ message|shorten(20) }}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Including and extending non-component view templates
|
|
74
|
+
|
|
75
|
+
By default, the Nunjucks adapter expects you to use the Fractality component `@handle` syntax to refer to components to include or extend in your templates.
|
|
76
|
+
|
|
77
|
+
However, if you wish to include (or extend) non-component templates, you can also specify a path (or an array of paths) of directories for Nunjucks to search in for non-component templates when configuring your Nunjucks instance. For example:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
import nunjucksAdapter from '@fractality/nunjucks';
|
|
81
|
+
const nunj = nunjucksAdapter({
|
|
82
|
+
paths: ['path/to/files'],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
{% include 'foo.html' %}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
In this example the file `foo.html` would be searched for in the `path/to/files` directory and included if found.
|
|
91
|
+
|
|
92
|
+
> Using additional search paths in this manner **does not** prevent standard `@handle` syntax includes working as well.
|
|
93
|
+
|
|
94
|
+
## Extensions
|
|
95
|
+
|
|
96
|
+
The following Nunjucks extensions come **automatically pre-installed**. These are often useful when building or documenting Fractal-based component libraries.
|
|
97
|
+
|
|
98
|
+
If you **do not wish** to include these extensions, set `pristine: true` when configuring your Nunjucks adapter instance.
|
|
99
|
+
|
|
100
|
+
### render
|
|
101
|
+
|
|
102
|
+
The `render` extension renders a component (referenced by it's handle) using the context data provided to it in the template. If no data is provided, it will use the context data defined within it's configuration file, if present.
|
|
103
|
+
|
|
104
|
+
**This can be very useful as an alternative to using an `include` to import sub-components.** `Include`'d components do not pull in their own context so using `render` instead can help prevent repetition of context data in the configuration files of components that include sub-components.
|
|
105
|
+
|
|
106
|
+
```html
|
|
107
|
+
<!-- pass in data for rendering -->
|
|
108
|
+
{% render '@example', {title: 'An Example'} %} {% render '@example', someData %}
|
|
109
|
+
|
|
110
|
+
<!-- use the config file data for rendering -->
|
|
111
|
+
{% render '@example' %}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
You can also pass in a _partial_ data object (i.e. containing only some of the properties the component expects) and then pass a third argument of `true` to the tag to populate the missing items from the default context data. This allows you to override only the items you need to for this instance of the rendered component.
|
|
115
|
+
|
|
116
|
+
```html
|
|
117
|
+
<!-- will get any missing properties from the component context data -->
|
|
118
|
+
{% render '@another-example', {title: 'Another Example'}, true %}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### context
|
|
122
|
+
|
|
123
|
+
Outputs the resolved context data for a component.
|
|
124
|
+
|
|
125
|
+
```html
|
|
126
|
+
{% context '@example' %}
|
|
127
|
+
|
|
128
|
+
<!--
|
|
129
|
+
Outputs:
|
|
130
|
+
{
|
|
131
|
+
"foo": "bar",
|
|
132
|
+
"baz": "bar"
|
|
133
|
+
}
|
|
134
|
+
-->
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### view
|
|
138
|
+
|
|
139
|
+
Outputs the raw view template contents for the specified component.
|
|
140
|
+
|
|
141
|
+
```html
|
|
142
|
+
{% view '@example' %}
|
|
143
|
+
|
|
144
|
+
<!--
|
|
145
|
+
Outputs:
|
|
146
|
+
<p>{{ text }}</p>
|
|
147
|
+
-->
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Filters
|
|
151
|
+
|
|
152
|
+
The following Nunjucks filters come **automatically pre-installed**. As with the extensions, setting `pristine: true` in the Nunjucks adapter instance config will prevent them being added.
|
|
153
|
+
|
|
154
|
+
### path
|
|
155
|
+
|
|
156
|
+
Takes a root-relative path and re-writes it if required to make it work in static HTML exports.
|
|
157
|
+
|
|
158
|
+
**It is strongly recommended to use this filter whenever you need to link to any static assets from your templates.**
|
|
159
|
+
|
|
160
|
+
```html
|
|
161
|
+
{{ '/css/my-stylesheet.css' | path }}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The path argument should begin with a slash and be relative to the web root. During a static HTML export this path will then be re-written to be relative to the current page.
|
|
165
|
+
|
|
166
|
+
## Special variables
|
|
167
|
+
|
|
168
|
+
The Nunjucks adapter also makes a few special variables available to your templates. They all have names prefixed with an underscore to help prevent clashes with any context data variables that are set by the user.
|
|
169
|
+
|
|
170
|
+
Note that using these may tie your templates a little more tightly into Fractality so you may choose not to use them for that reason.
|
|
171
|
+
|
|
172
|
+
### \_config
|
|
173
|
+
|
|
174
|
+
Contains the full Fractality configuration object. Useful for when you want to refer to a configuration item in your documentation (or components).
|
|
175
|
+
|
|
176
|
+
```html
|
|
177
|
+
{{ _config.project.title }}
|
|
178
|
+
<!-- outputs the project title -->
|
|
179
|
+
{{ _config.components.ext }}
|
|
180
|
+
<!-- outputs the extension used for components -->
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### \_self
|
|
184
|
+
|
|
185
|
+
Contains a simple data object representation of the top-level item (i.e. component or page) being rendered.
|
|
186
|
+
|
|
187
|
+
```html
|
|
188
|
+
{{ _self.title }}
|
|
189
|
+
<!-- outputs 'Button' -->
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### \_target
|
|
193
|
+
|
|
194
|
+
This variable is only set in component preview layouts, and contains a simple data object representation of the item (i.e. component or page) being rendered _within_ the preview layout.
|
|
195
|
+
|
|
196
|
+
```html
|
|
197
|
+
{{ _target.title }}
|
|
198
|
+
<!-- outputs 'Button' -->
|
|
199
|
+
```
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fractality/nunjucks",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Nunjucks template adapter for Fractal.",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/frctl/fractal.git",
|
|
10
|
+
"directory": "packages/nunjucks"
|
|
11
|
+
},
|
|
12
|
+
"author": "Mark Perkins <mark@clearleft.com> (http://clearleft.com/)",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/frctl/fractal/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/frctl/fractal/tree/main/packages/nunjucks",
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"@fractality/fractality": ">= 1.1.0-alpha.0 < 2"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@fractality/core": "^0.5.0",
|
|
23
|
+
"lodash": "^4.17.21",
|
|
24
|
+
"nunjucks": "^3.2.4"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "2a5ad3e079ce7a9a418642377a6b6e07bb243c71"
|
|
30
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Environment } from 'nunjucks';
|
|
2
|
+
|
|
3
|
+
export class AsyncNunjucksEnvironment extends Environment {
|
|
4
|
+
render(path, context) {
|
|
5
|
+
return new Promise((resolve, reject) =>
|
|
6
|
+
super.render(path, context, (err, res) => {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
}
|
|
10
|
+
resolve(res);
|
|
11
|
+
}),
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
renderString(path, context) {
|
|
16
|
+
return new Promise((resolve, reject) =>
|
|
17
|
+
super.renderString(path, context, (err, res) => {
|
|
18
|
+
if (err) {
|
|
19
|
+
reject(err);
|
|
20
|
+
}
|
|
21
|
+
resolve(res);
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
renderAsync(path, context) {
|
|
27
|
+
return this.render(path, context);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
renderStringAsync(path, context) {
|
|
31
|
+
return this.renderString(path, context);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/adapter.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from 'lodash';
|
|
4
|
+
import nunjucks from 'nunjucks';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import { Adapter } from '@fractality/core';
|
|
8
|
+
import extensions from './extensions/index.js';
|
|
9
|
+
import filters from './filters/index.js';
|
|
10
|
+
import { AsyncNunjucksEnvironment } from './AsyncNunjucksEnvironment.js';
|
|
11
|
+
|
|
12
|
+
class NunjucksAdapter extends Adapter {
|
|
13
|
+
constructor(source, config, app) {
|
|
14
|
+
super(null, source);
|
|
15
|
+
|
|
16
|
+
const loaders = [];
|
|
17
|
+
const self = this;
|
|
18
|
+
|
|
19
|
+
this._app = app;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a custom string loader and instantiate a new Nunjucks environment object with it.
|
|
23
|
+
* We don't want to use the FileSystemLoader as we already have
|
|
24
|
+
* the contents of all files cached in the component file tree.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const StringLoader = nunjucks.Loader.extend({
|
|
28
|
+
getSource(handle) {
|
|
29
|
+
if (handle.indexOf('@') !== 0) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const view = self.getView(handle);
|
|
33
|
+
if (view) {
|
|
34
|
+
return {
|
|
35
|
+
src: view.content,
|
|
36
|
+
path: view.content,
|
|
37
|
+
noCache: true,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
loaders.push(new StringLoader());
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* If the user has specified any paths for directly loading templates,
|
|
46
|
+
* include a FileSystemLoader instance.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
if (config.paths) {
|
|
50
|
+
const FileSystemLoader = nunjucks.Loader.extend({
|
|
51
|
+
init(searchPaths) {
|
|
52
|
+
this.searchPaths = [].concat(searchPaths);
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
getSource(name) {
|
|
56
|
+
let fullpath = null;
|
|
57
|
+
const paths = this.searchPaths;
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < paths.length; i++) {
|
|
60
|
+
const basePath = path.resolve(paths[i]);
|
|
61
|
+
const p = path.resolve(paths[i], name);
|
|
62
|
+
|
|
63
|
+
if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
|
|
64
|
+
fullpath = p;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!fullpath) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
src: fs.readFileSync(fullpath, 'utf-8'),
|
|
75
|
+
path: fullpath,
|
|
76
|
+
noCache: true,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
loaders.push(new FileSystemLoader(config.paths));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Instantiate the Nunjucks environment instance.
|
|
85
|
+
*/
|
|
86
|
+
this._engine = new AsyncNunjucksEnvironment(loaders, config.env || {});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
render(path, str, context, meta) {
|
|
90
|
+
meta = meta || {};
|
|
91
|
+
setEnv('_self', meta.self, context);
|
|
92
|
+
setEnv('_target', meta.target, context);
|
|
93
|
+
setEnv('_env', meta.env, context);
|
|
94
|
+
setEnv('_config', this._app.config(), context);
|
|
95
|
+
return this.engine.renderStringAsync(str, context);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function setEnv(key, value, context) {
|
|
100
|
+
if (_.isUndefined(context[key]) && !_.isUndefined(value)) {
|
|
101
|
+
context[key] = value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default function (config) {
|
|
106
|
+
config = config || {};
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
register(source, app) {
|
|
110
|
+
const adapter = new NunjucksAdapter(source, config, app);
|
|
111
|
+
const nj = adapter.engine;
|
|
112
|
+
|
|
113
|
+
if (!config.pristine) {
|
|
114
|
+
_.each(filters(app) || {}, function (filter, name) {
|
|
115
|
+
addFilter(name, filter);
|
|
116
|
+
});
|
|
117
|
+
_.each(extensions(app) || {}, function (ext, name) {
|
|
118
|
+
nj.addExtension(name, ext);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_.each(config.filters || {}, function (filter, name) {
|
|
123
|
+
addFilter(name, filter);
|
|
124
|
+
});
|
|
125
|
+
_.each(config.extensions || {}, function (ext, name) {
|
|
126
|
+
nj.addExtension(name, ext);
|
|
127
|
+
});
|
|
128
|
+
_.each(config.globals || {}, function (value, name) {
|
|
129
|
+
nj.addGlobal(name, value);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
function addFilter(name, filter) {
|
|
133
|
+
if (typeof filter === 'function') {
|
|
134
|
+
nj.addFilter(name, filter);
|
|
135
|
+
} else if (typeof filter === 'object') {
|
|
136
|
+
nj.addFilter(name, filter.filter, filter.async);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return adapter;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
export default function (fractal) {
|
|
4
|
+
function ContextExtension() {
|
|
5
|
+
this.tags = ['context'];
|
|
6
|
+
|
|
7
|
+
this.parse = function (parser, nodes) {
|
|
8
|
+
var tok = parser.nextToken();
|
|
9
|
+
var args = parser.parseSignature(null, true);
|
|
10
|
+
parser.advanceAfterBlockEnd(tok.value);
|
|
11
|
+
return new nodes.CallExtensionAsync(this, 'run', args);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
this.run = function () {
|
|
15
|
+
const source = fractal.components;
|
|
16
|
+
const args = Array.from(arguments);
|
|
17
|
+
const callback = args.pop();
|
|
18
|
+
args.shift();
|
|
19
|
+
const handle = args[0];
|
|
20
|
+
const entity = source.find(handle);
|
|
21
|
+
if (!entity) {
|
|
22
|
+
throw new Error(`Could not render component '${handle}' - component not found.`);
|
|
23
|
+
}
|
|
24
|
+
const context = entity.isComponent ? entity.variants().default().context : entity.context;
|
|
25
|
+
source
|
|
26
|
+
.resolve(context)
|
|
27
|
+
.then((ctx) => {
|
|
28
|
+
callback(null, JSON.stringify(ctx, null, 4));
|
|
29
|
+
})
|
|
30
|
+
.catch((err) => {
|
|
31
|
+
callback(err);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new ContextExtension();
|
|
37
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _render from './render.js';
|
|
4
|
+
import _view from './view.js';
|
|
5
|
+
import _context from './context.js';
|
|
6
|
+
|
|
7
|
+
export default function (fractal) {
|
|
8
|
+
return {
|
|
9
|
+
render: _render(fractal),
|
|
10
|
+
view: _view(fractal),
|
|
11
|
+
context: _context(fractal),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import nunjucks from 'nunjucks';
|
|
4
|
+
import { utils } from '@fractality/core';
|
|
5
|
+
|
|
6
|
+
export default function (fractal) {
|
|
7
|
+
function RenderExtension() {
|
|
8
|
+
this.tags = ['render'];
|
|
9
|
+
|
|
10
|
+
this.parse = function (parser, nodes) {
|
|
11
|
+
var tok = parser.nextToken();
|
|
12
|
+
var args = parser.parseSignature(null, true);
|
|
13
|
+
parser.advanceAfterBlockEnd(tok.value);
|
|
14
|
+
return new nodes.CallExtensionAsync(this, 'run', args);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
this.run = function () {
|
|
18
|
+
const source = fractal.components;
|
|
19
|
+
const args = Array.from(arguments);
|
|
20
|
+
const rootContext = args[0].ctx;
|
|
21
|
+
const callback = args.pop();
|
|
22
|
+
args.shift();
|
|
23
|
+
const handle = args[0];
|
|
24
|
+
let context = args[1];
|
|
25
|
+
const merge = args[2] || false;
|
|
26
|
+
const entity = source.find(handle);
|
|
27
|
+
if (!entity) {
|
|
28
|
+
throw new Error(`Could not render component '${handle}' - component not found.`);
|
|
29
|
+
}
|
|
30
|
+
const defaultContext = entity.isComponent ? entity.variants().default().context : entity.context;
|
|
31
|
+
if (!context) {
|
|
32
|
+
context = defaultContext;
|
|
33
|
+
} else if (merge) {
|
|
34
|
+
context = utils.defaultsDeep(context, defaultContext);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
source.resolve(context).then((context) => {
|
|
38
|
+
// fix env for rendered components
|
|
39
|
+
let env = JSON.parse(JSON.stringify(rootContext._env));
|
|
40
|
+
context._env = env;
|
|
41
|
+
entity
|
|
42
|
+
.render(context)
|
|
43
|
+
.then((html) => {
|
|
44
|
+
callback(null, new nunjucks.runtime.SafeString(html));
|
|
45
|
+
})
|
|
46
|
+
.catch((err) => {
|
|
47
|
+
callback(err);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return new RenderExtension();
|
|
54
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import nunjucks from 'nunjucks';
|
|
4
|
+
|
|
5
|
+
export default function (fractal) {
|
|
6
|
+
function ViewExtension() {
|
|
7
|
+
this.tags = ['view'];
|
|
8
|
+
|
|
9
|
+
this.parse = function (parser, nodes) {
|
|
10
|
+
var tok = parser.nextToken();
|
|
11
|
+
var args = parser.parseSignature(null, true);
|
|
12
|
+
parser.advanceAfterBlockEnd(tok.value);
|
|
13
|
+
return new nodes.CallExtensionAsync(this, 'run', args);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
this.run = function () {
|
|
17
|
+
const source = fractal.components;
|
|
18
|
+
const args = Array.from(arguments);
|
|
19
|
+
const callback = args.pop();
|
|
20
|
+
args.shift();
|
|
21
|
+
const handle = args[0];
|
|
22
|
+
let entity = source.find(handle);
|
|
23
|
+
if (!entity) {
|
|
24
|
+
throw new Error(`Could not render component '${handle}' - component not found.`);
|
|
25
|
+
}
|
|
26
|
+
if (entity.isComponent) {
|
|
27
|
+
entity = entity.variants().default();
|
|
28
|
+
}
|
|
29
|
+
entity
|
|
30
|
+
.getContent()
|
|
31
|
+
.then((content) => {
|
|
32
|
+
callback(null, new nunjucks.runtime.SafeString(content));
|
|
33
|
+
})
|
|
34
|
+
.catch((err) => {
|
|
35
|
+
callback(err);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return new ViewExtension();
|
|
41
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from 'lodash';
|
|
4
|
+
import { utils } from '@fractality/core';
|
|
5
|
+
|
|
6
|
+
export default function (fractal) {
|
|
7
|
+
return function (path) {
|
|
8
|
+
let env = this.lookup('_env');
|
|
9
|
+
let request = env.request || this.lookup('_request') || this.ctx.request;
|
|
10
|
+
|
|
11
|
+
return !env || env.server
|
|
12
|
+
? path
|
|
13
|
+
: utils.relUrlPath(path, _.get(request, 'path', '/'), fractal.web.get('builder.urls'));
|
|
14
|
+
};
|
|
15
|
+
}
|