@zcorky/words-parser
v0.0.8
Published
parse and transform word in text, use custom renderers for words
Downloads
9
Maintainers
Readme
words-parser
Parse and transform word in text, use custom renderers for words.
- :sparkles: Support Chinese
- 🌐 Support English (WIP)
Install
$ npm install @zcorky/words-parser
Usage
import * as parser from '@zcorky/words-parser';
type Args = {
role: string;
};
const text: string = 'I am zero, who are you ?';
const words: parser.Words<Args> = [
{ text: 'zero', args: { role: 'hero' } },
{ text: 'you', args: { role: 'goodman' } },
];
const parsedToken = parser.parse(text, words);
// => [
// { text: 'zero', args: { role: 'hero' }, start: 5, end: 9 },
// { text: 'you', args: { role: 'goodman' }, start: 19, end: 22 },
// ]
const transformedToken = parser.transform(text, parsedToken);
// => [
// { type: 'TEXT', text: 'I\' am ', start: 0, end: 5 },
// { type: 'CUSTOM', text: 'zero', args: { role: 'hero' }, start: 5, end: 9 },
// { type: 'TEXT', text: ', who are ', start: 9, end: 19 },
// { type: 'CUSTOM', text: 'you', args: { role: 'goodman' }, start: 19, end: 22 },
// { type: 'TEXT', text: ', ' ?', start: 22, end: 24 },
// ]
// Custom Renderer with React
const renderers: parser.Renderers<Args, ReactNode> = {
TEXT(word) {
return <span>{word.text}</span>;
},
CUSTOM(word) {
return <Keyword text={word.text} />;
},
};
API
- See the detailed API Reference.
License
MIT © Moeover