inokawa / rich-textarea

A small customizable textarea for React to colorize, highlight, decorate texts, offer autocomplete and much more.
https://inokawa.github.io/rich-textarea/
MIT License
312 stars 8 forks source link

Optional custom functions for regex renderer #148

Closed alanhamlett closed 5 months ago

alanhamlett commented 5 months ago

In some cases we need the ability to run custom code when rendering, more than a regex can do. This adds an optional callback function that when provided, should return true or false on the given input text string. It's backwards compatible with current createRegexRenderer usages, but allows overwriting the regex using custom code to prevent matching when necessary.

alanhamlett commented 5 months ago

Alternatively we can use this method signature, maybe it's better?

matchers: [matcher: RegExp | ((_: string) => boolean), style: StyleOrRender][],
inokawa commented 5 months ago

matchers: [matcher: RegExp | ((_: string) => boolean), style: StyleOrRender][],

It seems better. Thank you for the suggestion!

inokawa commented 5 months ago

Oh... so how can we tokenize the text with the alternative method signature without regex?

I think your first suggestion is OK so anyway I'll merge this PR. Could you send a PR if you found a better way? Thank you!

inokawa commented 5 months ago

Released in 0.26.1.

alanhamlett commented 5 months ago

Thanks! Very quick ❤️

alanhamlett commented 5 months ago

Oops, should have added the regex result too. Adding in #149.