@@ -2,61 +2,64 @@ import cheerio from 'cheerio';
2
2
3
3
import replaceAll from './replaceAll' ;
4
4
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 {
7
7
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
+ }
17
13
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 ) ;
18
31
const dom = cheerio . load ( input ) ;
19
32
const clozeDeletions = dom ( 'code' ) ;
20
33
let mangle = input ;
34
+
21
35
clozeDeletions . each ( ( _i , elem ) => {
22
36
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 ( / < c o d e > / g) || [ ] ) . length === 1 ;
43
+ mangle = replaceAll (
44
+ mangle ,
45
+ old ,
46
+ handleKatexCloze ( v , num ++ , isStandalone )
47
+ ) ;
24
48
return ;
25
49
}
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 ( / < c o d e > / 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 ;
59
54
}
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 ++ ) ) ;
60
63
} ) ;
61
64
62
65
return mangle ;
0 commit comments