Skip to content

Commit

Permalink
Remove the spaces before and after the question marks in the multi-li…
Browse files Browse the repository at this point in the history
…ne basic flashcards (#719)

* Remove the spaces before and after the question marks in the multi-line basic flashcards

* Got CardType import back

* Fixed cardText trim to trimEnd method.

* Removed redundant tests
  • Loading branch information
matterai authored Aug 13, 2023
1 parent 1f69f96 commit 388dec9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,29 @@ export function parse(

const lines: string[] = text.replaceAll("\r\n", "\n").split("\n");
for (let i = 0; i < lines.length; i++) {
if (lines[i].length === 0) {
const currentLine = lines[i];
if (currentLine.length === 0) {
if (cardType) {
cards.push([cardType, cardText, lineNo]);
cardType = null;
}

cardText = "";
continue;
} else if (lines[i].startsWith("<!--") && !lines[i].startsWith("<!--SR:")) {
while (i + 1 < lines.length && !lines[i].includes("-->")) i++;
} else if (currentLine.startsWith("<!--") && !currentLine.startsWith("<!--SR:")) {
while (i + 1 < lines.length && !currentLine.includes("-->")) i++;
i++;
continue;
}

if (cardText.length > 0) {
cardText += "\n";
}
cardText += lines[i];
cardText += currentLine.trimEnd();

if (
lines[i].includes(singlelineReversedCardSeparator) ||
lines[i].includes(singlelineCardSeparator)
currentLine.includes(singlelineReversedCardSeparator) ||
currentLine.includes(singlelineCardSeparator)
) {
cardType = lines[i].includes(singlelineReversedCardSeparator)
? CardType.SingleLineReversed
Expand All @@ -64,20 +65,20 @@ export function parse(
cardText = "";
} else if (
cardType === null &&
((convertHighlightsToClozes && /==.*?==/gm.test(lines[i])) ||
(convertBoldTextToClozes && /\*\*.*?\*\*/gm.test(lines[i])) ||
(convertCurlyBracketsToClozes && /{{.*?}}/gm.test(lines[i])))
((convertHighlightsToClozes && /==.*?==/gm.test(currentLine)) ||
(convertBoldTextToClozes && /\*\*.*?\*\*/gm.test(currentLine)) ||
(convertCurlyBracketsToClozes && /{{.*?}}/gm.test(currentLine)))
) {
cardType = CardType.Cloze;
lineNo = i;
} else if (lines[i] === multilineCardSeparator) {
} else if (currentLine.trim() === multilineCardSeparator) {
cardType = CardType.MultiLineBasic;
lineNo = i;
} else if (lines[i] === multilineReversedCardSeparator) {
} else if (currentLine.trim() === multilineReversedCardSeparator) {
cardType = CardType.MultiLineReversed;
lineNo = i;
} else if (lines[i].startsWith("```") || lines[i].startsWith("~~~")) {
const codeBlockClose = lines[i].match(/`+|~+/)[0];
} else if (currentLine.startsWith("```") || currentLine.startsWith("~~~")) {
const codeBlockClose = currentLine.match(/`+|~+/)[0];
while (i + 1 < lines.length && !lines[i + 1].startsWith(codeBlockClose)) {
i++;
cardText += "\n" + lines[i];
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ test("Test parsing of multi line basic cards", () => {
expect(parse("Question\n?\nAnswer", ...defaultArgs)).toEqual([
[CardType.MultiLineBasic, "Question\n?\nAnswer", 1],
]);
expect(parse("Question\n? \nAnswer", ...defaultArgs)).toEqual([
[CardType.MultiLineBasic, "Question\n?\nAnswer", 1],
]);
expect(parse("Question\n?\nAnswer <!--SR:!2021-08-11,4,270-->", ...defaultArgs)).toEqual([
[CardType.MultiLineBasic, "Question\n?\nAnswer <!--SR:!2021-08-11,4,270-->", 1],
]);
Expand Down

0 comments on commit 388dec9

Please sign in to comment.