From 4f15473f285b49a0833ed816f6e269c808e32c82 Mon Sep 17 00:00:00 2001 From: Voctor Date: Mon, 10 Oct 2022 02:07:58 -0700 Subject: [PATCH] add distinctive highlight-based clozing based on https://github.com/reuseman/flashcards-obsidian/issues/118 --- src/services/parser.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/services/parser.ts b/src/services/parser.ts index d137d86..6e214b0 100644 --- a/src/services/parser.ts +++ b/src/services/parser.ts @@ -245,7 +245,21 @@ export class Parser { }); // Replace the highlight clozes in the line with Anki syntax - clozeText = clozeText.replace(this.regex.singleClozeHighlight, "{{c1::$2}}"); + // increment c1, c2, c3, etc. as regex greedily replaces for every match in the regex + // example: clozeText = clozeText.replace(this.regex.singleClozeHighlight, "{{c1::$2}}"); + + let clozeNumber = 1; + // loop through all the clozes in the line + for (const cloze of clozeText.matchAll(this.regex.singleClozeHighlight)) { + // if the cloze is not in a math block, then replace it with Anki syntax + const globalOffset = matchIndex + cloze.index; + const isInMathBlock = rangesToDiscard.some(x => (globalOffset >= x[0] && globalOffset + cloze[0].length <= x[1])); + if (!isInMathBlock) { + clozeText = clozeText.replace(cloze[0], `{{c${clozeNumber}::${cloze[2]}}}`); + clozeNumber++; + } + } + if (clozeText === match[2]) { // If the clozeText is the same as the match it means that the curly clozes were all in math blocks