Skip to content

Commit 677081a

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

File tree

4 files changed

+73
-186
lines changed

4 files changed

+73
-186
lines changed

example-plugins/solid/src/index.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@ function clozeTemplateRegex(c: Container): RegExp {
1212
const services = (c: Container): Partial<Container> => {
1313
return {
1414
clozeTemplateRegex: clozeTemplateRegex(c),
15-
standardReplacers: new Map(c.standardReplacers).set(
16-
'editFieldReplacer',
17-
({ initialValue, isFront, card, note, template }) => {
18-
let r = initialValue
19-
note.fieldValues.forEach((value, fieldName) => {
20-
r = r.replace(new RegExp(`{{(?:edit:)?${fieldName}}}`), value)
21-
})
22-
return r
23-
},
24-
),
25-
clozeReplacers: new Map(c.clozeReplacers).set(
15+
replacers: new Map(c.replacers).set(
2616
'editFieldReplacer',
2717
({ initialValue, isFront, card, note, template }) => {
2818
let r = initialValue

example-plugins/svelte/src/index.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,7 @@ function clozeTemplateRegex(c: Container): RegExp {
1515
const services = (c: Container): Partial<Container> => {
1616
return {
1717
clozeTemplateRegex: clozeTemplateRegex(c),
18-
standardReplacers: new Map(c.standardReplacers).set(
19-
'editFieldReplacer',
20-
({ initialValue, isFront, card, note, template }) => {
21-
let r = initialValue
22-
note.fieldValues.forEach((value, fieldName) => {
23-
r = r.replace(new RegExp(`{{(?:edit:)?${fieldName}}}`), value)
24-
})
25-
return r
26-
},
27-
),
28-
clozeReplacers: new Map(c.clozeReplacers).set(
18+
replacers: new Map(c.replacers).set(
2919
'editFieldReplacer',
3020
({ initialValue, isFront, card, note, template }) => {
3121
let r = initialValue

shared-dom/src/cardHtml.ts

+69-160
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,11 @@ 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-
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>
16881

169-
export type ClozeReplacer = (
170-
this: RenderContainer,
171-
args: ClozeReplacerArgs,
172-
) => string
82+
export type Replacer = (this: RenderContainer, args: ReplacerArgs) => string
17383

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>([
17885
['simpleFieldReplacer', simpleFieldReplacer],
17986
['conditionalReplacer', conditionalReplacer],
18087
['antiConditionalReplacer', antiConditionalReplacer],
@@ -183,14 +90,18 @@ export const clozeReplacers: Map<string, ClozeReplacer> = new Map<
18390
['clozeReplacer', clozeReplacer],
18491
])
18592

186-
function handleCloze(
93+
function handle(
18794
this: RenderContainer,
18895
card: Card,
18996
note: Note,
190-
template: ClozeTemplate,
97+
template: Template,
19198
short: boolean,
19299
) {
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
194105
if (short) {
195106
front = shortFront == null || shortFront.trim() === '' ? front : shortFront
196107
back = shortBack == null || shortBack.trim() === '' ? back : shortBack
@@ -208,14 +119,14 @@ function handleCloze(
208119
note,
209120
template,
210121
}
211-
for (const [, replacer] of this.clozeReplacers) {
122+
for (const [, replacer] of this.replacers) {
212123
args.initialValue = r
213124
r = replacer.bind(this)(args)
214125
}
215126
return r
216127
}
217128
const frontSide = replaceFields.call(this, true, front)
218-
if (frontSide === front) {
129+
if (frontSide === front || frontSide === '') {
219130
return null
220131
} else {
221132
const backSide = replaceFields
@@ -224,7 +135,6 @@ function handleCloze(
224135
return [frontSide, backSide] as const
225136
}
226137
}
227-
228138
function getClozeFields(
229139
this: RenderContainer,
230140
frontTemplate: string,
@@ -251,14 +161,10 @@ export function simpleFieldReplacer(
251161
}
252162

253163
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)
258164
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'),
262168
].filter(notEmpty)
263169
let r = args.initialValue
264170
const args2 = {
@@ -320,71 +226,74 @@ function stripHtmlReplacer(
320226

321227
function clozeReplacer(
322228
this: RenderContainer,
323-
{ initialValue, isFront, card, note, template }: ClozeReplacerArgs,
229+
{ initialValue, isFront, card, note, template }: ReplacerArgs,
324230
) {
325231
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 ??
345258
throwExp(
346259
'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) => [
364272
x.groups?.hint,
365273
x[0],
366274
])
367-
const bracketed = regexMatches.reduce((current, [hint, rawCloze]) => {
368-
const brackets = `
275+
const bracketed = regexMatches.reduce((current, [hint, rawCloze]) => {
276+
const brackets = `
369277
<span class="cloze-brackets-front">[</span>
370278
<span class="cloze-filler-front">${hint ?? '...'}</span>
371279
<span class="cloze-brackets-front">]</span>
372280
`
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+
`
380288
<span class="cloze-brackets-back">[</span>
381289
$<answer>
382290
<span class="cloze-brackets-back">]</span>
383291
`,
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+
}
388297
return r
389298
}
390299

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)