@@ -67,23 +67,7 @@ export function body(
67
67
return [ this . strip ( frontBack [ 0 ] ) , this . strip ( frontBack [ 1 ] ) ] as const
68
68
return frontBack
69
69
}
70
- if ( template . templateType . tag === 'standard' ) {
71
- return shortify (
72
- handleStandard . call (
73
- this ,
74
- card ,
75
- note ,
76
- template as StandardTemplate ,
77
- short ,
78
- ) ,
79
- )
80
- } else if ( template . templateType . tag === 'cloze' ) {
81
- return shortify (
82
- handleCloze . call ( this , card , note , template as ClozeTemplate , short ) ,
83
- )
84
- } else {
85
- assertNever ( template . templateType )
86
- }
70
+ return shortify ( handle . call ( this , card , note , template , short ) )
87
71
}
88
72
export interface ReplacerArgs {
89
73
initialValue : string
@@ -93,88 +77,11 @@ export interface ReplacerArgs {
93
77
template : Template
94
78
}
95
79
96
- export type StandardReplacerArgs = Omit < ReplacerArgs , 'template' > & {
97
- template : StandardTemplate
98
- }
99
-
100
- export type ClozeReplacerArgs = Omit < ReplacerArgs , 'template' > & {
101
- template : ClozeTemplate
102
- }
103
-
104
- export type Replacers = Map <
105
- string ,
106
- ( this : RenderContainer , args : ReplacerArgs ) => string
107
- >
108
-
109
- export type StandardReplacer = (
110
- this : RenderContainer ,
111
- args : StandardReplacerArgs ,
112
- ) => string
113
-
114
- export const standardReplacers : Map < string , StandardReplacer > = new Map <
115
- string ,
116
- StandardReplacer
117
- > ( [
118
- [ 'simpleFieldReplacer' , simpleFieldReplacer ] ,
119
- [ 'conditionalReplacer' , conditionalReplacer ] ,
120
- [ 'antiConditionalReplacer' , antiConditionalReplacer ] ,
121
- [ 'tagReplacer' , tagReplacer ] ,
122
- [ 'stripHtmlReplacer' , stripHtmlReplacer ] ,
123
- ] )
124
-
125
- function handleStandard (
126
- this : RenderContainer ,
127
- card : Card ,
128
- note : Note ,
129
- template : StandardTemplate ,
130
- short : boolean ,
131
- ) {
132
- let { front, back, shortFront, shortBack } =
133
- template . templateType . templates . find ( ( t ) => t . id === card . ord ) ??
134
- throwExp ( `Ord ${ card . ord } not found` )
135
- if ( short ) {
136
- front = shortFront == null || shortFront . trim ( ) === '' ? front : shortFront
137
- back = shortBack == null || shortBack . trim ( ) === '' ? back : shortBack
138
- }
139
- function replaceFields (
140
- this : RenderContainer ,
141
- isFront : boolean ,
142
- seed : string ,
143
- ) {
144
- let r = seed
145
- const args = {
146
- initialValue : seed ,
147
- isFront,
148
- card,
149
- note,
150
- template,
151
- }
152
- for ( const [ , replacer ] of this . standardReplacers ) {
153
- args . initialValue = r
154
- r = replacer . bind ( this ) ( args )
155
- }
156
- return r
157
- }
158
- const frontSide = replaceFields . call ( this , true , front )
159
- if ( frontSide === front || frontSide === '' ) {
160
- return null
161
- } else {
162
- const backSide = replaceFields
163
- . call ( this , false , back )
164
- . replace ( '{{FrontSide}}' , replaceFields . call ( this , false , front ) )
165
- return [ frontSide , backSide ] as const
166
- }
167
- }
80
+ export type Replacers = Map < string , Replacer >
168
81
169
- export type ClozeReplacer = (
170
- this : RenderContainer ,
171
- args : ClozeReplacerArgs ,
172
- ) => string
82
+ export type Replacer = ( this : RenderContainer , args : ReplacerArgs ) => string
173
83
174
- export const clozeReplacers : Map < string , ClozeReplacer > = new Map <
175
- string ,
176
- ClozeReplacer
177
- > ( [
84
+ export const replacers : Map < string , Replacer > = new Map < string , Replacer > ( [
178
85
[ 'simpleFieldReplacer' , simpleFieldReplacer ] ,
179
86
[ 'conditionalReplacer' , conditionalReplacer ] ,
180
87
[ 'antiConditionalReplacer' , antiConditionalReplacer ] ,
@@ -183,14 +90,18 @@ export const clozeReplacers: Map<string, ClozeReplacer> = new Map<
183
90
[ 'clozeReplacer' , clozeReplacer ] ,
184
91
] )
185
92
186
- function handleCloze (
93
+ function handle (
187
94
this : RenderContainer ,
188
95
card : Card ,
189
96
note : Note ,
190
- template : ClozeTemplate ,
97
+ template : Template ,
191
98
short : boolean ,
192
99
) {
193
- let { front, back, shortFront, shortBack } = template . templateType . template
100
+ let { front, back, shortFront, shortBack } =
101
+ template . templateType . tag === 'standard'
102
+ ? template . templateType . templates . find ( ( t ) => t . id === card . ord ) ??
103
+ throwExp ( `Ord ${ card . ord } not found` )
104
+ : template . templateType . template
194
105
if ( short ) {
195
106
front = shortFront == null || shortFront . trim ( ) === '' ? front : shortFront
196
107
back = shortBack == null || shortBack . trim ( ) === '' ? back : shortBack
@@ -208,14 +119,14 @@ function handleCloze(
208
119
note,
209
120
template,
210
121
}
211
- for ( const [ , replacer ] of this . clozeReplacers ) {
122
+ for ( const [ , replacer ] of this . replacers ) {
212
123
args . initialValue = r
213
124
r = replacer . bind ( this ) ( args )
214
125
}
215
126
return r
216
127
}
217
128
const frontSide = replaceFields . call ( this , true , front )
218
- if ( frontSide === front ) {
129
+ if ( frontSide === front || frontSide === '' ) {
219
130
return null
220
131
} else {
221
132
const backSide = replaceFields
@@ -224,7 +135,6 @@ function handleCloze(
224
135
return [ frontSide , backSide ] as const
225
136
}
226
137
}
227
-
228
138
function getClozeFields (
229
139
this : RenderContainer ,
230
140
frontTemplate : string ,
@@ -251,14 +161,10 @@ export function simpleFieldReplacer(
251
161
}
252
162
253
163
function tagReplacer ( this : RenderContainer , args : ReplacerArgs ) {
254
- const replacersMap =
255
- args . template . templateType . tag === 'standard'
256
- ? ( this . standardReplacers as Replacers )
257
- : ( this . clozeReplacers as Replacers )
258
164
const replacers = [
259
- replacersMap . get ( 'simpleFieldReplacer' ) ,
260
- replacersMap . get ( 'conditionalReplacer' ) ,
261
- replacersMap . get ( 'antiConditionalReplacer' ) ,
165
+ this . replacers . get ( 'simpleFieldReplacer' ) ,
166
+ this . replacers . get ( 'conditionalReplacer' ) ,
167
+ this . replacers . get ( 'antiConditionalReplacer' ) ,
262
168
] . filter ( notEmpty )
263
169
let r = args . initialValue
264
170
const args2 = {
@@ -320,71 +226,74 @@ function stripHtmlReplacer(
320
226
321
227
function clozeReplacer (
322
228
this : RenderContainer ,
323
- { initialValue, isFront, card, note, template } : ClozeReplacerArgs ,
229
+ { initialValue, isFront, card, note, template } : ReplacerArgs ,
324
230
) {
325
231
let r = initialValue
326
- note . fieldValues . forEach ( ( value , fieldName ) => {
327
- const i = ( card . ord . valueOf ( ) + 1 ) . toString ( )
328
- const clozeFields = getClozeFields . call (
329
- this ,
330
- template . templateType . template . front ,
331
- )
332
- const indexMatch = Array . from (
333
- value . matchAll ( this . clozeRegex ) ,
334
- ( x ) =>
335
- x . groups ?. clozeIndex ??
336
- throwExp ( 'This error should never occur - is `clozeRegex` broken?' ) ,
337
- ) . includes ( i )
338
- if ( ! indexMatch && clozeFields . includes ( fieldName ) ) {
339
- value = ''
340
- } else {
341
- value = Array . from ( value . matchAll ( this . clozeRegex ) )
342
- . filter (
343
- ( x ) =>
344
- ( x . groups ?. clozeIndex ??
232
+ if ( template . templateType . tag === 'cloze' ) {
233
+ const front = template . templateType . template . front
234
+ note . fieldValues . forEach ( ( value , fieldName ) => {
235
+ const i = ( card . ord . valueOf ( ) + 1 ) . toString ( )
236
+ const clozeFields = getClozeFields . call ( this , front )
237
+ const indexMatch = Array . from (
238
+ value . matchAll ( this . clozeRegex ) ,
239
+ ( x ) =>
240
+ x . groups ?. clozeIndex ??
241
+ throwExp ( 'This error should never occur - is `clozeRegex` broken?' ) ,
242
+ ) . includes ( i )
243
+ if ( ! indexMatch && clozeFields . includes ( fieldName ) ) {
244
+ value = ''
245
+ } else {
246
+ value = Array . from ( value . matchAll ( this . clozeRegex ) )
247
+ . filter (
248
+ ( x ) =>
249
+ ( x . groups ?. clozeIndex ??
250
+ throwExp (
251
+ 'This error should never occur - is `clozeRegex` broken?' ,
252
+ ) ) !== i ,
253
+ )
254
+ . map ( ( x ) => ( {
255
+ completeMatch : x [ 0 ] ,
256
+ answer :
257
+ x . groups ?. answer ??
345
258
throwExp (
346
259
'This error should never occur - is `clozeRegex` broken?' ,
347
- ) ) !== i ,
348
- )
349
- . map ( ( x ) => ( {
350
- completeMatch : x [ 0 ] ,
351
- answer :
352
- x . groups ?. answer ??
353
- throwExp ( 'This error should never occur - is `clozeRegex` broken?' ) ,
354
- } ) )
355
- . reduce (
356
- ( state , { completeMatch, answer } ) =>
357
- state . replace ( completeMatch , answer ) ,
358
- value ,
359
- )
360
- }
361
- if ( isFront ) {
362
- const regexMatches : ReadonlyArray < readonly [ string | undefined , string ] > =
363
- Array . from ( value . matchAll ( this . clozeRegex ) , ( x ) => [
260
+ ) ,
261
+ } ) )
262
+ . reduce (
263
+ ( state , { completeMatch, answer } ) =>
264
+ state . replace ( completeMatch , answer ) ,
265
+ value ,
266
+ )
267
+ }
268
+ if ( isFront ) {
269
+ const regexMatches : ReadonlyArray <
270
+ readonly [ string | undefined , string ]
271
+ > = Array . from ( value . matchAll ( this . clozeRegex ) , ( x ) => [
364
272
x . groups ?. hint ,
365
273
x [ 0 ] ,
366
274
] )
367
- const bracketed = regexMatches . reduce ( ( current , [ hint , rawCloze ] ) => {
368
- const brackets = `
275
+ const bracketed = regexMatches . reduce ( ( current , [ hint , rawCloze ] ) => {
276
+ const brackets = `
369
277
<span class="cloze-brackets-front">[</span>
370
278
<span class="cloze-filler-front">${ hint ?? '...' } </span>
371
279
<span class="cloze-brackets-front">]</span>
372
280
`
373
- return current . replace ( rawCloze , brackets )
374
- } , value )
375
- r = r . replace ( clozeTemplateFor . call ( this , fieldName ) , bracketed )
376
- } else {
377
- const answer = value . replace (
378
- this . clozeRegex ,
379
- `
281
+ return current . replace ( rawCloze , brackets )
282
+ } , value )
283
+ r = r . replace ( clozeTemplateFor . call ( this , fieldName ) , bracketed )
284
+ } else {
285
+ const answer = value . replace (
286
+ this . clozeRegex ,
287
+ `
380
288
<span class="cloze-brackets-back">[</span>
381
289
$<answer>
382
290
<span class="cloze-brackets-back">]</span>
383
291
` ,
384
- )
385
- r = r . replace ( clozeTemplateFor . call ( this , fieldName ) , answer )
386
- }
387
- } )
292
+ )
293
+ r = r . replace ( clozeTemplateFor . call ( this , fieldName ) , answer )
294
+ }
295
+ } )
296
+ }
388
297
return r
389
298
}
390
299
0 commit comments