Skip to content

Commit

Permalink
Add processTemplate plugin hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 18, 2020
1 parent c4557cb commit 370be52
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
4 changes: 4 additions & 0 deletions deno_dist/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<p align="center">
<img align="center" width="50%" src="https://user-images.githubusercontent.com/25597854/93541153-d8c8fe80-f912-11ea-9e65-a0089ca6d2b6.png">
</p>

<h1 align="center" style="text-align: center; width: fit-content; margin-left: auto; margin-right: auto;">eta (η)</h1>

<p align="center">
Expand Down
8 changes: 7 additions & 1 deletion deno_dist/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export interface EtaConfig {
};

/** Array of plugins */
plugins: Array<{ processFnString?: Function; processAST?: Function }>;
plugins: Array<
{
processFnString?: Function;
processAST?: Function;
processTemplate?: Function;
}
>;

/** Remove all safe-to-remove whitespace */
rmWhitespace: boolean;
Expand Down
9 changes: 9 additions & 0 deletions deno_dist/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export default function parse(
var lastIndex = 0;
var parseOptions = config.parse;

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i];
if (plugin.processTemplate) {
str = plugin.processTemplate(str, config);
}
}
}

/* Adding for EJS compatibility */
if (config.rmWhitespace) {
// Code taken directly from EJS
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface EtaConfig {
}

/** Array of plugins */
plugins: Array<{ processFnString?: Function; processAST?: Function }>
plugins: Array<{ processFnString?: Function; processAST?: Function; processTemplate?: Function }>

/** Remove all safe-to-remove whitespace */
rmWhitespace: boolean
Expand Down
9 changes: 9 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
var lastIndex = 0
var parseOptions = config.parse

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i]
if (plugin.processTemplate) {
str = plugin.processTemplate(str, config)
}
}
}

/* Adding for EJS compatibility */
if (config.rmWhitespace) {
// Code taken directly from EJS
Expand Down
54 changes: 54 additions & 0 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,57 @@ describe('Renders with different scopes', () => {
)
})
})

describe('processTemplate plugin', () => {
it('Simple plugin works correctly', () => {
let template = ':thumbsup:'

let emojiTransform = {
processTemplate: function (str: string) {
return str.replace(':thumbsup:', '👍')
}
}

let res = render(
template,
{},
{
plugins: [emojiTransform]
}
)

expect(res).toEqual('👍')
})

it('Multiple chained plugins work correctly', () => {
let template = ':thumbsup: This is a cool template'

let emojiTransform = {
processTemplate: function (str: string) {
return str.replace(':thumbsup:', '👍')
}
}

let capitalizeCool = {
processTemplate: function (str: string) {
return str.replace('cool', 'COOL')
}
}

let replaceThumbsUp = {
processTemplate: function (str: string) {
return str.replace('👍', '✨')
}
}

let res = render(
template,
{},
{
plugins: [emojiTransform, capitalizeCool, replaceThumbsUp]
}
)

expect(res).toEqual('✨ This is a COOL template')
})
})

0 comments on commit 370be52

Please sign in to comment.