Skip to content

Commit

Permalink
refactor: to reduce its Cognitive Complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 27, 2024
1 parent 1342a05 commit ab0c661
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions src/lib/parser/helpers/handleClozeDeletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,64 @@ import cheerio from 'cheerio';

import replaceAll from './replaceAll';

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

function handleKatexCloze(
content: string,
num: number,
isStandalone: boolean
): string {
const vReplaced = content.replace('KaTex:', '');
return isStandalone
? `{{c${num}::${vReplaced} }}`
: `{{c${num}::${vReplaced}}}`;
}

function handleRegularCloze(content: string, num: number): string {
return content.match(/c\d::/) ? `{{${content}}}` : `{{c${num}::${content}}}`;
}

export default function handleClozeDeletions(input: string) {
let num = findHighestClozeNumber(input);
const dom = cheerio.load(input);
const clozeDeletions = dom('code');
let mangle = input;

clozeDeletions.each((_i, elem) => {
const v = dom(elem).html();
if (!v) {
if (!v) return;

const old = `<code>${v}</code>`;

if (v.includes('KaTex')) {
const isStandalone = (mangle.match(/<code>/g) || []).length === 1;
mangle = replaceAll(
mangle,
old,
handleKatexCloze(v, num++, isStandalone)
);
return;
}
// Note: Does this handle the case where there cloze deletion is uppercase? C1
if (v.includes('{{c') && v.includes('}}') && !v.includes('KaTex')) {
// make Statement unreachable bc. even clozes can get such a formation
// eg: \frac{{c}} 1 would give that.
mangle = replaceAll(mangle, `<code>${v}</code>`, v);
} else if (!v.includes('KaTex') && v.match(/c\d::/)) {
// In the case user forgets the curly braces, add it for them
if (!v.includes('{{')) {
mangle = mangle.replace('<code>', '{{');
} else {
mangle = mangle.replace('<code>', '');
}
if (!v.endsWith('}}')) {
mangle = mangle.replace('</code>', '}}');
} else {
mangle = mangle.replace('</code>', '');
}
} else if (!v.includes('KaTex')) {
const old = `<code>${v}</code>`;
const newValue = v.match(/c\d::/) ? `{{${v}}}` : `{{c${num}::${v}}}`;
mangle = replaceAll(mangle, old, newValue);
num += 1;
} else {
const old = `<code>${v}</code>`;
// Remove 'KaTex:' from the content
const vReplaced = v.replace('KaTex:', '');
// Add space only for standalone KaTeX (when there's just one code block)
const isStandalone = (mangle.match(/<code>/g) || []).length === 1;
const newValue = isStandalone
? `{{c${num}::${vReplaced} }}`
: `{{c${num}::${vReplaced}}}`;
mangle = replaceAll(mangle, old, newValue);
num += 1;

if (v.includes('{{c') && v.includes('}}')) {
mangle = replaceAll(mangle, old, v);
return;
}

if (v.match(/c\d::/)) {
mangle = mangle.replace('<code>', v.includes('{{') ? '' : '{{');
mangle = mangle.replace('</code>', v.endsWith('}}') ? '' : '}}');
return;
}

mangle = replaceAll(mangle, old, handleRegularCloze(v, num++));
});

return mangle;
Expand Down

0 comments on commit ab0c661

Please sign in to comment.