Implement a custom parser worklet
mainThe parser prop allows you to define custom markdown logic. A parser is a function that takes a plaintext string and returns an array of MarkdownRange objects.
Critical Requirements:
- The parser function must be marked as a
worklet(using the'worklet';directive) because it is executed on the UI thread as the user types. - It should return an array of objects matching the
MarkdownRangeinterface.
Supported MarkdownType values:
'bold' | 'italic' | 'strikethrough' | 'emoji' | 'mention-here' | 'mention-user' | 'mention-report' | 'link' | 'code' | 'pre' | 'blockquote' | 'h1' | 'syntax'
MarkdownRange Interface:
interface MarkdownRange {
type: MarkdownType;
start: number;
length: number;
depth?: number;
}function parser(input: string) {
'worklet';
const ranges = [];
const regexp = /\*(.*?)\*/g;
let match;
while ((match = regexp.exec(input)) !== null) {
ranges.push({start: match.index, length: 1, type: 'syntax'});
ranges.push({start: match.index + 1, length: match[1]!.length, type: 'bold'});
ranges.push({start: match.index + 1 + match[1]!.length, length: 1, type: 'syntax'});
}
return ranges;
}