ecto 4.6.0 → 4.7.1
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 +503 -61
- package/dist/ecto.d.ts +1 -1
- package/dist/ecto.js +12 -6
- package/package.json +29 -31
package/README.md
CHANGED
|
@@ -42,6 +42,13 @@ Ecto is a modern template consolidation engine that enables the best template en
|
|
|
42
42
|
* [FrontMatter Helper Functions](#frontmatter-helper-functions)
|
|
43
43
|
* [Caching on Rendering](#caching-on-rendering)
|
|
44
44
|
* [Emitting Events](#emitting-events)
|
|
45
|
+
* [Creating Custom Engines](#creating-custom-engines)
|
|
46
|
+
* [Direct Engine Usage](#direct-engine-usage)
|
|
47
|
+
* [Creating a Custom Engine](#creating-a-custom-engine)
|
|
48
|
+
* [Integrating Custom Engine with Ecto](#integrating-custom-engine-with-ecto)
|
|
49
|
+
* [Engine Interface Reference](#engine-interface-reference)
|
|
50
|
+
* [BaseEngine Helper Methods](#baseengine-helper-methods)
|
|
51
|
+
* [Tips for Custom Engines](#tips-for-custom-engines)
|
|
45
52
|
* [How to Contribute](#how-to-contribute)
|
|
46
53
|
* [License](#license)
|
|
47
54
|
|
|
@@ -471,6 +478,68 @@ This will return the string “handlebars”, which is the corresponding engine
|
|
|
471
478
|
|
|
472
479
|
Gaining an understanding of this class will provide you with more options and possibilities when using Ecto.
|
|
473
480
|
|
|
481
|
+
## Detect Template Engine
|
|
482
|
+
|
|
483
|
+
Ecto provides a `detectEngine` method that can automatically detect the template engine from a template string by analyzing its syntax patterns. This is useful when you receive template content but don't know which engine it uses.
|
|
484
|
+
|
|
485
|
+
## detectEngine(source: string): string
|
|
486
|
+
|
|
487
|
+
The `detectEngine` method analyzes the template syntax and returns the detected engine name. If no specific template syntax is found, it returns the configured default engine.
|
|
488
|
+
|
|
489
|
+
| Name | Type | Description |
|
|
490
|
+
| ------ | ------ | ------------------------------------------------- |
|
|
491
|
+
| source | string | The template source string to analyze |
|
|
492
|
+
|
|
493
|
+
**Returns:** The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine if no specific syntax is detected.
|
|
494
|
+
|
|
495
|
+
### Basic Usage
|
|
496
|
+
|
|
497
|
+
```javascript
|
|
498
|
+
const ecto = new Ecto();
|
|
499
|
+
|
|
500
|
+
// Detect EJS templates
|
|
501
|
+
const ejsEngine = ecto.detectEngine('<%= name %>');
|
|
502
|
+
console.log(ejsEngine); // 'ejs'
|
|
503
|
+
|
|
504
|
+
// Detect Handlebars templates
|
|
505
|
+
const hbsEngine = ecto.detectEngine('{{name}}');
|
|
506
|
+
console.log(hbsEngine); // 'handlebars'
|
|
507
|
+
|
|
508
|
+
// Detect Markdown
|
|
509
|
+
const mdEngine = ecto.detectEngine('# Hello World\n\nThis is markdown');
|
|
510
|
+
console.log(mdEngine); // 'markdown'
|
|
511
|
+
|
|
512
|
+
// Detect Pug templates
|
|
513
|
+
const pugEngine = ecto.detectEngine('div.container\n h1 Hello');
|
|
514
|
+
console.log(pugEngine); // 'pug'
|
|
515
|
+
|
|
516
|
+
// Detect Nunjucks templates
|
|
517
|
+
const njkEngine = ecto.detectEngine('{% block content %}Hello{% endblock %}');
|
|
518
|
+
console.log(njkEngine); // 'nunjucks'
|
|
519
|
+
|
|
520
|
+
// Detect Liquid templates
|
|
521
|
+
const liquidEngine = ecto.detectEngine('{% assign name = "John" %}{{ name | upcase }}');
|
|
522
|
+
console.log(liquidEngine); // 'liquid'
|
|
523
|
+
|
|
524
|
+
// Returns default engine for plain text
|
|
525
|
+
const defaultEngine = ecto.detectEngine('Plain text without template syntax');
|
|
526
|
+
console.log(defaultEngine); // 'ejs' (or whatever is set as defaultEngine)
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
### Detection Patterns
|
|
530
|
+
|
|
531
|
+
The `detectEngine` method recognizes the following syntax patterns:
|
|
532
|
+
|
|
533
|
+
- **EJS**: `<% %>`, `<%= %>`, `<%- %>`
|
|
534
|
+
- **Handlebars/Mustache**: `{{ }}`, `{{# }}`, `{{> }}`
|
|
535
|
+
- **Pug**: Indentation-based syntax without angle brackets
|
|
536
|
+
- **Nunjucks**: `{% block %}`, `{% extends %}`, `{% include %}`
|
|
537
|
+
- **Liquid**: `{% assign %}`, `{% capture %}`, pipe filters `{{ var | filter }}`
|
|
538
|
+
- **Markdown**: Headers `#`, lists, code blocks, links, tables
|
|
539
|
+
|
|
540
|
+
Note: For ambiguous syntax (like simple `{{ }}` which could be Handlebars, Mustache, or Liquid), the method makes intelligent decisions based on additional context clues in the template.
|
|
541
|
+
|
|
542
|
+
|
|
474
543
|
# The Template Engines We Support
|
|
475
544
|
|
|
476
545
|
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.
|
|
@@ -566,67 +635,6 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
|
|
|
566
635
|
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
|
|
567
636
|
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
|
|
568
637
|
|
|
569
|
-
# Detect Template Engine
|
|
570
|
-
|
|
571
|
-
Ecto provides a `detectEngine` method that can automatically detect the template engine from a template string by analyzing its syntax patterns. This is useful when you receive template content but don't know which engine it uses.
|
|
572
|
-
|
|
573
|
-
## detectEngine(source: string): string
|
|
574
|
-
|
|
575
|
-
The `detectEngine` method analyzes the template syntax and returns the detected engine name. If no specific template syntax is found, it returns the configured default engine.
|
|
576
|
-
|
|
577
|
-
| Name | Type | Description |
|
|
578
|
-
| ------ | ------ | ------------------------------------------------- |
|
|
579
|
-
| source | string | The template source string to analyze |
|
|
580
|
-
|
|
581
|
-
**Returns:** The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine if no specific syntax is detected.
|
|
582
|
-
|
|
583
|
-
### Basic Usage
|
|
584
|
-
|
|
585
|
-
```javascript
|
|
586
|
-
const ecto = new Ecto();
|
|
587
|
-
|
|
588
|
-
// Detect EJS templates
|
|
589
|
-
const ejsEngine = ecto.detectEngine('<%= name %>');
|
|
590
|
-
console.log(ejsEngine); // 'ejs'
|
|
591
|
-
|
|
592
|
-
// Detect Handlebars templates
|
|
593
|
-
const hbsEngine = ecto.detectEngine('{{name}}');
|
|
594
|
-
console.log(hbsEngine); // 'handlebars'
|
|
595
|
-
|
|
596
|
-
// Detect Markdown
|
|
597
|
-
const mdEngine = ecto.detectEngine('# Hello World\n\nThis is markdown');
|
|
598
|
-
console.log(mdEngine); // 'markdown'
|
|
599
|
-
|
|
600
|
-
// Detect Pug templates
|
|
601
|
-
const pugEngine = ecto.detectEngine('div.container\n h1 Hello');
|
|
602
|
-
console.log(pugEngine); // 'pug'
|
|
603
|
-
|
|
604
|
-
// Detect Nunjucks templates
|
|
605
|
-
const njkEngine = ecto.detectEngine('{% block content %}Hello{% endblock %}');
|
|
606
|
-
console.log(njkEngine); // 'nunjucks'
|
|
607
|
-
|
|
608
|
-
// Detect Liquid templates
|
|
609
|
-
const liquidEngine = ecto.detectEngine('{% assign name = "John" %}{{ name | upcase }}');
|
|
610
|
-
console.log(liquidEngine); // 'liquid'
|
|
611
|
-
|
|
612
|
-
// Returns default engine for plain text
|
|
613
|
-
const defaultEngine = ecto.detectEngine('Plain text without template syntax');
|
|
614
|
-
console.log(defaultEngine); // 'ejs' (or whatever is set as defaultEngine)
|
|
615
|
-
```
|
|
616
|
-
|
|
617
|
-
### Detection Patterns
|
|
618
|
-
|
|
619
|
-
The `detectEngine` method recognizes the following syntax patterns:
|
|
620
|
-
|
|
621
|
-
- **EJS**: `<% %>`, `<%= %>`, `<%- %>`
|
|
622
|
-
- **Handlebars/Mustache**: `{{ }}`, `{{# }}`, `{{> }}`
|
|
623
|
-
- **Pug**: Indentation-based syntax without angle brackets
|
|
624
|
-
- **Nunjucks**: `{% block %}`, `{% extends %}`, `{% include %}`
|
|
625
|
-
- **Liquid**: `{% assign %}`, `{% capture %}`, pipe filters `{{ var | filter }}`
|
|
626
|
-
- **Markdown**: Headers `#`, lists, code blocks, links, tables
|
|
627
|
-
|
|
628
|
-
Note: For ambiguous syntax (like simple `{{ }}` which could be Handlebars, Mustache, or Liquid), the method makes intelligent decisions based on additional context clues in the template.
|
|
629
|
-
|
|
630
638
|
# Caching on Rendering
|
|
631
639
|
|
|
632
640
|
Ecto has a built-in caching mechanism that is `disabled by default` that allows you to cache the rendered output of templates. This is useful for improving performance and reducing the number of times a template needs to be rendered. There are currently two caching engines available: `MemoryCache` and `FileCache`.
|
|
@@ -704,6 +712,440 @@ const data = { firstName: "John", lastName: "Doe" };
|
|
|
704
712
|
await ecto.render(source, data); // <h1>Hello John Doe!</h1>
|
|
705
713
|
```
|
|
706
714
|
|
|
715
|
+
# Creating Custom Engines
|
|
716
|
+
|
|
717
|
+
Ecto allows you to create your own custom template engines by implementing the `EngineInterface`. This is useful when you want to integrate a template engine that isn't built into Ecto or when you need custom rendering logic.
|
|
718
|
+
|
|
719
|
+
## Direct Engine Usage
|
|
720
|
+
|
|
721
|
+
All built-in engines can be imported and used directly without going through the main `Ecto` class:
|
|
722
|
+
|
|
723
|
+
```typescript
|
|
724
|
+
import { EJS, Handlebars, Liquid, Markdown, Nunjucks, Pug } from 'ecto';
|
|
725
|
+
|
|
726
|
+
// Use any engine directly
|
|
727
|
+
const handlebars = new Handlebars();
|
|
728
|
+
const result = await handlebars.render('Hello {{name}}', { name: 'World' });
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
### Customizing Engines via Constructor
|
|
732
|
+
|
|
733
|
+
The recommended way to customize engines is through the `engineOptions` parameter in the Ecto constructor:
|
|
734
|
+
|
|
735
|
+
```typescript
|
|
736
|
+
import { Ecto } from 'ecto';
|
|
737
|
+
|
|
738
|
+
const ecto = new Ecto({
|
|
739
|
+
engineOptions: {
|
|
740
|
+
handlebars: {
|
|
741
|
+
noEscape: false,
|
|
742
|
+
strict: true
|
|
743
|
+
},
|
|
744
|
+
markdown: {
|
|
745
|
+
html: true,
|
|
746
|
+
breaks: true
|
|
747
|
+
},
|
|
748
|
+
ejs: {
|
|
749
|
+
delimiter: '?' // Use <? ?> instead of <% %>
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
// Engines are now configured with your options
|
|
755
|
+
await ecto.render('<? name ?>', { name: 'World' }, 'ejs');
|
|
756
|
+
```
|
|
757
|
+
|
|
758
|
+
### Accessing and Modifying Engines After Creation
|
|
759
|
+
|
|
760
|
+
After creating an Ecto instance, you can access each engine directly and modify it. This is useful for adding custom helpers or changing settings dynamically:
|
|
761
|
+
|
|
762
|
+
```typescript
|
|
763
|
+
import { Ecto } from 'ecto';
|
|
764
|
+
|
|
765
|
+
const ecto = new Ecto();
|
|
766
|
+
|
|
767
|
+
// Access the Handlebars engine and add custom helpers
|
|
768
|
+
ecto.handlebars.engine.registerHelper('uppercase', (text) => {
|
|
769
|
+
return text.toUpperCase();
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
ecto.handlebars.engine.registerHelper('formatDate', (date) => {
|
|
773
|
+
return new Date(date).toLocaleDateString();
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
// Now use the customized engine
|
|
777
|
+
const result = await ecto.render(
|
|
778
|
+
'Hello {{uppercase name}} - {{formatDate date}}',
|
|
779
|
+
{ name: 'world', date: '2024-01-15' },
|
|
780
|
+
'handlebars'
|
|
781
|
+
);
|
|
782
|
+
// Output: Hello WORLD - 1/15/2024
|
|
783
|
+
|
|
784
|
+
// Works with renderFromFile too
|
|
785
|
+
await ecto.renderFromFile('./template.hbs', { name: 'world', date: '2024-01-15' });
|
|
786
|
+
```
|
|
787
|
+
|
|
788
|
+
### Customizing Multiple Engines
|
|
789
|
+
|
|
790
|
+
You can customize multiple engines in the same Ecto instance:
|
|
791
|
+
|
|
792
|
+
```typescript
|
|
793
|
+
import { Ecto } from 'ecto';
|
|
794
|
+
|
|
795
|
+
const ecto = new Ecto();
|
|
796
|
+
|
|
797
|
+
// Customize Handlebars with helpers
|
|
798
|
+
ecto.handlebars.engine.registerHelper('bold', (text) => {
|
|
799
|
+
return `<strong>${text}</strong>`;
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
// Customize EJS options
|
|
803
|
+
ecto.ejs.opts = {
|
|
804
|
+
...ecto.ejs.opts,
|
|
805
|
+
delimiter: '?',
|
|
806
|
+
openDelimiter: '[',
|
|
807
|
+
closeDelimiter: ']'
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// Customize Markdown options
|
|
811
|
+
ecto.markdown.opts = {
|
|
812
|
+
...ecto.markdown.opts,
|
|
813
|
+
html: true,
|
|
814
|
+
breaks: true
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
// Use all customized engines
|
|
818
|
+
await ecto.render('{{bold "Hello"}}', {}, 'handlebars');
|
|
819
|
+
await ecto.render('[? name ?]', { name: 'World' }, 'ejs');
|
|
820
|
+
await ecto.render('# Hello\n\nVisit https://example.com', {}, 'markdown');
|
|
821
|
+
```
|
|
822
|
+
|
|
823
|
+
### Setting Partials Path for Handlebars
|
|
824
|
+
|
|
825
|
+
You can configure Handlebars to look for partials in multiple directories:
|
|
826
|
+
|
|
827
|
+
```typescript
|
|
828
|
+
import { Ecto } from 'ecto';
|
|
829
|
+
|
|
830
|
+
const ecto = new Ecto();
|
|
831
|
+
|
|
832
|
+
// Configure where to look for partials
|
|
833
|
+
ecto.handlebars.partialsPath = ['partials', 'includes', 'components', 'layouts'];
|
|
834
|
+
ecto.handlebars.rootTemplatePath = './templates';
|
|
835
|
+
|
|
836
|
+
// Now partials will be loaded from multiple directories
|
|
837
|
+
await ecto.render('{{> header}}Content{{> footer}}', {}, 'handlebars');
|
|
838
|
+
```
|
|
839
|
+
|
|
840
|
+
### Using Exported Engines Standalone
|
|
841
|
+
|
|
842
|
+
You can also use the exported engine classes independently of Ecto:
|
|
843
|
+
|
|
844
|
+
```typescript
|
|
845
|
+
import { Handlebars } from 'ecto';
|
|
846
|
+
|
|
847
|
+
// Create a standalone engine instance
|
|
848
|
+
const handlebars = new Handlebars();
|
|
849
|
+
|
|
850
|
+
// Add custom helpers
|
|
851
|
+
handlebars.engine.registerHelper('shout', (text) => {
|
|
852
|
+
return text.toUpperCase() + '!!!';
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
// Use it directly
|
|
856
|
+
const result = await handlebars.render('{{shout greeting}}', { greeting: 'hello' });
|
|
857
|
+
console.log(result); // HELLO!!!
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
This pattern is particularly powerful when:
|
|
861
|
+
- You need consistent helpers across all templates
|
|
862
|
+
- You want to configure engine-specific options globally
|
|
863
|
+
- You're building a framework or application with specific template requirements
|
|
864
|
+
- You need to extend engine functionality beyond default behavior
|
|
865
|
+
|
|
866
|
+
## Creating a Custom Engine
|
|
867
|
+
|
|
868
|
+
To create a custom engine, you need to:
|
|
869
|
+
|
|
870
|
+
1. Extend the `BaseEngine` class
|
|
871
|
+
2. Implement the `EngineInterface`
|
|
872
|
+
3. Define your engine's name and file extensions
|
|
873
|
+
4. Implement the `render` and `renderSync` methods
|
|
874
|
+
|
|
875
|
+
### Basic Custom Engine Example
|
|
876
|
+
|
|
877
|
+
Here's a simple example of a custom uppercase engine:
|
|
878
|
+
|
|
879
|
+
```typescript
|
|
880
|
+
import { BaseEngine, type EngineInterface } from 'ecto';
|
|
881
|
+
|
|
882
|
+
class UppercaseEngine extends BaseEngine implements EngineInterface {
|
|
883
|
+
constructor(options?: Record<string, unknown>) {
|
|
884
|
+
super();
|
|
885
|
+
|
|
886
|
+
// Define the engine name(s)
|
|
887
|
+
this.names = ['uppercase', 'upper'];
|
|
888
|
+
|
|
889
|
+
// Set any options
|
|
890
|
+
this.opts = options;
|
|
891
|
+
|
|
892
|
+
// Define file extensions this engine handles
|
|
893
|
+
this.setExtensions(['upper', 'uppercase']);
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
async render(source: string, data?: Record<string, unknown>): Promise<string> {
|
|
897
|
+
// Simple rendering logic: replace variables and uppercase everything
|
|
898
|
+
let result = source;
|
|
899
|
+
|
|
900
|
+
if (data) {
|
|
901
|
+
// Replace {{variable}} with data values
|
|
902
|
+
for (const [key, value] of Object.entries(data)) {
|
|
903
|
+
const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
|
|
904
|
+
result = result.replace(regex, String(value));
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// Convert to uppercase
|
|
909
|
+
return result.toUpperCase();
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
renderSync(source: string, data?: Record<string, unknown>): string {
|
|
913
|
+
// Same logic for synchronous rendering
|
|
914
|
+
let result = source;
|
|
915
|
+
|
|
916
|
+
if (data) {
|
|
917
|
+
for (const [key, value] of Object.entries(data)) {
|
|
918
|
+
const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
|
|
919
|
+
result = result.replace(regex, String(value));
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
return result.toUpperCase();
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// Use the custom engine
|
|
928
|
+
const engine = new UppercaseEngine();
|
|
929
|
+
const result = await engine.render('Hello {{name}}!', { name: 'World' });
|
|
930
|
+
console.log(result); // Output: HELLO WORLD!
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
### Integrating Custom Engine with Ecto
|
|
934
|
+
|
|
935
|
+
To use your custom engine with the main Ecto class, you need to register it:
|
|
936
|
+
|
|
937
|
+
```typescript
|
|
938
|
+
import { Ecto } from 'ecto';
|
|
939
|
+
|
|
940
|
+
const ecto = new Ecto();
|
|
941
|
+
|
|
942
|
+
// Create and register your custom engine
|
|
943
|
+
const uppercaseEngine = new UppercaseEngine();
|
|
944
|
+
|
|
945
|
+
// Add to the engines collection
|
|
946
|
+
ecto.engines.set('uppercase', uppercaseEngine);
|
|
947
|
+
|
|
948
|
+
// Register the file extensions
|
|
949
|
+
ecto.mappings.set('uppercase', ['upper', 'uppercase']);
|
|
950
|
+
|
|
951
|
+
// Now you can use it with render
|
|
952
|
+
const result = await ecto.render('Hello {{name}}!', { name: 'World' }, 'uppercase');
|
|
953
|
+
console.log(result); // HELLO WORLD!
|
|
954
|
+
|
|
955
|
+
// Or with renderFromFile (for .upper or .uppercase files)
|
|
956
|
+
await ecto.renderFromFile('./template.upper', { name: 'World' });
|
|
957
|
+
```
|
|
958
|
+
|
|
959
|
+
### Advanced Custom Engine with Template Library
|
|
960
|
+
|
|
961
|
+
Here's a more advanced example that wraps an external template library:
|
|
962
|
+
|
|
963
|
+
```typescript
|
|
964
|
+
import { BaseEngine, type EngineInterface } from 'ecto';
|
|
965
|
+
import Handlebars from 'handlebars'; // Example external library
|
|
966
|
+
|
|
967
|
+
class CustomHandlebarsEngine extends BaseEngine implements EngineInterface {
|
|
968
|
+
constructor(options?: Record<string, unknown>) {
|
|
969
|
+
super();
|
|
970
|
+
|
|
971
|
+
this.names = ['custom-handlebars'];
|
|
972
|
+
this.opts = options;
|
|
973
|
+
this.setExtensions(['chbs']);
|
|
974
|
+
|
|
975
|
+
// Initialize the underlying engine
|
|
976
|
+
this.engine = Handlebars.create();
|
|
977
|
+
|
|
978
|
+
// Register custom helpers
|
|
979
|
+
this.engine.registerHelper('shout', (text: string) => {
|
|
980
|
+
return text.toUpperCase() + '!!!';
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
this.engine.registerHelper('whisper', (text: string) => {
|
|
984
|
+
return text.toLowerCase() + '...';
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
async render(source: string, data?: Record<string, unknown>): Promise<string> {
|
|
989
|
+
const template = this.engine.compile(source, this.opts);
|
|
990
|
+
return template(data);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
renderSync(source: string, data?: Record<string, unknown>): string {
|
|
994
|
+
const template = this.engine.compile(source, this.opts);
|
|
995
|
+
return template(data);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// Usage
|
|
1000
|
+
const engine = new CustomHandlebarsEngine();
|
|
1001
|
+
const result = await engine.render(
|
|
1002
|
+
'Normal: {{name}}, Shout: {{shout name}}, Whisper: {{whisper name}}',
|
|
1003
|
+
{ name: 'Hello' }
|
|
1004
|
+
);
|
|
1005
|
+
console.log(result);
|
|
1006
|
+
// Output: Normal: Hello, Shout: HELLO!!!, Whisper: hello...
|
|
1007
|
+
```
|
|
1008
|
+
|
|
1009
|
+
### Custom Engine with Partials Support
|
|
1010
|
+
|
|
1011
|
+
If your engine needs to support partials, you can handle the `rootTemplatePath`:
|
|
1012
|
+
|
|
1013
|
+
```typescript
|
|
1014
|
+
import fs from 'node:fs';
|
|
1015
|
+
import { BaseEngine, type EngineInterface } from 'ecto';
|
|
1016
|
+
|
|
1017
|
+
class CustomEngineWithPartials extends BaseEngine implements EngineInterface {
|
|
1018
|
+
private partials: Map<string, string> = new Map();
|
|
1019
|
+
|
|
1020
|
+
constructor(options?: Record<string, unknown>) {
|
|
1021
|
+
super();
|
|
1022
|
+
this.names = ['custom-partials'];
|
|
1023
|
+
this.opts = options;
|
|
1024
|
+
this.setExtensions(['cpart']);
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
async render(source: string, data?: Record<string, unknown>): Promise<string> {
|
|
1028
|
+
// Load partials if rootTemplatePath is set
|
|
1029
|
+
if (this.rootTemplatePath) {
|
|
1030
|
+
await this.loadPartials(this.rootTemplatePath);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// Process includes: {{> partialName}}
|
|
1034
|
+
let result = source;
|
|
1035
|
+
const includeRegex = /\{\{>\s*(\w+)\s*\}\}/g;
|
|
1036
|
+
result = result.replace(includeRegex, (match, partialName) => {
|
|
1037
|
+
return this.partials.get(partialName) || match;
|
|
1038
|
+
});
|
|
1039
|
+
|
|
1040
|
+
// Process variables: {{variableName}}
|
|
1041
|
+
if (data) {
|
|
1042
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1043
|
+
const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
|
|
1044
|
+
result = result.replace(regex, String(value));
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
return result;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
renderSync(source: string, data?: Record<string, unknown>): string {
|
|
1052
|
+
if (this.rootTemplatePath) {
|
|
1053
|
+
this.loadPartialsSync(this.rootTemplatePath);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
let result = source;
|
|
1057
|
+
const includeRegex = /\{\{>\s*(\w+)\s*\}\}/g;
|
|
1058
|
+
result = result.replace(includeRegex, (match, partialName) => {
|
|
1059
|
+
return this.partials.get(partialName) || match;
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
if (data) {
|
|
1063
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1064
|
+
const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
|
|
1065
|
+
result = result.replace(regex, String(value));
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
return result;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
private async loadPartials(partialsPath: string): Promise<void> {
|
|
1073
|
+
const files = await fs.promises.readdir(partialsPath);
|
|
1074
|
+
for (const file of files) {
|
|
1075
|
+
if (file.endsWith('.cpart')) {
|
|
1076
|
+
const name = file.replace('.cpart', '');
|
|
1077
|
+
const content = await fs.promises.readFile(
|
|
1078
|
+
`${partialsPath}/${file}`,
|
|
1079
|
+
'utf-8'
|
|
1080
|
+
);
|
|
1081
|
+
this.partials.set(name, content);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
private loadPartialsSync(partialsPath: string): void {
|
|
1087
|
+
const files = fs.readdirSync(partialsPath);
|
|
1088
|
+
for (const file of files) {
|
|
1089
|
+
if (file.endsWith('.cpart')) {
|
|
1090
|
+
const name = file.replace('.cpart', '');
|
|
1091
|
+
const content = fs.readFileSync(`${partialsPath}/${file}`, 'utf-8');
|
|
1092
|
+
this.partials.set(name, content);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
// Usage
|
|
1099
|
+
const engine = new CustomEngineWithPartials();
|
|
1100
|
+
engine.rootTemplatePath = './templates/partials';
|
|
1101
|
+
const result = await engine.render('{{> header}}\nHello {{name}}!\n{{> footer}}', {
|
|
1102
|
+
name: 'World'
|
|
1103
|
+
});
|
|
1104
|
+
```
|
|
1105
|
+
|
|
1106
|
+
## Engine Interface Reference
|
|
1107
|
+
|
|
1108
|
+
When creating a custom engine, implement these required methods:
|
|
1109
|
+
|
|
1110
|
+
```typescript
|
|
1111
|
+
interface EngineInterface {
|
|
1112
|
+
// Engine name(s) - can support multiple aliases
|
|
1113
|
+
names: string[];
|
|
1114
|
+
|
|
1115
|
+
// The underlying template engine (if wrapping a library)
|
|
1116
|
+
engine: any;
|
|
1117
|
+
|
|
1118
|
+
// Optional configuration
|
|
1119
|
+
opts?: Record<string, unknown>;
|
|
1120
|
+
|
|
1121
|
+
// Root path for templates (used for partials/includes)
|
|
1122
|
+
rootTemplatePath?: string;
|
|
1123
|
+
|
|
1124
|
+
// Async rendering method
|
|
1125
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
1126
|
+
|
|
1127
|
+
// Synchronous rendering method
|
|
1128
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
1129
|
+
}
|
|
1130
|
+
```
|
|
1131
|
+
|
|
1132
|
+
## BaseEngine Helper Methods
|
|
1133
|
+
|
|
1134
|
+
The `BaseEngine` class provides useful helper methods:
|
|
1135
|
+
|
|
1136
|
+
- `getExtensions()`: Get registered file extensions
|
|
1137
|
+
- `setExtensions(extensions: string[])`: Set file extensions this engine handles
|
|
1138
|
+
- `deleteExtension(name: string)`: Remove a file extension
|
|
1139
|
+
|
|
1140
|
+
## Tips for Custom Engines
|
|
1141
|
+
|
|
1142
|
+
1. **Error Handling**: Always wrap your rendering logic in try-catch blocks
|
|
1143
|
+
2. **Options**: Make your engine configurable through the constructor options
|
|
1144
|
+
3. **Performance**: Consider caching compiled templates if applicable
|
|
1145
|
+
4. **Type Safety**: Use TypeScript for better development experience
|
|
1146
|
+
5. **Testing**: Write comprehensive tests for your custom engine
|
|
1147
|
+
6. **Documentation**: Document your engine's syntax and features
|
|
1148
|
+
|
|
707
1149
|
# How to Contribute
|
|
708
1150
|
|
|
709
1151
|
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.
|
package/dist/ecto.d.ts
CHANGED
package/dist/ecto.js
CHANGED
|
@@ -114,7 +114,7 @@ var EJS = class extends BaseEngine {
|
|
|
114
114
|
// src/engines/handlebars.ts
|
|
115
115
|
import fs from "fs";
|
|
116
116
|
import { fumanchu } from "@jaredwray/fumanchu";
|
|
117
|
-
import
|
|
117
|
+
import { decode } from "ent";
|
|
118
118
|
var Handlebars = class extends BaseEngine {
|
|
119
119
|
partialsPath = ["partials", "includes", "templates"];
|
|
120
120
|
constructor(options) {
|
|
@@ -130,7 +130,7 @@ var Handlebars = class extends BaseEngine {
|
|
|
130
130
|
}
|
|
131
131
|
const template = this.engine.compile(source, this.opts);
|
|
132
132
|
let result = template(data, this.opts);
|
|
133
|
-
result =
|
|
133
|
+
result = decode(result);
|
|
134
134
|
return result;
|
|
135
135
|
}
|
|
136
136
|
renderSync(source, data) {
|
|
@@ -139,7 +139,7 @@ var Handlebars = class extends BaseEngine {
|
|
|
139
139
|
}
|
|
140
140
|
const template = this.engine.compile(source, this.opts);
|
|
141
141
|
let result = template(data, this.opts);
|
|
142
|
-
result =
|
|
142
|
+
result = decode(result);
|
|
143
143
|
return result;
|
|
144
144
|
}
|
|
145
145
|
initPartials() {
|
|
@@ -819,8 +819,7 @@ var Ecto = class extends Hookified {
|
|
|
819
819
|
}
|
|
820
820
|
}
|
|
821
821
|
}
|
|
822
|
-
if (source.includes("](") &&
|
|
823
|
-
(source.includes("[") || source.includes(" && (source.includes("[") || source.includes("!["))) {
|
|
824
823
|
markdownIndicators++;
|
|
825
824
|
}
|
|
826
825
|
if (source.includes("|") && source.includes("---")) {
|
|
@@ -969,6 +968,13 @@ var Ecto = class extends Hookified {
|
|
|
969
968
|
}
|
|
970
969
|
};
|
|
971
970
|
export {
|
|
971
|
+
EJS,
|
|
972
972
|
Ecto,
|
|
973
|
-
EctoEvents
|
|
973
|
+
EctoEvents,
|
|
974
|
+
Handlebars,
|
|
975
|
+
Liquid,
|
|
976
|
+
Markdown,
|
|
977
|
+
Nunjucks,
|
|
978
|
+
Pug
|
|
974
979
|
};
|
|
980
|
+
/* v8 ignore next -- @preserve */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ecto",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.1",
|
|
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",
|
|
@@ -17,17 +17,6 @@
|
|
|
17
17
|
},
|
|
18
18
|
"author": "Jared Wray <me@jaredwray.com>",
|
|
19
19
|
"license": "MIT",
|
|
20
|
-
"scripts": {
|
|
21
|
-
"lint": "biome check --write --error-on-warnings",
|
|
22
|
-
"lint:ci": "biome check --error-on-warnings",
|
|
23
|
-
"clean": "rimraf ./dist yarn.lock package-lock.json pnpm-lock.yaml ./coverage ./node_modules ./site/readme.md ./dist-site",
|
|
24
|
-
"test": "pnpm lint && vitest run --coverage",
|
|
25
|
-
"test:ci": "pnpm lint:ci && vitest run --coverage",
|
|
26
|
-
"build": "rimraf ./dist && tsup src/ecto.ts --format esm --dts --clean",
|
|
27
|
-
"prepare": "pnpm build",
|
|
28
|
-
"website:build": "rimraf ./site/readme.md && npx -y docula build -s ./site -o ./dist-site",
|
|
29
|
-
"website:serve": "rimraf ./site/readme.md && npx -y docula serve -s ./site -o ./dist-site"
|
|
30
|
-
},
|
|
31
20
|
"keywords": [
|
|
32
21
|
"templates",
|
|
33
22
|
"generator",
|
|
@@ -49,35 +38,44 @@
|
|
|
49
38
|
"liquidjs"
|
|
50
39
|
],
|
|
51
40
|
"dependencies": {
|
|
52
|
-
"@jaredwray/fumanchu": "^4.
|
|
53
|
-
"cacheable": "^2.0
|
|
41
|
+
"@jaredwray/fumanchu": "^4.4.0",
|
|
42
|
+
"cacheable": "^2.2.0",
|
|
54
43
|
"ejs": "^3.1.10",
|
|
55
|
-
"
|
|
56
|
-
"
|
|
44
|
+
"ent": "^2.2.2",
|
|
45
|
+
"hookified": "^1.13.0",
|
|
46
|
+
"liquidjs": "^10.24.0",
|
|
57
47
|
"nunjucks": "^3.2.4",
|
|
58
48
|
"pug": "^3.0.3",
|
|
59
|
-
"
|
|
60
|
-
"writr": "^4.5.0"
|
|
49
|
+
"writr": "^5.0.1"
|
|
61
50
|
},
|
|
62
51
|
"devDependencies": {
|
|
63
|
-
"@biomejs/biome": "^2.
|
|
64
|
-
"@faker-js/faker": "^10.
|
|
52
|
+
"@biomejs/biome": "^2.3.7",
|
|
53
|
+
"@faker-js/faker": "^10.1.0",
|
|
65
54
|
"@types/ejs": "^3.1.5",
|
|
66
|
-
"@types/
|
|
55
|
+
"@types/ent": "^2.2.8",
|
|
56
|
+
"@types/node": "^24.10.1",
|
|
67
57
|
"@types/nunjucks": "^3.2.6",
|
|
68
58
|
"@types/pug": "^2.0.10",
|
|
69
|
-
"@
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"vitest": "^3.2.4",
|
|
76
|
-
"webpack": "^5.101.3"
|
|
59
|
+
"@vitest/coverage-v8": "^4.0.14",
|
|
60
|
+
"docula": "^0.31.1",
|
|
61
|
+
"rimraf": "^6.1.2",
|
|
62
|
+
"tsup": "^8.5.1",
|
|
63
|
+
"typescript": "^5.9.3",
|
|
64
|
+
"vitest": "^4.0.14"
|
|
77
65
|
},
|
|
78
66
|
"files": [
|
|
79
67
|
"dist",
|
|
80
68
|
"bin",
|
|
81
69
|
"LISCENSE"
|
|
82
|
-
]
|
|
83
|
-
|
|
70
|
+
],
|
|
71
|
+
"scripts": {
|
|
72
|
+
"lint": "biome check --write --error-on-warnings",
|
|
73
|
+
"lint:ci": "biome check --error-on-warnings",
|
|
74
|
+
"clean": "rimraf ./dist yarn.lock package-lock.json pnpm-lock.yaml ./coverage ./node_modules ./site/readme.md ./dist-site",
|
|
75
|
+
"test": "pnpm lint && vitest run --coverage",
|
|
76
|
+
"test:ci": "pnpm lint:ci && vitest run --coverage",
|
|
77
|
+
"build": "rimraf ./dist && tsup src/ecto.ts --format esm --dts --clean",
|
|
78
|
+
"website:build": "rimraf ./site/readme.md && npx -y docula build -s ./site -o ./dist-site",
|
|
79
|
+
"website:serve": "rimraf ./site/readme.md && npx -y docula serve -s ./site -o ./dist-site"
|
|
80
|
+
}
|
|
81
|
+
}
|