diff --git a/README.md b/README.md index 4cddf8e..5d8c49b 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,20 @@ Synchronously renders the shortcodes in the given content through shortcode (syn `content` must be a string. +### `Muteferrika.on(name, handler)` + +Sets the handler for the given event. + +`name` is the name of the event. See [events section](#events) for more information. + +`callback` the event handler function which will be called when the event is fired. See [events section](#events) for more information. + +## Events + +| Name | Arguments | Description | +| --- | --- | --- | +| tagRender | `fullMatch` `shortcodeOutput` `finalOutput` | This event will be fired before the shortcode tag is being replaced by the rendered output | + ## Contribution Contributions and pull requests are kindly welcomed! diff --git a/src/index.js b/src/index.js index 039ce79..5208982 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,16 @@ const { */ function Muteferrika () { this._shortcodes = [] + this._onTagRender = + function (fullMatch, finalOutput, shortcodeOutput) { + return finalOutput.replace( + new RegExp( + this.escapeRegExp(fullMatch), + 'gmi' + ), + shortcodeOutput + ) + } } // #region Private Method(s) of Muteferrika @@ -90,11 +100,10 @@ function _prepFinalOutput (fullMatch, shortcodeOutput, finalOutput) { // validations if (shortcodeOutput && typeof shortcodeOutput === 'string') { // replace the shortcode full match with the output from the original text - return finalOutput.replace( - new RegExp( - helpers.escapeRegExp(fullMatch), - 'gmi' - ), + return this._onTagRender.call( + { escapeRegExp }, + fullMatch, + finalOutput, shortcodeOutput ) } @@ -428,4 +437,35 @@ Muteferrika.prototype.renderSync = return output } +/** + * Sets the handler for the given event + * @param {string} name Name of the event + * @param {function} handler Event handler function + */ +Muteferrika.prototype.on = + function (name, handler) { + // validation + if (typeof name !== 'string') { + throw new TypeError(`"name" is expected to be a string but got: ${typeof name}`) + } + + // remove space chars from start and end of the shortcode name + const _name = name.trim() + + // validation + if (!_name) throw new TypeError('"name" cannot be empty') + if (typeof handler !== 'function') { + throw new TypeError(`"handler" is expected to be a function but got: ${typeof handler}`) + } + + switch (_name) { + case 'tagRender': + this._onTagRender = handler + + break + default: + break + } + } + module.exports = Muteferrika diff --git a/test.js b/test.js index 65737cd..8a2a6e1 100644 --- a/test.js +++ b/test.js @@ -411,6 +411,26 @@ describe( // eslint-disable-next-line expect(response).toBe('[parent][child][/parent]') }) + + // eslint-disable-next-line + test('custom `tagRender` event test', async () => { + const ibrahim = new Muteferrika() + + ibrahim.add('parent', customCb) + + ibrahim.on( + 'tagRender', + function () { + return 'tagRender event is fired.' + } + ) + + const response = + await ibrahim.render('[parent][child][/parent]') + + // eslint-disable-next-line + expect(response).toBe('tagRender event is fired.') + }) } ) @@ -700,5 +720,75 @@ describe( ibrahim.renderSync(noop) }).toThrow() }) + + // eslint-disable-next-line + test('on() - "name" validation', () => { + const ibrahim = new Muteferrika() + + // eslint-disable-next-line + expect(() => { + ibrahim.on({}, customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('', customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on([], customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on(1, customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on(false, customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on(' ', customCb) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on(noop, customCb) + }).toThrow() + }) + + // eslint-disable-next-line + test('on() - "handler" validation', () => { + const ibrahim = new Muteferrika() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('test', {}) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('test', '') + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('test', []) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('test', 1) + }).toThrow() + + // eslint-disable-next-line + expect(() => { + ibrahim.on('test', false) + }).toThrow() + }) } )