diff --git a/deno_dist/README.md b/deno_dist/README.md index 350dfde..449e088 100644 --- a/deno_dist/README.md +++ b/deno_dist/README.md @@ -1,3 +1,7 @@ +

+ +

+

eta (η)

diff --git a/deno_dist/config.ts b/deno_dist/config.ts index 4a87e5b..233f316 100644 --- a/deno_dist/config.ts +++ b/deno_dist/config.ts @@ -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; diff --git a/deno_dist/parse.ts b/deno_dist/parse.ts index dbd36a3..19e69fc 100644 --- a/deno_dist/parse.ts +++ b/deno_dist/parse.ts @@ -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 diff --git a/src/config.ts b/src/config.ts index 3487ed2..7429959 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 diff --git a/src/parse.ts b/src/parse.ts index 812d296..df2d1df 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -35,6 +35,15 @@ export default function parse(str: string, config: EtaConfig): Array 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 diff --git a/test/render.spec.ts b/test/render.spec.ts index 66693ba..048ee8f 100644 --- a/test/render.spec.ts +++ b/test/render.spec.ts @@ -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') + }) +})