Skip to content

Commit b2cc48e

Browse files
committed
moved a conditional check into clozeReplacer
1 parent ff702d0 commit b2cc48e

File tree

2 files changed

+70
-160
lines changed

2 files changed

+70
-160
lines changed

shared-dom/src/cardHtml.ts

+68-156
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,7 @@ export function body(
6767
return [this.strip(frontBack[0]), this.strip(frontBack[1])] as const
6868
return frontBack
6969
}
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))
8771
}
8872
export interface ReplacerArgs {
8973
initialValue: string
@@ -93,88 +77,14 @@ export interface ReplacerArgs {
9377
template: Template
9478
}
9579

96-
export type StandardReplacerArgs = Omit<ReplacerArgs, 'template'> & {
97-
template: StandardTemplate
98-
}
99-
100-
export type ClozeReplacerArgs = Omit<ReplacerArgs, 'template'> & {
101-
template: ClozeTemplate
102-
}
103-
10480
export type Replacers = Map<
10581
string,
10682
(this: RenderContainer, args: ReplacerArgs) => string
10783
>
10884

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-
}
168-
169-
export type ClozeReplacer = (
170-
this: RenderContainer,
171-
args: ClozeReplacerArgs,
172-
) => string
85+
export type Replacer = (this: RenderContainer, args: ReplacerArgs) => string
17386

174-
export const clozeReplacers: Map<string, ClozeReplacer> = new Map<
175-
string,
176-
ClozeReplacer
177-
>([
87+
export const replacers: Map<string, Replacer> = new Map<string, Replacer>([
17888
['simpleFieldReplacer', simpleFieldReplacer],
17989
['conditionalReplacer', conditionalReplacer],
18090
['antiConditionalReplacer', antiConditionalReplacer],
@@ -183,14 +93,18 @@ export const clozeReplacers: Map<string, ClozeReplacer> = new Map<
18393
['clozeReplacer', clozeReplacer],
18494
])
18595

186-
function handleCloze(
96+
function handle(
18797
this: RenderContainer,
18898
card: Card,
18999
note: Note,
190-
template: ClozeTemplate,
100+
template: Template,
191101
short: boolean,
192102
) {
193-
let { front, back, shortFront, shortBack } = template.templateType.template
103+
let { front, back, shortFront, shortBack } =
104+
template.templateType.tag === 'standard'
105+
? template.templateType.templates.find((t) => t.id === card.ord) ??
106+
throwExp(`Ord ${card.ord} not found`)
107+
: template.templateType.template
194108
if (short) {
195109
front = shortFront == null || shortFront.trim() === '' ? front : shortFront
196110
back = shortBack == null || shortBack.trim() === '' ? back : shortBack
@@ -208,14 +122,14 @@ function handleCloze(
208122
note,
209123
template,
210124
}
211-
for (const [, replacer] of this.clozeReplacers) {
125+
for (const [, replacer] of this.replacers) {
212126
args.initialValue = r
213127
r = replacer.bind(this)(args)
214128
}
215129
return r
216130
}
217131
const frontSide = replaceFields.call(this, true, front)
218-
if (frontSide === front) {
132+
if (frontSide === front || frontSide === '') {
219133
return null
220134
} else {
221135
const backSide = replaceFields
@@ -224,7 +138,6 @@ function handleCloze(
224138
return [frontSide, backSide] as const
225139
}
226140
}
227-
228141
function getClozeFields(
229142
this: RenderContainer,
230143
frontTemplate: string,
@@ -251,14 +164,10 @@ export function simpleFieldReplacer(
251164
}
252165

253166
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)
258167
const replacers = [
259-
replacersMap.get('simpleFieldReplacer'),
260-
replacersMap.get('conditionalReplacer'),
261-
replacersMap.get('antiConditionalReplacer'),
168+
this.replacers.get('simpleFieldReplacer'),
169+
this.replacers.get('conditionalReplacer'),
170+
this.replacers.get('antiConditionalReplacer'),
262171
].filter(notEmpty)
263172
let r = args.initialValue
264173
const args2 = {
@@ -320,71 +229,74 @@ function stripHtmlReplacer(
320229

321230
function clozeReplacer(
322231
this: RenderContainer,
323-
{ initialValue, isFront, card, note, template }: ClozeReplacerArgs,
232+
{ initialValue, isFront, card, note, template }: ReplacerArgs,
324233
) {
325234
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 ??
235+
if (template.templateType.tag === 'cloze') {
236+
const front = template.templateType.template.front
237+
note.fieldValues.forEach((value, fieldName) => {
238+
const i = (card.ord.valueOf() + 1).toString()
239+
const clozeFields = getClozeFields.call(this, front)
240+
const indexMatch = Array.from(
241+
value.matchAll(this.clozeRegex),
242+
(x) =>
243+
x.groups?.clozeIndex ??
244+
throwExp('This error should never occur - is `clozeRegex` broken?'),
245+
).includes(i)
246+
if (!indexMatch && clozeFields.includes(fieldName)) {
247+
value = ''
248+
} else {
249+
value = Array.from(value.matchAll(this.clozeRegex))
250+
.filter(
251+
(x) =>
252+
(x.groups?.clozeIndex ??
253+
throwExp(
254+
'This error should never occur - is `clozeRegex` broken?',
255+
)) !== i,
256+
)
257+
.map((x) => ({
258+
completeMatch: x[0],
259+
answer:
260+
x.groups?.answer ??
345261
throwExp(
346262
'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) => [
263+
),
264+
}))
265+
.reduce(
266+
(state, { completeMatch, answer }) =>
267+
state.replace(completeMatch, answer),
268+
value,
269+
)
270+
}
271+
if (isFront) {
272+
const regexMatches: ReadonlyArray<
273+
readonly [string | undefined, string]
274+
> = Array.from(value.matchAll(this.clozeRegex), (x) => [
364275
x.groups?.hint,
365276
x[0],
366277
])
367-
const bracketed = regexMatches.reduce((current, [hint, rawCloze]) => {
368-
const brackets = `
278+
const bracketed = regexMatches.reduce((current, [hint, rawCloze]) => {
279+
const brackets = `
369280
<span class="cloze-brackets-front">[</span>
370281
<span class="cloze-filler-front">${hint ?? '...'}</span>
371282
<span class="cloze-brackets-front">]</span>
372283
`
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-
`
284+
return current.replace(rawCloze, brackets)
285+
}, value)
286+
r = r.replace(clozeTemplateFor.call(this, fieldName), bracketed)
287+
} else {
288+
const answer = value.replace(
289+
this.clozeRegex,
290+
`
380291
<span class="cloze-brackets-back">[</span>
381292
$<answer>
382293
<span class="cloze-brackets-back">]</span>
383294
`,
384-
)
385-
r = r.replace(clozeTemplateFor.call(this, fieldName), answer)
386-
}
387-
})
295+
)
296+
r = r.replace(clozeTemplateFor.call(this, fieldName), answer)
297+
}
298+
})
299+
}
388300
return r
389301
}
390302

shared-dom/src/renderContainer.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import {
55
html,
66
renderTemplate,
77
strip,
8-
standardReplacers,
9-
clozeReplacers,
8+
replacers,
109
noteOrds,
1110
templateIndexes,
1211
} from './cardHtml.js'
1312

1413
export const defaultRenderContainer = {
15-
standardReplacers,
16-
clozeReplacers,
14+
replacers,
1715
clozeRegex,
1816
clozeTemplateRegex,
1917
body,

0 commit comments

Comments
 (0)