Skip to content

Commit

Permalink
fix: picking up of inline reversed cards (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw authored Oct 20, 2024
1 parent d5f54ed commit e313a51
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e313a51

Please sign in to comment.