@@ -134,13 +134,29 @@ export class FormatConverter {
134134 return [ note_text . replace ( regexp , mask ) , matches ]
135135 }
136136
137- decensor ( note_text : string , mask : string , replacements : string [ ] , escape : boolean ) : string {
138- let i = 0 ;
139- return note_text . replace ( new RegExp ( mask , 'g' ) , ( ) : string => {
140- let replacement : string = replacements [ i ++ ] ;
141- return escape ? escapeHtml ( replacement ) : replacement ;
142- } ) ;
143- }
137+ decensor ( note_text : string , mask : string , replacements : string [ ] , escape : boolean ) : string {
138+ let index = 0 ;
139+
140+ // note_text example: "The OBSTOANKICODEDISPLAY is worth OBSTOANKICODEDISPLAY today"
141+ // maskGlobalReg example: /OBSTOANKICODEDISPLAY/g
142+ const maskGlobalRegex = new RegExp ( mask , 'g' ) ;
143+
144+ const matchCount = ( note_text . match ( maskGlobalRegex ) || [ ] ) . length ;
145+
146+ // Validate that we have exactly enough replacements
147+ if ( matchCount !== replacements . length ) {
148+ throw new Error ( `Mismatch between placeholders (${ matchCount } ) and replacements (${ replacements . length } )` ) ;
149+ }
150+
151+ // replacements example: ["10", "15"]
152+ note_text = note_text . replace ( maskGlobalRegex , ( ) => {
153+ const replacement = replacements [ index ++ ] ;
154+ return escape ? escapeHtml ( replacement ) : replacement ;
155+ } ) ;
156+
157+ // note_text expected: "The 10 is worth 15 today"
158+ return note_text ;
159+ }
144160
145161 format ( note_text : string , cloze : boolean , highlights_to_cloze : boolean ) : string {
146162 note_text = this . obsidian_to_anki_math ( note_text )
0 commit comments