@@ -34,56 +34,99 @@ const registeredMatchersFactories: Map<string, MatcherFactory> = new Map<
3434
3535export function TextFormatter ( {
3636 text,
37- disabled ,
37+ disable ,
3838} : {
3939 text : string ;
40- disabled ?: boolean ;
40+ disable ?: boolean ;
4141} ) : JSX . Element {
42- if ( disabled ) {
42+ console . log ( text ) ;
43+ if ( disable ) {
4344 return < > { text } </ > ;
4445 }
46+ const formatter = new textFormatter ( ) ;
47+ const elements = formatter . parseText ( text ) ;
4548
46- const elements = [ ] ;
47- let matchers = new Map < string , Matcher > ( ) ;
48- let content = '' ;
49- for ( let token of text ) {
50- registeredMatchersFactories . forEach ( ( factory , key ) => {
51- if ( matchers . has ( key ) ) {
52- return ;
53- }
49+ return < > { elements } </ > ;
50+ }
5451
55- const newMatcher = factory . createIfMatch ( token ) ;
56- if ( ! newMatcher ) {
57- return ;
58- }
59- matchers . set ( key , newMatcher ) ;
60- } ) ;
61- if ( matchers . size !== 0 ) {
62- let matcherDone = false ;
63- matchers . forEach ( ( matcher , key ) => {
64- const result = matcher . next ( token ) ;
65- switch ( result ) {
66- case MatchResult . DONE : {
67- const result = matcher . render ( ) ;
68- elements . push ( result ) ;
69- matcherDone = true ;
70- matchers = new Map < string , Matcher > ( ) ;
71- break ;
72- }
73- case MatchResult . UNMATCHED : {
74- matchers . delete ( key ) ;
75- break ;
76- }
77- }
52+ class textFormatter {
53+ elements : any [ ] ;
54+ matchers : Map < string , Matcher > ;
55+ content : string ;
56+
57+ constructor ( ) {
58+ this . elements = [ ] ;
59+ this . matchers = new Map ( ) ;
60+ this . content = '' ;
61+ }
62+
63+ parseText ( text : string ) : any [ ] {
64+ for ( let token of text ) {
65+ registeredMatchersFactories . forEach ( ( factory , key ) => {
66+ this . addMatcherIfTokenMatch ( factory , key , token ) ;
7867 } ) ;
79- content += token ;
80- if ( matcherDone ) {
81- content = '' ;
68+ let matched = false ;
69+ if ( this . matchers . size !== 0 ) {
70+ this . matchers . forEach ( ( matcher , key ) => {
71+ const result = matcher . next ( token ) ;
72+ switch ( result ) {
73+ case MatchResult . DONE : {
74+ this . handleDone ( matcher ) ;
75+ matched = true ;
76+ break ;
77+ }
78+ case MatchResult . UNMATCHED : {
79+ this . handleUnmatched ( key , token ) ;
80+ break ;
81+ }
82+ }
83+ } ) ;
84+ }
85+ if ( ! matched ) {
86+ this . content += token ;
8287 }
83- } else {
84- content += token ;
8588 }
89+ this . pushContent ( ) ;
90+ return this . elements ;
91+ }
92+
93+ addMatcherIfTokenMatch (
94+ factory : MatcherFactory ,
95+ matcherKey : string ,
96+ token : string ,
97+ ) {
98+ if ( this . matchers . has ( matcherKey ) ) {
99+ return ;
100+ }
101+
102+ const newMatcher = factory . createIfMatch ( token ) ;
103+ if ( ! newMatcher ) {
104+ return ;
105+ }
106+ this . pushContent ( ) ;
107+ this . matchers . set ( matcherKey , newMatcher ) ;
108+ }
109+
110+ handleDone ( matcher : Matcher ) {
111+ const result = matcher . render ( ) ;
112+ this . elements . push ( result ) ;
113+ this . matchers = new Map < string , Matcher > ( ) ;
114+ this . content = '' ;
115+ }
116+
117+ handleUnmatched ( matcherToDelete : string , token : string ) {
118+ this . matchers . delete ( matcherToDelete ) ;
119+ if ( this . matchers . size === 0 ) {
120+ this . content += token ;
121+ this . pushContent ( ) ;
122+ }
123+ }
124+
125+ pushContent ( ) {
126+ if ( this . content === '' ) {
127+ return ;
128+ }
129+ this . elements . push ( this . content ) ;
130+ this . content = '' ;
86131 }
87- elements . push ( content ) ;
88- return < > { elements } </ > ;
89132}
0 commit comments