TypeScript highlighter that takes a data object, and applies a highlighting function to selected property values.
I needed to render a data object representing an article. Before rendering it to HTML, I wanted to mark certain parts of the text with a background color.
- Create string or regex-based highlighters descriptions
- Create a factory function that takes these highlighters and converts them to functions that will wrap matching text with an HTML mark element and background color. It returns a highlighter.
- Use the highlighter to convert the original data object to a data object with the same shape, but where matching text is highlighted.