@@ -7,7 +7,7 @@ type ParsedSection = {
77function parseStyles (
88 inputText : string ,
99 customParsers : Array < { matcher : RegExp ; stylename : string } > = [ ]
10- ) {
10+ ) : ParsedSection [ ] {
1111 /** Split the input text into an array of lines */
1212 const inputLines = inputText . split ( / [ \r \n \3] / g) ;
1313
@@ -64,6 +64,84 @@ function parseStyles(
6464 return parsedStyles . flat ( ) ;
6565}
6666
67+ function styleByIndex (
68+ inputText : string ,
69+ {
70+ boldIndexes,
71+ italicsIndexes,
72+ } : { boldIndexes : number [ ] ; italicsIndexes : number [ ] }
73+ ) : ParsedSection [ ] {
74+ const inputLines = inputText . split ( / [ \r \n \3] / g) ;
75+ const words = inputLines . flatMap ( ( line , index ) =>
76+ line
77+ . trim ( )
78+ . split ( ' ' )
79+ . map ( ( word ) => ( { content : word , line : index } ) )
80+ ) ;
81+
82+ const wordsWithStyles = words . map ( ( word , index ) => {
83+ let style = 'regular' ;
84+ if ( boldIndexes . includes ( index ) ) style = 'bold' ;
85+ if ( italicsIndexes . includes ( index ) ) style = 'italics' ;
86+ return {
87+ ...word ,
88+ style,
89+ } ;
90+ } ) ;
91+
92+ return wordsWithStyles ;
93+ }
94+
95+ function styleBySearch (
96+ inputText : string ,
97+ {
98+ boldString,
99+ italicsString,
100+ } : { boldString ?: string ; italicsString ?: string } = { }
101+ ) : ParsedSection [ ] {
102+ let regex ;
103+ const italics = italicsString ? italicsString . trim ( ) : '' ;
104+ const bold = boldString ? boldString . trim ( ) : '' ;
105+ if ( boldString && italicsString ) {
106+ regex = new RegExp ( `(${ bold } )|(${ italics } )` , 'g' ) ;
107+ } else if ( boldString ) {
108+ regex = new RegExp ( `(${ bold } )` , 'g' ) ;
109+ } else if ( italicsString ) {
110+ regex = new RegExp ( `(${ italics } )` , 'g' ) ;
111+ }
112+
113+ if ( ! regex ) {
114+ return [
115+ {
116+ content : inputText ,
117+ style : 'regular' ,
118+ line : 0 ,
119+ } ,
120+ ] ;
121+ }
122+
123+ const inputLines = inputText . split ( / [ \r \n \3] / g) ;
124+
125+ return inputLines . flatMap ( ( line , index ) => {
126+ return line
127+ . split ( regex )
128+ . filter ( Boolean )
129+ . map ( ( part ) => {
130+ const style =
131+ part === boldString
132+ ? 'bold'
133+ : part === italicsString
134+ ? 'italics'
135+ : 'regular' ;
136+ return {
137+ content : part . trim ( ) ,
138+ style,
139+ line : index ,
140+ } ;
141+ } ) ;
142+ } ) ;
143+ }
144+
67145const defaultFontMap = {
68146 bold : 'Arial-BoldMT' ,
69147 italics : 'Arial-ItalicMT' ,
@@ -74,4 +152,4 @@ const defaultFontMap = {
74152const version : string = '_npmVersion' ;
75153
76154// Export values to appear in jsx files
77- export { parseStyles , defaultFontMap , version } ;
155+ export { parseStyles , styleByIndex , styleBySearch , defaultFontMap , version } ;
0 commit comments