Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: picking up of inline reversed cards #1143

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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