Repository: st3v3nmw/obsidian-spaced-repetition
Pull request #1143 (2024-10-20 09:36:28)

fix: picking up of inline reversed cards
Fix issue reported in #1132.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/st3v3nmw/obsidian-spaced-repetition/pull/1143?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Stephen+Mwangi) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 93.00%. Comparing base [(`d5f54ed`)](https://app.codecov.io/gh/st3v3nmw/obsidian-spaced-repetition/commit/d5f54ed4975d250061077ccb98441b75dc73b9a3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Stephen+Mwangi) to head [(`58a1436`)](https://app.codecov.io/gh/st3v3nmw/obsidian-spaced-repetition/commit/58a1436de15743c271233e0ed235fda3a79e601f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Stephen+Mwangi).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #1143      +/-   ##
==========================================
- Coverage   93.09%   93.00%   -0.10%     
==========================================
  Files          42       42              
  Lines        4564     4558       -6     
  Branches      376      648     +272     
==========================================
- Hits         4249     4239      -10     
  Misses        313      313              
- Partials        2        6       +4     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/st3v3nmw/obsidian-spaced-repetition/pull/1143?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Stephen+Mwangi).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Stephen+Mwangi).


Patch:
diff --git a/src/parser.ts b/src/parser.ts
index c748bf9..326a841 100644
--- a/src/parser.ts
+++ b/src/parser.ts
@@ -62,19 +62,10 @@ function hasInlineMarker(text: string, marker: string): boolean {
     // No marker provided
     if (marker.length == 0) return false;
 
-    // Make sure it's an "exact" match
-    // For instance:
-    //  hasInlineMarker("a:::b", "::") -> false
-    //  hasInlineMarker("a::b", "::") -> true
+    // Check if the marker is in the text
     const markerIdx = text.indexOf(marker);
     if (markerIdx === -1) return false;
 
-    const prevChar = markerIdx > 0 ? text[markerIdx - 1] : null;
-    const nextChar =
-        markerIdx + marker.length < text.length ? text[markerIdx + marker.length] : null;
-    const markerMatchesExactly = prevChar !== marker[0] && nextChar !== marker[0];
-    if (!markerMatchesExactly) return false;
-
     // Check if it's inside an inline code block
     return !markerInsideCodeBlock(text, marker, markerIdx);
 }
@@ -93,6 +84,13 @@ export function parse(text: string, options: ParserOptions): ParsedQuestionInfo[
         console.log("Text to parse:\n<<<" + text + ">>>");
     }
 
+    // Sort inline separators by length, longest first
+    const inlineSeparators = [
+        { separator: options.singleLineCardSeparator, type: CardType.SingleLineBasic },
+        { separator: options.singleLineReversedCardSeparator, type: CardType.SingleLineReversed },
+    ];
+    inlineSeparators.sort((a, b) => b.separator.length - a.separator.length);
+
     const cards: ParsedQuestionInfo[] = [];
     let cardText = "";
     let cardType: CardType | null = null;
@@ -146,18 +144,14 @@ export function parse(text: string, options: ParserOptions): ParsedQuestionInfo[
         cardText += currentLine.trimEnd();
 
         // Pick up inline cards
-        const hasSingleLineCardSeparator: boolean = hasInlineMarker(
-            currentLine,
-            options.singleLineCardSeparator,
-        );
-        if (
-            hasSingleLineCardSeparator ||
-            // Has single line reversed card separator
-            hasInlineMarker(currentLine, options.singleLineReversedCardSeparator)
-        ) {
-            cardType = hasSingleLineCardSeparator
-                ? CardType.SingleLineBasic
-                : CardType.SingleLineReversed;
+        for (const { separator, type } of inlineSeparators) {
+            if (hasInlineMarker(currentLine, separator)) {
+                cardType = type;
+                break;
+            }
+        }
+
+        if (cardType == CardType.SingleLineBasic || cardType == CardType.SingleLineReversed) {
             cardText = currentLine;
             firstLineNo = i;
 


Test patch:
diff --git a/tests/unit/parser.test.ts b/tests/unit/parser.test.ts
index 17dbaab..be286bd 100644
--- a/tests/unit/parser.test.ts
+++ b/tests/unit/parser.test.ts
@@ -121,6 +121,19 @@ test("Test parsing of single line reversed cards", () => {
             clozePatterns: [],
         }),
     ).toEqual([[CardType.SingleLineReversed, "Question::Answer", 0, 0]]);
+    expect(
+        parseT("Qn 1?:>Answer.\n\nQn 2?<:>Answer.\n", {
+            singleLineCardSeparator: ":>",
+            singleLineReversedCardSeparator: "<:>",
+            multilineCardSeparator: ";>",
+            multilineReversedCardSeparator: "<;>",
+            multilineCardEndMarker: "---",
+            clozePatterns: [],
+        }),
+    ).toEqual([
+        [CardType.SingleLineBasic, "Qn 1?:>Answer.", 0, 0],
+        [CardType.SingleLineReversed, "Qn 2?<:>Answer.", 2, 2],
+    ]);
 
     // empty string or whitespace character provided
     expect(