Skip to content

Commit

Permalink
feat: tagRender event
Browse files Browse the repository at this point in the history
  • Loading branch information
hsynlms committed Mar 26, 2021
1 parent 5c86c3a commit cda8d34
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
50 changes: 45 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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
90 changes: 90 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
})
}
)

Expand Down Expand Up @@ -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()
})
}
)

0 comments on commit cda8d34

Please sign in to comment.