Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to allow users to register their own parser hooks #46

Merged
merged 6 commits into from
Apr 30, 2021

Conversation

siddharthvp
Copy link
Collaborator

@siddharthvp siddharthvp commented Mar 26, 2021

A simple contrived example would be:

  • banana.registerParserPlugin('foobar', nodes => nodes[0] === 'foo' ? nodes[1]: nodes[2])

Which makes

  • banana.i18n('{{foobar:foo|first|second}}') --> 'first'
  • banana.i18n('{{foobar:bar|first|second}}') --> 'second'

This can be used practically for a lot of purposes, particularly for operations that require some data to be available which can be fetched from the client script. For example on mediawiki sites, a client script could add a hook for formatting dates using the month names fetched from MediaWiki API.

Let me know what you think about this. Another less explicit way of allowing these hooks would be to export the BananaEmitter class, so that clients can directly modify its prototype.

@siddharthvp
Copy link
Collaborator Author

@santhoshtr any thoughts?

@santhoshtr
Copy link
Member

santhoshtr commented Apr 16, 2021

This looks like a good idea to me. Your PR make sense too. This is more or less in alignment with the extend option available in jquery.i18n(https://github.com/wikimedia/jquery.i18n#extending-the-parser).

Could you please add some test too?

Also, documentation(README.md)

A simple contrived example would be:

- banana.registerParserPlugin('foobar', nodes => nodes[0] === 'foo' ? nodes[1]: nodes[2])

Which makes
- banana.i18n('{{foobar:foo|first|second') --> 'first'
- banana.i18n('{{foobar:bar|first|second') --> 'second'

This can be used practically for a lot of purposes, particularly for operations that require some data to be available which can be fetched from the client script. For example on mediawiki sites, a client script could add a hook for formatting dates using the month names fetched from MediaWiki API.

Let me know what you think about this. Another less explicit way of allowing these hooks would be to export the BananaEmitter class, so that clients can directly modify its prototype.
@siddharthvp
Copy link
Collaborator Author

Done: updated documentation (adapted from jquery-i18n documentation), added tests and typescript type.

Copy link
Member

@santhoshtr santhoshtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general looks good. I left some minor comments. Also, the master version just migrated to github workflows. So please make sure CI is passing. Thanks for working on this

types/banana-i18n.d.ts Outdated Show resolved Hide resolved
test/banana.test.js Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@santhoshtr santhoshtr merged commit e10fdf7 into wikimedia:master Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants