Skip to content

Commit ab0c661

Browse files
committed
refactor: to reduce its Cognitive Complexity
1 parent 1342a05 commit ab0c661

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

src/lib/parser/helpers/handleClozeDeletions.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,64 @@ import cheerio from 'cheerio';
22

33
import replaceAll from './replaceAll';
44

5-
export default function handleClozeDeletions(input: string) {
6-
// Find the highest existing cloze number or default to 0
5+
// Helper functions first
6+
function findHighestClozeNumber(input: string): number {
77
const clozeRegex = /c(\d+)::/g;
8-
let num = 1;
9-
let match;
10-
const numbers = [];
11-
while ((match = clozeRegex.exec(input)) !== null) {
12-
numbers.push(parseInt(match[1]));
13-
}
14-
if (numbers.length > 0) {
15-
num = Math.max(...numbers) + 1;
16-
}
8+
const numbers = Array.from(input.matchAll(clozeRegex)).map((match) =>
9+
parseInt(match[1])
10+
);
11+
return numbers.length > 0 ? Math.max(...numbers) + 1 : 1;
12+
}
1713

14+
function handleKatexCloze(
15+
content: string,
16+
num: number,
17+
isStandalone: boolean
18+
): string {
19+
const vReplaced = content.replace('KaTex:', '');
20+
return isStandalone
21+
? `{{c${num}::${vReplaced} }}`
22+
: `{{c${num}::${vReplaced}}}`;
23+
}
24+
25+
function handleRegularCloze(content: string, num: number): string {
26+
return content.match(/c\d::/) ? `{{${content}}}` : `{{c${num}::${content}}}`;
27+
}
28+
29+
export default function handleClozeDeletions(input: string) {
30+
let num = findHighestClozeNumber(input);
1831
const dom = cheerio.load(input);
1932
const clozeDeletions = dom('code');
2033
let mangle = input;
34+
2135
clozeDeletions.each((_i, elem) => {
2236
const v = dom(elem).html();
23-
if (!v) {
37+
if (!v) return;
38+
39+
const old = `<code>${v}</code>`;
40+
41+
if (v.includes('KaTex')) {
42+
const isStandalone = (mangle.match(/<code>/g) || []).length === 1;
43+
mangle = replaceAll(
44+
mangle,
45+
old,
46+
handleKatexCloze(v, num++, isStandalone)
47+
);
2448
return;
2549
}
26-
// Note: Does this handle the case where there cloze deletion is uppercase? C1
27-
if (v.includes('{{c') && v.includes('}}') && !v.includes('KaTex')) {
28-
// make Statement unreachable bc. even clozes can get such a formation
29-
// eg: \frac{{c}} 1 would give that.
30-
mangle = replaceAll(mangle, `<code>${v}</code>`, v);
31-
} else if (!v.includes('KaTex') && v.match(/c\d::/)) {
32-
// In the case user forgets the curly braces, add it for them
33-
if (!v.includes('{{')) {
34-
mangle = mangle.replace('<code>', '{{');
35-
} else {
36-
mangle = mangle.replace('<code>', '');
37-
}
38-
if (!v.endsWith('}}')) {
39-
mangle = mangle.replace('</code>', '}}');
40-
} else {
41-
mangle = mangle.replace('</code>', '');
42-
}
43-
} else if (!v.includes('KaTex')) {
44-
const old = `<code>${v}</code>`;
45-
const newValue = v.match(/c\d::/) ? `{{${v}}}` : `{{c${num}::${v}}}`;
46-
mangle = replaceAll(mangle, old, newValue);
47-
num += 1;
48-
} else {
49-
const old = `<code>${v}</code>`;
50-
// Remove 'KaTex:' from the content
51-
const vReplaced = v.replace('KaTex:', '');
52-
// Add space only for standalone KaTeX (when there's just one code block)
53-
const isStandalone = (mangle.match(/<code>/g) || []).length === 1;
54-
const newValue = isStandalone
55-
? `{{c${num}::${vReplaced} }}`
56-
: `{{c${num}::${vReplaced}}}`;
57-
mangle = replaceAll(mangle, old, newValue);
58-
num += 1;
50+
51+
if (v.includes('{{c') && v.includes('}}')) {
52+
mangle = replaceAll(mangle, old, v);
53+
return;
5954
}
55+
56+
if (v.match(/c\d::/)) {
57+
mangle = mangle.replace('<code>', v.includes('{{') ? '' : '{{');
58+
mangle = mangle.replace('</code>', v.endsWith('}}') ? '' : '}}');
59+
return;
60+
}
61+
62+
mangle = replaceAll(mangle, old, handleRegularCloze(v, num++));
6063
});
6164

6265
return mangle;

0 commit comments

Comments
 (0)