@gov-cy/dsf-email-templates 1.0.8

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.
@@ -0,0 +1,25 @@
1
+ name: Mailtrap test
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - main
7
+ paths:
8
+ - '**'
9
+
10
+ jobs:
11
+ mailtrap-test:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Use Node.js
16
+ uses: actions/setup-node@v2
17
+ with:
18
+ node-version: 18
19
+ - name: Install dependencies
20
+ run: npm install
21
+ - name: Mailtrap test
22
+ env:
23
+ MAILTRAP_USERNAME: ${{ secrets.MAILTRAP_USERNAME }}
24
+ MAILTRAP_PASSWORD: ${{ secrets.MAILTRAP_PASSWORD }}
25
+ run: npm run test-mailtrap-njk ./src/njk/templates/submission-email.njk # Run the mailtrap-njk.js script
@@ -0,0 +1,63 @@
1
+ name: Tag and Publish on Version Change
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ paths:
8
+ - '**'
9
+ jobs:
10
+ # Duplicated from `.github/workflows/version-sanity-checks.yml` could use https://github.blog/2022-02-10-using-reusable-workflows-github-actions ?
11
+ tag-and-publish-on-version-change:
12
+ name: Tag and publish on version change
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - uses: EndBug/version-check@v2.1.0
17
+ with:
18
+ static-checking: localIsNew
19
+ file-url: https://raw.githubusercontent.com/gov-cy/dsf-email-template/latest/package.json
20
+ id: version-check
21
+ - name: Check version not decreased
22
+ if: steps.version-check.outputs.changed == 'true' && steps.version-check.outputs.type == 'undefined'
23
+ run: echo "You should NEVER decrement/ decrease the version in the package.json. This will undermine confidence in our ability to follow semver rules"; exit 1
24
+ - name: Check package.lock::version updated if ./dist changed
25
+ if: steps.version-check.outputs.changed == 'false'
26
+ run: echo "You have updated the distribution without incrementing the version number. You must update package.json::version to publish a new version"; exit 1
27
+ - name: Create 'vX.X.X' version git tag
28
+ uses: actions/github-script@v5
29
+ with:
30
+ script: |
31
+ github.rest.git.createRef({
32
+ owner: context.repo.owner,
33
+ repo: context.repo.repo,
34
+ ref: 'refs/tags/v' + '${{ steps.version-check.outputs.version }}',
35
+ sha: context.sha
36
+ })
37
+ - uses: dev-drprasad/delete-tag-and-release@v0.2.1
38
+ with:
39
+ tag_name: latest
40
+ env:
41
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42
+ - name: Create 'latest' git tag
43
+ uses: actions/github-script@v5
44
+ with:
45
+ script: |
46
+ github.rest.git.createRef({
47
+ owner: context.repo.owner,
48
+ repo: context.repo.repo,
49
+ ref: 'refs/tags/latest',
50
+ sha: context.sha
51
+ })
52
+ # Publish on npm
53
+ - name: Setup Node
54
+ uses: actions/setup-node@v2
55
+ with:
56
+ node-version: '18.x'
57
+ registry-url: 'https://registry.npmjs.org'
58
+ - name: Install dependencies
59
+ run: npm install
60
+ - name: Publish package on NPM 📦
61
+ run: npm publish --access=public
62
+ env:
63
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,22 @@
1
+ name: Unit test
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - main
7
+ paths:
8
+ - '**'
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Use Node.js
16
+ uses: actions/setup-node@v2
17
+ with:
18
+ node-version: 18
19
+ - name: Install dependencies
20
+ run: npm install
21
+ - name: Test
22
+ run: npm test
package/CHANGELOG.md ADDED
@@ -0,0 +1,76 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [v1.0.7] - 2024-04-22
9
+
10
+ ### Added
11
+ - publish to NPM in workflow
12
+
13
+ ### Changed
14
+ - Changed mailtrap workflow to built the email html before it send's it
15
+
16
+ ## [v1.0.6] - 2024-04-21
17
+
18
+ ### Added
19
+ - mailtrap workflow
20
+
21
+ ## [v1.0.4] - 2024-04-20
22
+
23
+ ### Added
24
+ - tag and version workflow
25
+ - unit test workflow
26
+
27
+ ## [v1.0.4] - 2024-04-20
28
+
29
+ ### Added
30
+ - tag and version workflow
31
+ - unit test workflow
32
+
33
+ ## [v1.0.3] - 2024-04-20
34
+
35
+ ### Changed
36
+ - Updated `bodyList` to have better margin in Outlook
37
+
38
+ ## [v1.0.2] - 2024-04-14
39
+
40
+ ### Added
41
+ - `DSFEmailRenderer.renderFromJson`
42
+ - `DSFEmailRenderer.renderFromString`
43
+ - `DSFEmailRenderer.renderFromFile`
44
+ - `DSFEmailRenderer.saveFile`
45
+ - Added unit tests for generated HTML `test\moca\unit.test.js`
46
+ - Added mailtrap tests that generates and sends email from njk file `test\mailtrap-njk.js`
47
+
48
+ ### Changed
49
+ - Updates to load njk directory when package is linked to another project
50
+ - Changed the way `DSFEmailRenderer`. Now everything revolves around the `renderFromString` function
51
+ - Changed `bodyHeading.njk`, `bodyList.njk`, `bodyHeading.njk` to include reusable code from paragraph
52
+
53
+ ### Fixed
54
+ - Fixed `bodyList` to be a paragraph of it's own
55
+
56
+ ## [v1.0.1] - 2024-04-11
57
+
58
+ Complete restructure to prepare to be used as an NPM Package
59
+
60
+ ### Added
61
+ - `CHANGELOG.md`
62
+ - Module classes `src\DSFEmailRender.mjs` and `src\index.mjs`
63
+ - Command line use with `bin\dsf-email-templater.js` and `bin\dsf-email-templater.cmd`
64
+
65
+ ### Changed
66
+ - Complete restructure to work with a DSFEmailRender class and prepare to be used as an NPM Package
67
+ - Updated `package.json` to work as type module and prepare to be used as NPM Package
68
+ - Updated `README.md`
69
+ - Updated `mailtrap.js` to work as ES Module
70
+
71
+ ### Removed
72
+ - `dist` folder and all files not used with the new structure.
73
+
74
+ ## [v1.0.0] - 2024-04-10
75
+ ### Changed
76
+ - Total redesign. Removed mjml, added nunjucks templates to built emails the DSF way
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 gov-cy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/NOTES.md ADDED
@@ -0,0 +1,49 @@
1
+ Notes for the project
2
+
3
+ ## Testing
4
+
5
+ ### Unit tests
6
+
7
+ ....
8
+
9
+ ### Mailtrap
10
+
11
+ Send test email on you mailtrap account.
12
+
13
+ To run the script takes 1 argument which is the path of HTML file for the body of the email
14
+
15
+ for example:
16
+
17
+ ```shell
18
+ npm run test build/submission-email.html
19
+ ```
20
+
21
+ Note that to use you need to set the mailtrap username and password as follows:
22
+
23
+ On powershell
24
+ ```shell
25
+ $env:MAILTRAP_USERNAME = 'xxxxxxxxx'
26
+ $env:MAILTRAP_PASSWORD = 'yyyyyyyy'
27
+ ```
28
+
29
+ On windows
30
+ ```shell
31
+ set MAILTRAP_USERNAME=xxxxxxxxx
32
+ set MAILTRAP_PASSWORD=yyyyyyyy
33
+ ```
34
+
35
+ On Unix
36
+ ```shell
37
+ export MAILTRAP_USERNAME=xxxxxxxxx
38
+ export MAILTRAP_PASSWORD=yyyyyyyy
39
+ ```
40
+
41
+ in javascript
42
+ ```javascript
43
+ cap['browserstack.username'] = process.env.MAILTRAP_USERNAME || 'xxxxxxxxx';
44
+ cap['browserstack.accessKey'] = process.env.MAILTRAP_PASSWORD || 'yyyyyyyy';
45
+ ```
46
+
47
+ ### Putsmail
48
+
49
+ Use https://www.putsmail.com/ to send sample html emails
package/README.md ADDED
@@ -0,0 +1,394 @@
1
+ ![License](https://img.shields.io/github/license/gov-cy/dsf-email-template)
2
+ [![Unit test](https://github.com/gov-cy/dsf-email-template/actions/workflows/unit-test.yml/badge.svg)](https://github.com/gov-cy/dsf-email-template/actions/workflows/unit-test.yml)
3
+ [![tag-and-publish-on-version-change](https://github.com/gov-cy/dsf-email-template/actions/workflows/tag-and-publish-on-version-change.yml/badge.svg)](https://github.com/gov-cy/dsf-email-template/actions/workflows/tag-and-publish-on-version-change.yml)
4
+ [![tag-and-publish-on-version-change](https://github.com/gov-cy/dsf-email-template/actions/workflows/mailtrap-test.yml/badge.svg)](https://github.com/gov-cy/dsf-email-template/actions/workflows/mailtrap-test.yml)
5
+
6
+ Use this project to produce html email templates to be used by DSF. These emails should:
7
+
8
+ - Follow the HTML best practices for emails (for example max-width: 600px, use of inline styling)
9
+ - Minimize the risk of being stuck in “SPAM”
10
+ - Make sure it works well with most email clients
11
+ - Be responsive (work well on desktop and mobiles)
12
+ - Follow accessibility guidelines
13
+ - Include the gov.cy branding
14
+
15
+ The project uses [nunjucks](https://mozilla.github.io/nunjucks/) templates to built the email html.
16
+
17
+ The project also uses https://mailtrap.io/ for spam analysis and to verify HTML compatibility
18
+
19
+ Compatibility with clients based on mailtrap test on 2024-04-10
20
+
21
+ | Client | Desktop | Mobile | Web |
22
+ | ------ | ------- | ------ |----- |
23
+ | Apple mail | 97% | 95% | 100% |
24
+ | Gmail | 100% | 89% | 92% |
25
+ | Outlook | 80% | 93% | 94% |
26
+ | Yahoo Mail | 100% | 80% | 80% |
27
+ | Other | 100% | 89% | 89% |
28
+
29
+ ## Features
30
+
31
+ `DSF-email-templates` can:
32
+ - Generate email HTML from input nunjucks template, using the project's base template and macros.
33
+ - Generate email HTML from input JSON data
34
+
35
+ It can be used by importing the class in your code, or from the command line using file input.
36
+
37
+ ## Install
38
+
39
+ First, install the DSF Email Templates package using npm:
40
+
41
+ ```shell
42
+ npm install @gov-cy/dsf-email-templates
43
+ ```
44
+
45
+ ## Project structure
46
+
47
+ - `bin` contains the command line tools
48
+ - `src` contains the source files
49
+ - `src\njk` contain the nunjucks source base and macros
50
+ - `src\templates` contain the template files used to build the html files
51
+ - `build` contains the build HTML files
52
+ - `test` contains the scripts for testing
53
+
54
+ ## Using it Programmatically
55
+
56
+ You can use the `DSFEmailRenderer` class to render email templates programmatically in your Node.js applications.
57
+
58
+ The class allows you to render HTML email using various input sources such as nunjucks template or JSON objects.
59
+
60
+ ### Method 1: Using nunjucks template for input
61
+
62
+ You can use nunjucks template as input to render the desired HTML email using DSF base template and macro components.
63
+
64
+ Render email HTML directly from a string template using the `renderFromString` method. Here's an example:
65
+
66
+ ```js
67
+ import { DSFEmailRenderer } from '@gov-cy/dsf-email-templates';
68
+
69
+ // Create an instance of DSFEmailRenderer
70
+ const renderer = new DSFEmailRenderer();
71
+
72
+ const inputString = `
73
+ {% extends "govcyBase.njk" %}
74
+ {% from "govcyEmailElement.njk" import govcyEmailElement %}
75
+ {% set lang='en'%}
76
+ {% block lang %}{{lang}}{% endblock %}
77
+ {% block subject %}Service email{% endblock %}
78
+ {% block header -%}{{ govcyEmailElement ('header',{serviceName:'Service name', name:'First and Last name',lang:lang}) }}{%- endblock %}
79
+ {% block body -%}
80
+ {% call govcyEmailElement('body') -%}
81
+ {% call govcyEmailElement('bodyHeading',{headinLevel:1}) -%}Heading 1{%- endcall %}
82
+ {% call govcyEmailElement('bodyParagraph') -%}Paragraph{%- endcall %}
83
+ {% endcall %}
84
+ {% endblock %}
85
+ {% block footer -%}
86
+ {{ govcyEmailElement ('footer',{footerText:'Service name'}) }}
87
+ {%- endblock %}
88
+ `;
89
+
90
+ //Render the email template
91
+ const renderedTemplate = renderer.renderFromString(inputString);
92
+
93
+ console.log(renderedTemplate); // Output the rendered template
94
+ ```
95
+
96
+ You may also use the `renderFromFile` and provide a text file with the nunjucks input template as follows:
97
+
98
+ ```js
99
+ import DSFEmailRenderer from '@gov-cy/dsf-email-templates';
100
+
101
+ // Create an instance of DSFEmailRenderer
102
+ const renderer = new DSFEmailRenderer();
103
+
104
+ // Specify the path of the template file
105
+ const templatePath = 'path/to/template.njk';
106
+
107
+ // Render the email template from the input file
108
+ const renderedTemplate = await renderer.renderFromFile(templatePath);
109
+
110
+ console.log(renderedTemplate); // Output the rendered template
111
+
112
+ ```
113
+
114
+ #### Nunjucks template with DSF assets
115
+
116
+ To use the DSF base template start with the following line in your template:
117
+
118
+ ```njk
119
+ {% extends "govcyBase.njk" %}
120
+ ```
121
+
122
+ You can then use various the base template's blocks to built the structure of the email. The available blocks are:
123
+ - **lang**: Sets the lang html property
124
+ - **subject**: Set's the html title. Usually the same as the intended email subject
125
+ - **pre**: Pre header text that is invisible in the html body
126
+ - **header**: The header of the email with the gov.cy logo and name of the service or site
127
+ - **success**: An area dedicated to showing the success panel. To be used when sending emails after successful submissions from a service.
128
+ - **body**: The main body of the email.
129
+ - **footer**: The footer of the email
130
+
131
+ Here's an example using the blocks of the base template. Note that this will only render the HTML email structure and not the UI components.
132
+
133
+ ```njk
134
+ {% extends "govcyBase.njk" %}
135
+ {% set lang='el'%}
136
+ {% block lang %}{{lang}}{% endblock %}
137
+ {% block subject %}Service email{% endblock %}
138
+ {% block pre -%}The pre header text'{%- endblock %}
139
+ {% block header -%}The header{%- endblock %}
140
+ {% block success -%}Success message{%- endblock %}
141
+ {% block body -%}The Body{% endblock %}
142
+ {% block footer -%}The footer{%- endblock %}
143
+ ```
144
+
145
+ To render the UI components use the `govcyEmailElement` macro. To do that first you need to import the macro by adding the following line:
146
+
147
+ ```njk
148
+ {% from "govcyEmailElement.njk" import govcyEmailElement %}
149
+ ```
150
+
151
+ To use the `govcyEmailElement` macro you need to define the component name and it's parameters. The available components the following:
152
+
153
+ <details>
154
+ <summary>preHeader</summary>
155
+
156
+ Returns govcy pre header for emails html. It should be used in the `pre` block.
157
+
158
+ **Parameters**
159
+ - {string} `preText` The pre header text. Will escape text
160
+
161
+ Usage example:
162
+
163
+ ```njk
164
+ {{ govcyEmailElement
165
+ (
166
+ 'preHeader',
167
+ {
168
+ preText:'The pre header text'
169
+ }
170
+ )
171
+ }}
172
+ ```
173
+ </details>
174
+
175
+ <details>
176
+ <summary>header</summary>
177
+
178
+ Returns govcy header for emails html. It should be used in the `header` block.
179
+
180
+ **Parameters**
181
+ - {string} `serviceName` The service name. Will escape text
182
+ - {string} `name` The name used in salutation. Will escape text
183
+ - {string} `lang` The language used. Can be 'en','el'. Optional, default is 'el'.
184
+
185
+ Usage example:
186
+
187
+ ```njk
188
+ {{ govcyEmailElement
189
+ (
190
+ 'header',
191
+ {
192
+ serviceName:'Service name',
193
+ name:'First and Last name',
194
+ lang:'en'
195
+ }
196
+ )
197
+ }}
198
+ ```
199
+ </details>
200
+
201
+ <details>
202
+ <summary>body</summary>
203
+
204
+ Returns govcy body for emails html. It should be used in the `body` block. Accepts no parameters and will return the content available inside the macro.
205
+
206
+ Usage example:
207
+
208
+ ```njk
209
+ {% call govcyEmailElement('body') %}
210
+ BODY
211
+ {% endcall%}
212
+ ```
213
+ </details>
214
+
215
+ <details>
216
+ <summary>bodyHeading</summary>
217
+
218
+ Returns govcy an h1, h2, h3 or h3 for emails html. It should be used in the `body` block. It returns the content available inside the macro.
219
+
220
+ **Parameters**
221
+ - {string} `headinLevel` The heading level. Optional, default is 1. Can be 1,2,3,4
222
+
223
+ Usage example:
224
+
225
+ ```njk
226
+ {% call govcyEmailElement('bodyHeading',{headinLevel:2}) -%}
227
+ Heading 2
228
+ {%- endcall %}
229
+ ```
230
+ </details>
231
+
232
+ <details>
233
+ <summary>bodyParagraph</summary>
234
+
235
+ Returns govcy a paragraph. It should be used in the `body` block. It accepts no parameters and returns the content available inside the macro.
236
+
237
+ Usage example:
238
+
239
+ ```njk
240
+ {% call govcyEmailElement('bodyParagraph') -%}
241
+ Lorem ipsum
242
+ {%- endcall %}
243
+ ```
244
+ </details>
245
+
246
+ <details>
247
+ <summary>bodyList</summary>
248
+
249
+ Returns govcy an ordered or un-ordered list. It should be used in the `body` block.
250
+
251
+ **Parameters**
252
+ - {string} `type` The list type. Optional, default is 'ul'. Can be 'ul', 'ol'
253
+ - {array} `items` The array of items to turn onto list items
254
+
255
+ Usage example:
256
+
257
+ ```njk
258
+ {{ govcyEmailElement ('bodyList',{type:'ol', items: ["item 1", "item 2", "item 3"]}) }}
259
+ {{ govcyEmailElement ('bodyList',{type:'ul', items: ['<a href="#">item 1</a>', "item 2", "item 3"]}) }}
260
+ ```
261
+ </details>
262
+
263
+ ---------
264
+
265
+ Here's a complete example of a nunjucks template using DSF assets:
266
+
267
+ ```njk
268
+ {# extend the base html #}
269
+ {% extends "govcyBase.njk" %}
270
+ {# import components #}
271
+ {% from "govcyEmailElement.njk" import govcyEmailElement %}
272
+ {# set language #}
273
+ {% set lang='el'%}
274
+ {# set the blocks of the base template #}
275
+ {# language block #}
276
+ {% block lang %}{{lang}}{% endblock %}
277
+ {# subject block #}
278
+ {% block subject %}Έκδοση πιστοποιητικού γέννησης{% endblock %}
279
+ {# header block #}
280
+ {% block header -%}
281
+ {# use the header component #}
282
+ {{ govcyEmailElement ('header',{serviceName:'Έκδοση πιστοποιητικού γέννησης', name:'Όνομα Επώνυμο',lang:lang}) }}
283
+ {%- endblock %}
284
+ {# success block #}
285
+ {% block success -%}
286
+ {# use the email success component #}
287
+ {{ govcyEmailElement ('success',
288
+ {
289
+ title:'Το πιστοποιητικό γέννησης που εκδώσατε είναι έτοιμο',
290
+ body:'Αριθμός αναφοράς 123456'}
291
+ ) }}
292
+ {%- endblock %}
293
+ {# body block #}
294
+ {% block body -%}
295
+ {# use the body component #}
296
+ {% call govcyEmailElement('body') -%}
297
+ {# use combinatopn of body components to complete the body #}
298
+ {% call govcyEmailElement('bodyParagraph') -%}
299
+ Η ημερομηνία έκδοσης είναι 1/1/2019 και ο αριθμός αναφοράς σας είναι 123456.
300
+ {%- endcall %}
301
+ {{ govcyEmailElement ('bodyList',
302
+ {type:'ol',
303
+ items: [
304
+ "<b>Ονοματεπώνυμο</b>: Όνομα Επώνυμο",
305
+ "<b>Αριθμός ταυτότητας</b>: 123456",
306
+ "<b>Ημερομηνία έκδοσης</b>: 1/1/2019"
307
+ ]
308
+ })
309
+ }}
310
+ {% call govcyEmailElement('bodyParagraph') -%}
311
+ Μπορείτε να τα κατεβάσετε στη σελίδα <a href="#">Τα έγγραφά μου</a> μαζί με όσα έγγραφα έχετε εκδώσει.
312
+ {%- endcall %}
313
+ {% call govcyEmailElement('bodyParagraph') -%}
314
+ Στη σελίδα <a href="#">Οι συναλλαγές μου</a> μπορείτε να βρείτε την απόδειξη πληρωμής και τις συναλλαγές σας.
315
+ {%- endcall %}
316
+ {% call govcyEmailElement('bodyParagraph') -%}
317
+ Αν έχετε ερωτήσεις, επικοινωνήστε με το Τμήμα Αρχείου Πληθυσμού και Μετανάστευσης στο migration@crmd.moi.gov.cy. Συμπεριλάβετε τον αριθμό αναφοράς 123456.
318
+ {%- endcall %}
319
+ {% call govcyEmailElement('bodyParagraph') -%}
320
+ Μην απαντήσετε σε αυτό το email.
321
+ {%- endcall %}
322
+
323
+ {%- endcall%}
324
+ {%- endblock %}
325
+ {# footer block #}
326
+ {% block footer -%}
327
+ {# use the footer component #}
328
+ {{ govcyEmailElement ('footer',{footerText:'Έκδοση πιστοποιητικού γέννησης'}) }}
329
+ {%- endblock %}
330
+ ```
331
+
332
+ ### Method 2: Using JSON object for input
333
+
334
+ You can use JSON object as input to render the desired HTML email using the DSF base templare and macro components, by using the the `renderFromJson` method. Here's an example:
335
+
336
+ ```js
337
+ import { DSFEmailRenderer } from '@gov-cy/dsf-email-templates';
338
+
339
+ // Create an instance of DSFEmailRenderer
340
+ const renderer = new DSFEmailRenderer();
341
+
342
+ const inputJson={
343
+ lang: "el",
344
+ subject: "Service email",
345
+ pre: "The pre header text",
346
+ header: {serviceName:'Service name', name:'First and Last name'},
347
+ success: {
348
+ title:'title part',
349
+ body:'body part'
350
+ },
351
+ body: [
352
+ {"component": "bodyHeading",params: '{"headinLevel":1}',body:"Heading 1"},
353
+ {"component": "bodyParagraph", body:"Paragraph"},
354
+ {"component": "bodyList", params: 'type:"ol", items: ["item 1", "item 2", "item 3"]'}
355
+ ],
356
+ footer: {
357
+ footerText: "Name of service"
358
+ }
359
+ }
360
+
361
+ //Render the email template
362
+ const renderedTemplate = renderer.renderFromJson(inputJson);
363
+
364
+ console.log(renderedTemplate); // Output the rendered template
365
+ ```
366
+
367
+ ## Using it from Command line
368
+
369
+ Use `dsf-email-templater` to built the email html file by defining the input and output as arguments.
370
+
371
+ The basic usage syntax is:
372
+
373
+ ```shell
374
+ dsf-email-templater <input.njk> <output.html>
375
+ ```
376
+
377
+ Replace `<input.njk>` with the path to your Nunjucks template file and `<output.html>` with the desired path for the rendered HTML output file.
378
+
379
+ For example:
380
+
381
+ ```shell
382
+ dsf-email-templater .\src\built-with-nunjucks.js .\src\njk\templates\submission-email.njk .\build\submission-email.html
383
+ ```
384
+
385
+ Note for non-Windows users: you may need to replace regular slashes ( `\` ) with backslashes ( `/` ) in all the paths of the above commands.
386
+
387
+ ## License
388
+
389
+ The package is released under the [MIT License](https://opensource.org/licenses/MIT).
390
+
391
+ ## Contact
392
+
393
+ If you have any questions or feedback, please feel free to reach out to us at [dsf-admin@dits.dmrid.gov.cy](mailto:dsf-admin@dits.dmrid.gov.cy)
394
+
@@ -0,0 +1,5 @@
1
+ @IF EXIST "%~dp0\node.exe" (
2
+ "%~dp0\node.exe" "%~dp0\dsf-email-templater.js" %*
3
+ ) ELSE (
4
+ node "%~dp0\dsf-email-templater.js" %*
5
+ )
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { DSFEmailRenderer } from '../src/index.mjs';
4
+
5
+ const args = process.argv.slice(2);
6
+
7
+ if (args.length !== 2) {
8
+ console.error('Usage: dsf-email-templater <input.njk> <output.html>');
9
+ process.exit(1);
10
+ }
11
+
12
+ const [inputPath, outputPath] = args;
13
+
14
+ const renderer = new DSFEmailRenderer();
15
+
16
+ (async () => {
17
+ try {
18
+ const inputTemplate = await renderer.renderFromFile(inputPath);
19
+ await renderer.saveFile(inputTemplate, outputPath);
20
+ } catch (error) {
21
+ console.error(error);
22
+ process.exit(1);
23
+ }
24
+ })();