ecto 4.1.2 → 4.1.3

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ![Ecto](site/logo.svg "Ecto")
2
2
 
3
- ## Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Mustache, and Handlebars
3
+ # Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Mustache, and Handlebars
4
4
 
5
5
  [![tests](https://github.com/jaredwray/ecto/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/ecto/actions/workflows/tests.yml)
6
6
  [![Release Status](https://github.com/jaredwray/ecto/workflows/release/badge.svg)](https://github.com/jaredwray/ecto/actions)
@@ -12,19 +12,43 @@
12
12
 
13
13
  Ecto is a modern template consolidation engine that enables the best template engines: EJS, Markdown, Pug, Nunjucks, Mustache, Handlebars, and Liquid. It consolidates these template engines to a single library, allowing you to use any of them with ease.
14
14
 
15
- ## Features
15
+ # Table of Contents
16
+ * [Features](#features)
17
+ * [ESM and Node Version Support](#esm-and-node-version-support)
18
+ * [Getting Started](#getting-started)
19
+ * [Only the Top Template Engines and Their Extensions](#only-the-top-template-engines-and-their-extensions)
20
+ * [API - Methods and Parameters](#api---methods-and-parameters)
21
+ * [Render From String](#render-from-string)
22
+ * [Render From File](#render-from-file)
23
+ * [Default Engine](#default-engine)
24
+ * [Engine Mappings](#engine-mappings)
25
+ * [FrontMatter Helper Functions](#frontmatter-helper-functions)
26
+ * [The Template Engines We Support](#the-template-engines-we-support)
27
+ * [EJS](#ejs)
28
+ * [Markdown](#markdown)
29
+ * [PUG](#pug)
30
+ * [Nunjucks](#nunjucks)
31
+ * [Mustache](#mustache)
32
+ * [Handlebars](#handlebars)
33
+ * [Liquid](#liquid)
34
+ * [FrontMatter Helper Functions](#frontmatter-helper-functions)
35
+ * [How to Contribute](#how-to-contribute)
36
+ * [License](#license)
37
+
38
+ # Features
16
39
 
17
40
  * Zero Config by default but all properties exposed for flexibility. Check out our easy [API.](#api)
18
41
  * Async `render` and `renderFromFile` functions for ES6 and Typescript.
19
42
  * [Render via Template File](#render-from-file) with Automatic Engine Selection. No more selecting which engine to use, engine choice is automatically decided based on the file extension.
20
43
  * [Only the Top Template Engines](#only-the-top-template-engines-and-their-extensions): EJS, Markdown, Pug, Nunjucks, Mustache, Liquid, and Handlebars.
44
+ * FrontMatter Helpers `.hasFrontMatter`, `.getFrontMatter`, and `.removeFrontMatter` for Markdown files.
21
45
  * Maintained with Monthly Updates!
22
46
 
23
- ## ESM and Node Version Support
47
+ # ESM and Node Version Support
24
48
 
25
49
  This package is ESM only and tested on the current lts version and its previous. Please don't open issues for questions regarding CommonJS / ESM or previous Nodejs versions. To learn more about using ESM please read this from `sindresorhus`: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
26
50
 
27
- ## Getting Started
51
+ # Getting Started
28
52
 
29
53
  The [Node.js package manager documentation](https://nodejs.org/en/download/package-manager/) provides the commands needed to complete the install on Windows and other operating systems.
30
54
 
@@ -81,23 +105,7 @@ let data = { firstName: "John", lastName: "Doe"};
81
105
  await ecto.renderFromFile("./path/to/template.ejs", data); // returns <h1>Hello John Doe!</h1>
82
106
  ```
83
107
 
84
- Next Steps:
85
- * [More examples on Render from String](#render-from-string)
86
- * [More examples on Render from File](#render-from-file)
87
- * [Examples on setting the Default Engine](#default-engine)
88
- * [Examples by Specific Engines](#examples-by-specific-engines)
89
- * [Check out the entire API / Functions](#api)
90
- * [Learn more about Markdown](#markdown)
91
- * [Learn more about Handlebars / Mustache and Helpers that we added](#handlebars)
92
- * [More about the template engines we support](#the-template-engines-we-support)
93
- * [Learn about the Mappings](#mappings-class)
94
- * [How to Contribute](#how-to-contribute)
95
- * [How to Submit an Issue](#how-to-submit-an-issue)
96
- * [How to use Ecto in Typescript](#using-typescript)
97
-
98
- -----
99
-
100
- ## Only the Top Template Engines and Their Extensions
108
+ # Only the Top Template Engines and Their Extensions
101
109
 
102
110
  We focus on the most popular and well-maintained consolidation engines. Unfortunately other engines suffered from packages that were unsupported, making it difficult to validate them as working fully. Some engines also had limited types and lacked ease of use.
103
111
 
@@ -115,9 +123,7 @@ Our goal is to support the top engines, handling the vast majority of use cases.
115
123
 
116
124
  _The `Extensions` are listed above for when we [Render from File](#render-from-file)._
117
125
 
118
- -----
119
-
120
- ## API
126
+ # API - Methods and Parameters
121
127
  The API is focused on using the main Ecto class:
122
128
 
123
129
  ```javascript
@@ -268,7 +274,7 @@ This string parameter can be used to set the default template engine for an inst
268
274
 
269
275
  `Ecto.defaultEngine` is set by default to EJS.If you would like to change the template engine there are several options available to users when setting the value of `defaultEngine`:
270
276
 
271
- ##### Set the engine in the arguments of the Ecto constructor
277
+ ## Set the engine in the arguments of the Ecto constructor
272
278
 
273
279
  One option for setting the default engine, is to do so in the Ecto constructor:
274
280
 
@@ -280,7 +286,7 @@ One option for setting the default engine, is to do so in the Ecto constructor:
280
286
  - `const ecto = new Ecto({defaultEngine: "handlebars"});`
281
287
  - `const ecto = new Ecto({defaultEngine: "liquid"});`
282
288
 
283
- ##### Set the default engine as a parameter
289
+ ## Set the default engine as a parameter
284
290
 
285
291
  We can also set the default engine as a parameter, like so:
286
292
 
@@ -296,7 +302,7 @@ const ecto = new Ecto({defaultEngine: "liquid"});
296
302
  ecto.defaultEngine = "mustache";
297
303
  ```
298
304
 
299
- ##### Set the engine as a parameter on the render function
305
+ ## Set the engine as a parameter on the render function
300
306
 
301
307
  You can explicitly override the Ecto.defaultEngine parameter in the render function:
302
308
 
@@ -307,7 +313,7 @@ const data = {firstName: "John", lastName: "Doe"};
307
313
  await ecto.render(source, data, "handlebars"); //returns <h1>Hello John Doe!</h1>
308
314
  ```
309
315
 
310
- ##### Override the auto selection on renderFromFile
316
+ ## Override the auto selection on renderFromFile
311
317
 
312
318
  The `renderFromFile` function automatically decides on the template engine, based on the file extension in the file path you specify. However, you can also explicitly set the engine you would like to use. Here we set the engine to be Pug.
313
319
 
@@ -332,7 +338,7 @@ const ecto = Ecto();
332
338
  ecto.Handlebars.engine.SafeString("<div>HTML Content!</div>");
333
339
  ```
334
340
 
335
- ### Find Template Without Extension - Helper Methods
341
+ ## Find Template Without Extension - Helper Methods
336
342
 
337
343
  We have added in a couple of helper methods to make it easier to find a template without the extension. This is useful when you want to find a template without the extension. For example, if you have a template called `template.ejs` and you want to find it without the extension, you can use the `findTemplateWithoutExtension` for async or `findTemplateWithoutExtensionSync` function. This function takes two parameters:
338
344
 
@@ -348,25 +354,7 @@ const templateFilePath = ecto.findTemplateWithoutExtensionSync("./path/to/", "in
348
354
 
349
355
  This will return the full path of the template based on what extension is there such as `./path/to/index.ejs`.
350
356
 
351
- ### Using Typescript
352
- Typescript is the language Ecto is already written in and while many of these examples are in `javascript` they should be compatible with `typescript`. To use Ecto just do an import:
353
- ```javascript
354
- import { Ecto } from "ecto";
355
- ```
356
-
357
- and then simply call it like you do with standard javascript:
358
- ```javascript
359
- const ecto = Ecto();
360
- const source = "# markdown rulezz!";
361
- const output = await ecto.render(source, undefined, "markdown");
362
- console.log(output) //should be <h1 id="markdown-rulezz">markdown rulezz!</h1>
363
- ```
364
- _NOTE: this example would be in an `async` function._
365
-
366
-
367
- ### Examples By Specific Engines
368
-
369
- #### Markdown
357
+ # Markdown Example
370
358
 
371
359
  Markdown does not contain complexities such as data objects, or partials and layouts. To render markdown its as simple as:
372
360
 
@@ -384,7 +372,7 @@ await ecto.renderByFile("/path/to/file.md");
384
372
 
385
373
  We are using [Writr](https://writr.org/) which is remark with all the plugins and features you need.
386
374
 
387
- #### Handlebars
375
+ # Handlebars Example
388
376
 
389
377
  In Ecto we use the [Fumanchu](https://www.npmjs.com/package/@jaredwray/fumanchu) engine to render `mustache` and `handlebars` related templates. This is because handlebars is based on mustache with just more additional features. Fumanchu includes handlebar engine with helpers.
390
378
 
@@ -397,11 +385,8 @@ ecto.render(source, undefined, "handlebars").then((output) => {
397
385
  });
398
386
  ```
399
387
 
400
- ------
401
-
402
388
 
403
-
404
- ## Mappings Class
389
+ # Engine Mappings
405
390
 
406
391
  Ecto contains a mapping class containing all of the engines registered in the system. The class has been designed to allow users to edit existing engine mappings. Let us explore this class in detail.
407
392
 
@@ -413,7 +398,7 @@ The `Map` object has the following structure: `Map<string, Array<string>> `
413
398
 
414
399
  From here you can start setting and editing engine mappings.
415
400
 
416
- ### Setting an engine mapping - set(name:string, extensions:Array&lt;string>): void
401
+ ## Setting an engine mapping - set(name:string, extensions:Array&lt;string>): void
417
402
 
418
403
  To set an engine mapping with extensions simply write:
419
404
 
@@ -429,7 +414,7 @@ This sets the handlebars engine to accept files with the following file extensio
429
414
  * .hbs
430
415
  * .hjs
431
416
 
432
- ### Delete an engine mapping - delete(name:string): void
417
+ ## Delete an engine mapping - delete(name:string): void
433
418
 
434
419
  To delete a mapping entirely you can use the `delete` method.
435
420
 
@@ -439,7 +424,7 @@ mappings.delete("handlebars");
439
424
 
440
425
  This will remove this engine mapping entirely.
441
426
 
442
- ### Delete an extension - deleteExtension(name:string, extension:string): void
427
+ ## Delete an extension - deleteExtension(name:string, extension:string): void
443
428
 
444
429
  To delete an extension for a particular engine mapping, you can use the `deleteExtension` method. This method takes two arguments:
445
430
 
@@ -448,17 +433,15 @@ To delete an extension for a particular engine mapping, you can use the `deleteE
448
433
 
449
434
  The following code deletes two of the extensions for our `handlebars` engine mapping.
450
435
 
451
-
452
436
  ```
453
437
  mappings.deleteExtension("handlebars", "hbs","hjs");
454
438
  ```
455
439
 
456
-
457
440
  After executing this code the only accepted file extension for the Handlebars engine will be `handlebars`.
458
441
 
459
442
  Other useful methods include `get` and `getName`.
460
443
 
461
- ### get method - get(name:string): Array&lt;string> | undefined
444
+ ## get method - get(name:string): Array&lt;string> | undefined
462
445
 
463
446
  The `get` method takes one argument, `name:string `, and will return the extensions for the name you specify. If extensions are found, they are returned as an `Array<string>`. If no engine mapping can be found for the name you specify, ` undefined` is returned. To use the `get` method simply write:
464
447
 
@@ -470,51 +453,23 @@ mappings.get("handlebars")
470
453
 
471
454
  This will retrieve the array of extensions assigned to the Handlebars engine.
472
455
 
473
- ### getName method - getName(extension:string): string | undefined
456
+ ## getName method - getName(extension:string): string | undefined
474
457
 
475
458
  The `getName` method takes a single argument, `extension:string`. If a valid extension is given, this method will return the name of the engine mapping that the extension belongs to. For example:
476
459
 
477
-
478
460
  ```
479
461
  mappings.getName("hjs")
480
462
  ```
481
463
 
482
-
483
464
  This will return the string “handlebars”, which is the corresponding engine for this extension. If no match can be found, this method will return `undefined`.
484
465
 
485
466
  Gaining an understanding of this class will provide you with more options and possibilities when using Ecto.
486
467
 
487
- ## How to Contribute
488
-
489
- This is an open-source project under MIT License. If you would like to get involved and contribute to this project, simply follow these steps:
490
-
491
- 1. Create a fork of this project, this will act as your copy of a GitHub repository allowing you to make changes to code without affecting the original project.
492
- 2. Clone this fork to create a local repository.
493
- 3. Make the desired changes to your copy of the project, commit, and push those changes when ready.
494
- 4. Finally, create a pull request to suggest your changes to the original project.
495
-
496
- ### Pull Requests
497
-
498
- Pull requests are used in open-source projects or in some corporate workflows to manage changes from contributors and to initiate code review before such changes are merged.
499
-
500
- By creating a pull request, you tell others about the changes you've pushed to your fork of a GitHub repository, so that the maintainers of the original repository can review your changes, discuss them, and integrate them into the base branch.
501
-
502
- ## How to Submit an Issue
503
-
504
- Issues can be used to keep track of bugs, enhancements, or other requests. Issues can be created based on code from pull requests, comments, or created from the main repository page.
505
-
506
- 1. Navigate to the [main page for this repository.](https://github.com/jaredwray/ecto)
507
- 2. Under the repository name, jaredwray/ecto, click the "Issues" tab.
508
- 3. Click New Issue.
509
- 4. Enter the title and description for your issue, and click "Submit new issue".
510
-
511
- ## The Template Engines we support
512
-
513
- ### What is a Template Engine?
468
+ # The Template Engines We Support
514
469
 
515
470
  A template engine is a tool that allows developers to write HTML markup that contains the template engine’s defined tags and syntax. These tags are used to insert variables into the final output of the template, or run some programming logic at run-time before sending the final HTML to the browser for display.
516
471
 
517
- ### EJS
472
+ ## EJS
518
473
 
519
474
  EJS stands for Embedded JavaScript. It is a templating engine that allows users to generate HTML using plain JavaScript.
520
475
 
@@ -522,7 +477,7 @@ You define HTML pages in the EJS syntax and specify where various data will be s
522
477
 
523
478
  It is a tool for generating web pages that can include dynamic data and can share templated pieces with other web pages. It is not a front-end framework. While EJS can be used by client-side Javascript to generate HTML on the client-side, it is typically used by your back-end to generate web pages in response to some URL request. EJS is not a client-side framework like Angular or React.
524
479
 
525
- ### Markdown
480
+ ## Markdown
526
481
 
527
482
  Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. Markdown is now one of the world’s most popular markup languages.
528
483
 
@@ -536,7 +491,7 @@ There are several reasons why people might choose Markdown instead of standard t
536
491
  * Markdown is future proof. Even if the application you’re using stops working at some point in the future, you’ll still be able to read your Markdown-formatted text using a text editing application.
537
492
  * Markdown is widely supported. Websites like Reddit and GitHub support Markdown, along with many other desktop and web-based applications.
538
493
 
539
- ### PUG
494
+ ## PUG
540
495
 
541
496
  Pug.js is an HTML templating engine that takes simple Pug code, which the Pug compiler will compile into HTML code that browsers can understand. Some features and advantages of the Pug template engine are as follows:
542
497
 
@@ -544,7 +499,7 @@ Pug.js is an HTML templating engine that takes simple Pug code, which the Pug co
544
499
  * Pug supports JavaScript natively.
545
500
  * Pug is excellent for handling dynamic, changing data. Imagine we have an email template, with certain fields to be customized depending on who you are sending the email to. Before sending the email we can compile the Pug code to HTML, using the user data to fill the gaps where the dynamic information should go.
546
501
 
547
- ### Nunjucks
502
+ ## Nunjucks
548
503
 
549
504
  Nunjucks is a rich and powerful template engine for JavaScript. Nunjucks is developed by Mozilla and maintained by the Node JS Foundation. Nunjucks can be used in both Node and the browser.
550
505
 
@@ -557,7 +512,7 @@ Some of the advantages of using Nunjucks for your project are:
557
512
  * Easily extensible with custom filters and extensions.
558
513
  * Available in Node and all modern web browsers, along with precompilation options.
559
514
 
560
- ### Mustache
515
+ ## Mustache
561
516
 
562
517
  Mustache is a logic-less template syntax. It can be used for HTML, config files, source code, and more. It is often referred to as “logic-less” as there are no if statements, else clauses, or for loops. There are only tags. Tags are replaced with actual values at runtime.
563
518
 
@@ -574,7 +529,7 @@ We see two braces around `{{ name }}`. This is Mustache syntax to show that it i
574
529
 
575
530
  Mustache is not actually a templating engine. Mustache is a specification for a templating language. In general, we would write templates according to the Mustache specification, and they can then be compiled by a templating engine to be rendered, eventually creating an output.
576
531
 
577
- ### Handlebars
532
+ ## Handlebars
578
533
 
579
534
  Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions. Handlebars expressions are wrapped in double curly braces, like this: `{{expression}}`. We use `@jaredwray/fumanchu` as it contains handbars and helpers.
580
535
 
@@ -586,7 +541,7 @@ const source = "{{year}}";
586
541
  await ecto.render(source, undefined, "handlebars"); //returns current year as a number
587
542
  ```
588
543
 
589
- #### Liquid
544
+ ## Liquid
590
545
 
591
546
  Some refer to Liquid as a template language, while others may call it a template engine. It doesn't really matter which label you apply, in many ways both are right. It has a syntax (like traditional programming languages), has concepts such as output, logic, and loops, and it interacts with variables and data, just as you would with a language such as PHP.
592
547
 
@@ -596,6 +551,18 @@ Liquid files have the extension of `.liquid`. A liquid file is a mix of standard
596
551
 
597
552
  The double curly brace delimiters `{{ }}` denote output, and the curly brace percentage delimiters `{% %}` denote logic. You'll become very familiar with these as every Liquid construct begins with one, or the other. Another way of thinking of delimiters is as placeholders. A placeholder can be viewed as a piece of code that will ultimately be replaced by data when the compiled file is sent to the browser.
598
553
 
554
+ # FrontMatter Helper Functions
555
+
556
+ Ecto has added in some helper functions for frontmatter in markdown files. Frontmatter is metadata that is at the top of a markdown file. It is used to store information about the file such as the author, date, tags, and license.
557
+
558
+ * `.hasFrontMatter(source: string): boolean` - This function checks if the markdown file has frontmatter. It takes in a string and returns a boolean value.
559
+ * `.getFrontMatter(source: string): object` - This function gets the frontmatter from the markdown file. It takes in a string and returns an object.
560
+ * `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
561
+
562
+ # How to Contribute
563
+
564
+ To contribute please ready our [CONTRIBUTING.md](CONTRIBUTING.md) guide. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
565
+
599
566
  # License
600
567
 
601
568
  [MIT License - Copyright (c) Jared Wray](LICENSE)
package/dist/ecto.d.ts CHANGED
@@ -77,27 +77,157 @@ declare class Ecto {
77
77
  private readonly __handlebars;
78
78
  private readonly __liquid;
79
79
  constructor(options?: Record<string, unknown>);
80
+ /**
81
+ * Get the default engine
82
+ */
80
83
  get defaultEngine(): string;
84
+ /**
85
+ * Set the default engine
86
+ * @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
87
+ */
81
88
  set defaultEngine(value: string);
89
+ /**
90
+ * Get the Engine Mappings. This is used to map file extensions to engines
91
+ * @returns {EngineMap}
92
+ */
82
93
  get mappings(): EngineMap;
94
+ /**
95
+ * Get the EJS Engine
96
+ * @returns {EJS}
97
+ */
83
98
  get ejs(): EJS;
99
+ /**
100
+ * Get the Markdown Engine
101
+ * @returns {Markdown}
102
+ */
84
103
  get markdown(): Markdown;
104
+ /**
105
+ * Get the Pug Engine
106
+ * @returns {Pug}
107
+ */
85
108
  get pug(): Pug;
109
+ /**
110
+ * Get the Nunjucks Engine
111
+ * @returns {Nunjucks}
112
+ */
86
113
  get nunjucks(): Nunjucks;
114
+ /**
115
+ * Get the Handlebars Engine
116
+ * @returns {Handlebars}
117
+ */
87
118
  get handlebars(): Handlebars;
119
+ /**
120
+ * Get the Liquid Engine
121
+ * @returns {Liquid}
122
+ */
88
123
  get liquid(): Liquid;
124
+ /**
125
+ * Async render the source with the data
126
+ * @param {string} source - The source to render
127
+ * @param {Record<string, unknown} data - data to render with the source
128
+ * @param {string} [engineName] - The engine to use for rendering
129
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
130
+ * @param {string} [filePathOutput] - The file path to write the output
131
+ * @returns {Promise<string>}
132
+ */
89
133
  render(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): Promise<string>;
134
+ /**
135
+ * Synchronously render the source with the data
136
+ * @param {string} source - The source to render
137
+ * @param {Record<string, unknown} data - data to render with the source
138
+ * @param {string} [engineName] - The engine to use for rendering
139
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
140
+ * @param {string} [filePathOutput] - The file path to write the output
141
+ * @returns {string}
142
+ */
90
143
  renderSync(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): string;
144
+ /**
145
+ * Render from a file path
146
+ * @param {string} filePath - The file path to the source
147
+ * @param {Record<string, unknown>} data - The data to render with the source
148
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
149
+ * @param {string} [filePathOutput] - The file path to write the output
150
+ * @param {string} [engineName] - The engine to use for rendering
151
+ * @returns
152
+ */
91
153
  renderFromFile(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): Promise<string>;
154
+ /**
155
+ * Sync render from a file path
156
+ * @param {string} filePath - The file path to the source
157
+ * @param {Record<string, unknown>} data - The data to render with the source
158
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
159
+ * @param {string} [filePathOutput] - The file path to write the output
160
+ * @param {string} [engineName] - The engine to use for rendering
161
+ * @returns {string}
162
+ */
92
163
  renderFromFileSync(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): string;
164
+ /**
165
+ * Ensure the file path exists or create it
166
+ * @param {string} path
167
+ * @returns {Promise<void>}
168
+ */
93
169
  ensureFilePath(path: string): Promise<void>;
170
+ /**
171
+ * Ensure the file path exists or create it synchronously
172
+ * @param {string} path
173
+ * @returns {void}
174
+ */
94
175
  ensureFilePathSync(path: string): void;
176
+ /**
177
+ * Get the Engine By File Path
178
+ * @param {string} filePath
179
+ * @returns {string} - will return the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
180
+ */
95
181
  getEngineByFilePath(filePath: string): string;
182
+ /**
183
+ * Find the template without the extension. This will look in a directory for a file that starts with the template name
184
+ * @param {string} path - the path to look for the template file
185
+ * @param {string} templateName
186
+ * @returns {Promise<string>} - the path to the template file
187
+ */
96
188
  findTemplateWithoutExtension(path: string, templateName: string): Promise<string>;
189
+ /**
190
+ * Syncronously find the template without the extension. This will look in a directory for a file that starts with the template name
191
+ * @param {string} path - the path to look for the template file
192
+ * @param {string} templateName
193
+ * @returns {string} - the path to the template file
194
+ */
97
195
  findTemplateWithoutExtensionSync(path: string, templateName: string): string;
196
+ /**
197
+ * Is it a valid engine that is registered in ecto
198
+ * @param engineName
199
+ * @returns {boolean}
200
+ */
98
201
  isValidEngine(engineName?: string): boolean;
202
+ /**
203
+ * Register the engine mappings
204
+ * @returns {void}
205
+ */
99
206
  registerEngineMappings(): void;
207
+ /**
208
+ * Get Render Engine by the engine name. Default is EJS
209
+ * @param {string} engineName
210
+ * @returns {EngineInterface}
211
+ */
100
212
  getRenderEngine(engineName: string): EngineInterface;
213
+ /**
214
+ * Checks if the source has front matter
215
+ * @param {string} source
216
+ * @returns {boolean}
217
+ */
218
+ hasFrontMatter(source: string): boolean;
219
+ /**
220
+ * Get the Front Matter from the source
221
+ * @param {string} source
222
+ * @returns {Record<string, unknown>}
223
+ */
224
+ getFrontMatter(source: string): Record<string, unknown>;
225
+ /**
226
+ * Remove the Front Matter from the source
227
+ * @param {string} source
228
+ * @returns {string}
229
+ */
230
+ removeFrontMatter(source: string): string;
101
231
  private writeFile;
102
232
  private writeFileSync;
103
233
  }
package/dist/ecto.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/ecto.ts
2
2
  import fs2 from "node:fs";
3
+ import { Writr as Writr2 } from "writr";
3
4
 
4
5
  // src/engine-map.ts
5
6
  var EngineMap = class {
@@ -312,38 +313,80 @@ var Ecto = class {
312
313
  this.__defaultEngine = options.defaultEngine;
313
314
  }
314
315
  }
316
+ /**
317
+ * Get the default engine
318
+ */
315
319
  get defaultEngine() {
316
320
  return this.__defaultEngine;
317
321
  }
322
+ /**
323
+ * Set the default engine
324
+ * @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
325
+ */
318
326
  set defaultEngine(value) {
319
327
  value = value.toLowerCase().trim();
320
328
  if (this.isValidEngine(value)) {
321
329
  this.__defaultEngine = value;
322
330
  }
323
331
  }
332
+ /**
333
+ * Get the Engine Mappings. This is used to map file extensions to engines
334
+ * @returns {EngineMap}
335
+ */
324
336
  get mappings() {
325
337
  return this.__mapping;
326
338
  }
327
- // Engines
339
+ /**
340
+ * Get the EJS Engine
341
+ * @returns {EJS}
342
+ */
328
343
  get ejs() {
329
344
  return this.__ejs;
330
345
  }
346
+ /**
347
+ * Get the Markdown Engine
348
+ * @returns {Markdown}
349
+ */
331
350
  get markdown() {
332
351
  return this.__markdown;
333
352
  }
353
+ /**
354
+ * Get the Pug Engine
355
+ * @returns {Pug}
356
+ */
334
357
  get pug() {
335
358
  return this.__pug;
336
359
  }
360
+ /**
361
+ * Get the Nunjucks Engine
362
+ * @returns {Nunjucks}
363
+ */
337
364
  get nunjucks() {
338
365
  return this.__nunjucks;
339
366
  }
367
+ /**
368
+ * Get the Handlebars Engine
369
+ * @returns {Handlebars}
370
+ */
340
371
  get handlebars() {
341
372
  return this.__handlebars;
342
373
  }
374
+ /**
375
+ * Get the Liquid Engine
376
+ * @returns {Liquid}
377
+ */
343
378
  get liquid() {
344
379
  return this.__liquid;
345
380
  }
346
- // String Render
381
+ /**
382
+ * Async render the source with the data
383
+ * @param {string} source - The source to render
384
+ * @param {Record<string, unknown} data - data to render with the source
385
+ * @param {string} [engineName] - The engine to use for rendering
386
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
387
+ * @param {string} [filePathOutput] - The file path to write the output
388
+ * @returns {Promise<string>}
389
+ */
347
390
  // eslint-disable-next-line max-params
348
391
  async render(source, data, engineName, rootTemplatePath, filePathOutput) {
349
392
  let result = "";
@@ -357,7 +400,15 @@ var Ecto = class {
357
400
  await this.writeFile(filePathOutput, result);
358
401
  return result;
359
402
  }
360
- // Render Sync
403
+ /**
404
+ * Synchronously render the source with the data
405
+ * @param {string} source - The source to render
406
+ * @param {Record<string, unknown} data - data to render with the source
407
+ * @param {string} [engineName] - The engine to use for rendering
408
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
409
+ * @param {string} [filePathOutput] - The file path to write the output
410
+ * @returns {string}
411
+ */
361
412
  // eslint-disable-next-line max-params
362
413
  renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
363
414
  let result = "";
@@ -371,7 +422,15 @@ var Ecto = class {
371
422
  this.writeFileSync(filePathOutput, result);
372
423
  return result;
373
424
  }
374
- // Render from File
425
+ /**
426
+ * Render from a file path
427
+ * @param {string} filePath - The file path to the source
428
+ * @param {Record<string, unknown>} data - The data to render with the source
429
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
430
+ * @param {string} [filePathOutput] - The file path to write the output
431
+ * @param {string} [engineName] - The engine to use for rendering
432
+ * @returns
433
+ */
375
434
  // eslint-disable-next-line max-params
376
435
  async renderFromFile(filePath, data, rootTemplatePath, filePathOutput, engineName) {
377
436
  let result = "";
@@ -380,7 +439,15 @@ var Ecto = class {
380
439
  result = await this.render(source, data, engineName, rootTemplatePath, filePathOutput);
381
440
  return result;
382
441
  }
383
- // Render from File Sync
442
+ /**
443
+ * Sync render from a file path
444
+ * @param {string} filePath - The file path to the source
445
+ * @param {Record<string, unknown>} data - The data to render with the source
446
+ * @param {string} [rootTemplatePath] - The root path to the template if using includes / partials
447
+ * @param {string} [filePathOutput] - The file path to write the output
448
+ * @param {string} [engineName] - The engine to use for rendering
449
+ * @returns {string}
450
+ */
384
451
  // eslint-disable-next-line max-params
385
452
  renderFromFileSync(filePath, data, rootTemplatePath, filePathOutput, engineName) {
386
453
  let result = "";
@@ -389,6 +456,11 @@ var Ecto = class {
389
456
  result = this.renderSync(source, data, engineName, rootTemplatePath, filePathOutput);
390
457
  return result;
391
458
  }
459
+ /**
460
+ * Ensure the file path exists or create it
461
+ * @param {string} path
462
+ * @returns {Promise<void>}
463
+ */
392
464
  async ensureFilePath(path) {
393
465
  const pathList = path.split("/");
394
466
  pathList.pop();
@@ -397,6 +469,11 @@ var Ecto = class {
397
469
  fs2.mkdirSync(directory, { recursive: true });
398
470
  }
399
471
  }
472
+ /**
473
+ * Ensure the file path exists or create it synchronously
474
+ * @param {string} path
475
+ * @returns {void}
476
+ */
400
477
  ensureFilePathSync(path) {
401
478
  const pathList = path.split("/");
402
479
  pathList.pop();
@@ -405,6 +482,11 @@ var Ecto = class {
405
482
  fs2.mkdirSync(directory, { recursive: true });
406
483
  }
407
484
  }
485
+ /**
486
+ * Get the Engine By File Path
487
+ * @param {string} filePath
488
+ * @returns {string} - will return the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
489
+ */
408
490
  getEngineByFilePath(filePath) {
409
491
  let result = this.__defaultEngine;
410
492
  if (filePath !== void 0) {
@@ -416,6 +498,12 @@ var Ecto = class {
416
498
  }
417
499
  return result;
418
500
  }
501
+ /**
502
+ * Find the template without the extension. This will look in a directory for a file that starts with the template name
503
+ * @param {string} path - the path to look for the template file
504
+ * @param {string} templateName
505
+ * @returns {Promise<string>} - the path to the template file
506
+ */
419
507
  async findTemplateWithoutExtension(path, templateName) {
420
508
  let result = "";
421
509
  const files = await fs2.promises.readdir(path);
@@ -427,6 +515,12 @@ var Ecto = class {
427
515
  }
428
516
  return result;
429
517
  }
518
+ /**
519
+ * Syncronously find the template without the extension. This will look in a directory for a file that starts with the template name
520
+ * @param {string} path - the path to look for the template file
521
+ * @param {string} templateName
522
+ * @returns {string} - the path to the template file
523
+ */
430
524
  findTemplateWithoutExtensionSync(path, templateName) {
431
525
  let result = "";
432
526
  const files = fs2.readdirSync(path);
@@ -438,7 +532,11 @@ var Ecto = class {
438
532
  }
439
533
  return result;
440
534
  }
441
- // Engines
535
+ /**
536
+ * Is it a valid engine that is registered in ecto
537
+ * @param engineName
538
+ * @returns {boolean}
539
+ */
442
540
  isValidEngine(engineName) {
443
541
  let result = false;
444
542
  if (engineName !== void 0 && this.__mapping.get(engineName) !== void 0) {
@@ -446,6 +544,10 @@ var Ecto = class {
446
544
  }
447
545
  return result;
448
546
  }
547
+ /**
548
+ * Register the engine mappings
549
+ * @returns {void}
550
+ */
449
551
  registerEngineMappings() {
450
552
  for (const eng of this.__engines) {
451
553
  for (const name of eng.names) {
@@ -453,6 +555,11 @@ var Ecto = class {
453
555
  }
454
556
  }
455
557
  }
558
+ /**
559
+ * Get Render Engine by the engine name. Default is EJS
560
+ * @param {string} engineName
561
+ * @returns {EngineInterface}
562
+ */
456
563
  getRenderEngine(engineName) {
457
564
  let result = this.__ejs;
458
565
  const cleanEngineName = engineName.trim().toLowerCase();
@@ -488,6 +595,36 @@ var Ecto = class {
488
595
  }
489
596
  return result;
490
597
  }
598
+ /**
599
+ * Checks if the source has front matter
600
+ * @param {string} source
601
+ * @returns {boolean}
602
+ */
603
+ hasFrontMatter(source) {
604
+ const writr = new Writr2(source);
605
+ if (writr.frontMatterRaw !== "") {
606
+ return true;
607
+ }
608
+ return false;
609
+ }
610
+ /**
611
+ * Get the Front Matter from the source
612
+ * @param {string} source
613
+ * @returns {Record<string, unknown>}
614
+ */
615
+ getFrontMatter(source) {
616
+ const writr = new Writr2(source);
617
+ return writr.frontMatter;
618
+ }
619
+ /**
620
+ * Remove the Front Matter from the source
621
+ * @param {string} source
622
+ * @returns {string}
623
+ */
624
+ removeFrontMatter(source) {
625
+ const writr = new Writr2(source);
626
+ return writr.body;
627
+ }
491
628
  async writeFile(filePath, source) {
492
629
  if (filePath && source) {
493
630
  await this.ensureFilePath(filePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
5
5
  "type": "module",
6
6
  "main": "./dist/ecto.js",
@@ -50,7 +50,7 @@
50
50
  "dependencies": {
51
51
  "@jaredwray/fumanchu": "^2.1.0",
52
52
  "ejs": "^3.1.10",
53
- "liquidjs": "^10.17.0",
53
+ "liquidjs": "^10.18.0",
54
54
  "nunjucks": "^3.2.4",
55
55
  "pug": "^3.0.3",
56
56
  "underscore": "^1.13.7",
@@ -59,16 +59,16 @@
59
59
  "devDependencies": {
60
60
  "@types/ejs": "^3.1.5",
61
61
  "@types/express": "^5.0.0",
62
- "@types/node": "^22.7.5",
62
+ "@types/node": "^22.8.1",
63
63
  "@types/nunjucks": "^3.2.6",
64
64
  "@types/pug": "^2.0.10",
65
- "@types/underscore": "^1.11.15",
66
- "@vitest/coverage-v8": "^2.1.2",
67
- "docula": "^0.9.2",
65
+ "@types/underscore": "^1.13.0",
66
+ "@vitest/coverage-v8": "^2.1.3",
67
+ "docula": "^0.9.4",
68
68
  "rimraf": "^6.0.1",
69
- "tsup": "^8.3.0",
69
+ "tsup": "^8.3.5",
70
70
  "typescript": "^5.6.3",
71
- "vitest": "^2.1.2",
71
+ "vitest": "^2.1.3",
72
72
  "webpack": "^5.95.0",
73
73
  "xo": "^0.59.3"
74
74
  },