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

Fixing missing cards #701

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ venv/

# env
env.sh

main.js
2 changes: 1 addition & 1 deletion CHANGELOG.md
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased]

- [Changed] Parser now accepts white spaces before and after multiline separator #690
- [Changed] Parse flashcards without separates as a new CardType.Note #685
- [Style] Added comments explaning various sections of the code

#### [1.10.1](https://github.com/st3v3nmw/obsidian-spaced-repetition/compare/1.10.0...1.10.1)

- style: Fix formatting [`#678`](https://github.com/st3v3nmw/obsidian-spaced-repetition/pull/678)
Expand Down
12,242 changes: 12,242 additions & 0 deletions package-lock.json
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use pnpm instead

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"auto-changelog": "^2.4.0",
"builtin-modules": "^3.3.0",
"chai": "^4.3.7",
"esbuild": "~0.17.18",
"esbuild": "^0.17.19",
"eslint": "^8.40.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
Expand Down
2 changes: 2 additions & 0 deletions src/flashcard-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ export class Deck {
}
this.totalFlashcards++;

// deckPath.length === 0 means we have an individual card
// if the length is not 0, then we must recurse to get an individual card
if (deckPath.length === 0) {
if (cardObj.isDue) {
this.dueFlashcards.push(cardObj);
Expand Down
97 changes: 72 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import {
FrontMatterCache,
getAllTags,
HeadingCache,
Notice,
Plugin,
TAbstractFile,
TFile,
HeadingCache,
getAllTags,
FrontMatterCache,
} from "obsidian";
import * as graph from "pagerank.js";

import { SRSettingTab, SRSettings, DEFAULT_SETTINGS } from "src/settings";
import { FlashcardModal, Deck } from "src/flashcard-modal";
import { StatsModal, Stats } from "src/stats-modal";
import { ReviewQueueListView, REVIEW_QUEUE_VIEW_TYPE } from "src/sidebar";
import { DEFAULT_SETTINGS, SRSettings, SRSettingTab } from "src/settings";
import { Deck, FlashcardModal } from "src/flashcard-modal";
import { Stats, StatsModal } from "src/stats-modal";
import { REVIEW_QUEUE_VIEW_TYPE, ReviewQueueListView } from "src/sidebar";
import { Card, CardType, ReviewResponse, schedule } from "src/scheduling";
import {
YAML_FRONT_MATTER_REGEX,
SCHEDULING_INFO_REGEX,
LEGACY_SCHEDULING_EXTRACTOR,
MULTI_SCHEDULING_EXTRACTOR,
SCHEDULING_INFO_REGEX,
YAML_FRONT_MATTER_REGEX,
} from "src/constants";
import { escapeRegexString, cyrb53 } from "src/utils";
import { cyrb53, escapeRegexString } from "src/utils";
import { ReviewDeck, ReviewDeckSelectionModal } from "src/review-deck";
import { t } from "src/lang/helpers";
import { parse } from "src/parser";
import { appIcon } from "src/icons/appicon";
import { escapeSeparator } from "src/parser";

interface PluginData {
settings: SRSettings;
Expand Down Expand Up @@ -283,6 +284,18 @@ export default class SRPlugin extends Plugin {
}

const notes: TFile[] = this.app.vault.getMarkdownFiles();

// for debugging. count number of notes that satisfy paths
// let noteCount = 0;
// for (const note of notes) {
// const deckPath: string[] = this.findDeckPath(note);
// if (deckPath.length !== 0) {
// noteCount++;
// }
// }
// console.log(`Expected ${noteCount} flashcards. Because OSR avoids double counting when multiple tags are present
// for a card, the number of cards that show up under your Obsidian tags may be different.`);

for (const note of notes) {
if (
this.data.settings.noteFoldersToIgnore.some((folder) =>
Expand Down Expand Up @@ -313,6 +326,8 @@ export default class SRPlugin extends Plugin {
}

const deckPath: string[] = this.findDeckPath(note);

// if deckPath.length == 0, that means we did not find the flashcardTags in the note path
if (deckPath.length !== 0) {
const flashcardsInNoteAvgEase: number = await this.findFlashcardsInNote(
note,
Expand Down Expand Up @@ -605,6 +620,11 @@ export default class SRPlugin extends Plugin {
new Notice(t("ALL_CAUGHT_UP"));
}

/**
* Checks whether a single note path contains the flashcardTags (i.e. #flashcards). If not, returns empty string,
* and the note will not be added to the deckTree or considered for parsing.
* @param note
*/
findDeckPath(note: TFile): string[] {
let deckPath: string[] = [];
if (this.data.settings.convertFoldersToDecks) {
Expand All @@ -630,6 +650,10 @@ export default class SRPlugin extends Plugin {
return deckPath;
}

/**
* Parses note for flashcard(s), and also adds it to the deckTree.
* Keep in mind this function only operates on one note.
* */
async findFlashcardsInNote(
note: TFile,
deckPath: string[],
Expand Down Expand Up @@ -737,35 +761,58 @@ export default class SRPlugin extends Plugin {
siblingMatches.push([front, back]);
}
} else {
let idx: number;
let frontBackSplitStartIdx: number;
if (cardType === CardType.SingleLineBasic) {
idx = cardText.indexOf(settings.singleLineCardSeparator);
frontBackSplitStartIdx = cardText.indexOf(settings.singleLineCardSeparator);
siblingMatches.push([
cardText.substring(0, idx),
cardText.substring(idx + settings.singleLineCardSeparator.length),
cardText.substring(0, frontBackSplitStartIdx),
cardText.substring(
frontBackSplitStartIdx + settings.singleLineCardSeparator.length
),
]);
} else if (cardType === CardType.SingleLineReversed) {
idx = cardText.indexOf(settings.singleLineReversedCardSeparator);
const side1: string = cardText.substring(0, idx),
frontBackSplitStartIdx = cardText.indexOf(
settings.singleLineReversedCardSeparator
);
const side1: string = cardText.substring(0, frontBackSplitStartIdx),
side2: string = cardText.substring(
idx + settings.singleLineReversedCardSeparator.length
frontBackSplitStartIdx + settings.singleLineReversedCardSeparator.length
);
siblingMatches.push([side1, side2]);
siblingMatches.push([side2, side1]);
} else if (cardType === CardType.MultiLineBasic) {
idx = cardText.indexOf("\n" + settings.multilineCardSeparator + "\n");
const escapedSeparator = escapeSeparator(settings.multilineCardSeparator);
const regexPattern = `(\\r\n|\\r|\\n)[ ]*${escapedSeparator}[ ]*(\\n)`;

frontBackSplitStartIdx = cardText.search(regexPattern);

const substrLength = cardText.match(regexPattern)[0].length;

siblingMatches.push([
cardText.substring(0, idx),
cardText.substring(idx + 2 + settings.multilineCardSeparator.length),
cardText.substring(0, frontBackSplitStartIdx),
cardText.substring(frontBackSplitStartIdx + substrLength),
]);
} else if (cardType === CardType.MultiLineReversed) {
idx = cardText.indexOf("\n" + settings.multilineReversedCardSeparator + "\n");
const side1: string = cardText.substring(0, idx),
side2: string = cardText.substring(
idx + 2 + settings.multilineReversedCardSeparator.length
);
const escapedSeparator = escapeSeparator(
settings.multilineReversedCardSeparator
);
const regexPattern = `(\\r\n|\\r|\\n)[ ]*${escapedSeparator}[ ]*(\\n)`;

frontBackSplitStartIdx = cardText.search(regexPattern);

const substrLength = cardText.match(regexPattern)[0].length;

const side1: string = cardText.substring(0, frontBackSplitStartIdx),
side2: string = cardText.substring(frontBackSplitStartIdx + substrLength);
siblingMatches.push([side1, side2]);
siblingMatches.push([side2, side1]);
} else if (cardType == CardType.Note) {
// for note type, we have no card back - so put empty string
const side1: string = cardText;
const side2 = "";
siblingMatches.push([side1, side2]);
} else {
console.log("Card type not found!");
}
}

Expand Down
55 changes: 36 additions & 19 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { CardType } from "src/scheduling";

export function escapeSeparator(separator: string): string {
return separator.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function containsOnlySpacesAndOneSeparator(text: string, separator: string): boolean {
// Remove any leading or trailing spaces
text = text.trim();

// Check if the text consists of only spaces and exactly one question mark
const escapedSeparator = escapeSeparator(separator);

const regexPattern = `^[ ]*${escapedSeparator}[ ]*$`;

// console.log(regexPattern);

const regex = new RegExp(regexPattern);
return regex.test(text);
}

/**
* Returns flashcards found in `text`
*
Expand All @@ -23,19 +41,13 @@ export function parse(
let cardText = "";
const cards: [CardType, string, number][] = [];
let cardType: CardType | null = null;
let lineNo = 0;
let separatorLineNo = 0;

// console.log("parser working...");

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

cardText = "";
continue;
} else if (lines[i].startsWith("<!--") && !lines[i].startsWith("<!--SR:")) {
if (lines[i].startsWith("<!--") && !lines[i].startsWith("<!--SR:")) {
while (i + 1 < lines.length && !lines[i].includes("-->")) i++;
i++;
continue;
Expand All @@ -44,6 +56,8 @@ export function parse(
if (cardText.length > 0) {
cardText += "\n";
}

// this is the key line that builds the cardText
cardText += lines[i];

if (
Expand All @@ -54,12 +68,12 @@ export function parse(
? CardType.SingleLineReversed
: CardType.SingleLineBasic;
cardText = lines[i];
lineNo = i;
separatorLineNo = i;
if (i + 1 < lines.length && lines[i + 1].startsWith("<!--SR:")) {
cardText += "\n" + lines[i + 1];
i++;
}
cards.push([cardType, cardText, lineNo]);
cards.push([cardType, cardText, separatorLineNo]);
cardType = null;
cardText = "";
} else if (
Expand All @@ -69,13 +83,13 @@ export function parse(
(convertCurlyBracketsToClozes && /{{.*?}}/gm.test(lines[i])))
) {
cardType = CardType.Cloze;
lineNo = i;
} else if (lines[i] === multilineCardSeparator) {
separatorLineNo = i;
} else if (containsOnlySpacesAndOneSeparator(lines[i], multilineCardSeparator)) {
cardType = CardType.MultiLineBasic;
lineNo = i;
} else if (lines[i] === multilineReversedCardSeparator) {
separatorLineNo = i;
} else if (containsOnlySpacesAndOneSeparator(lines[i], multilineReversedCardSeparator)) {
cardType = CardType.MultiLineReversed;
lineNo = i;
separatorLineNo = i;
} else if (lines[i].startsWith("```") || lines[i].startsWith("~~~")) {
const codeBlockClose = lines[i].match(/`+|~+/)[0];
while (i + 1 < lines.length && !lines[i + 1].startsWith(codeBlockClose)) {
Expand All @@ -87,9 +101,12 @@ export function parse(
}
}

if (cardType && cardText) {
cards.push([cardType, cardText, lineNo]);
// add the last card
if (cardType === null) {
// we always want to create cards, so if no cardtype is found, make the cardtype note
cardType = CardType.Note;
}
cards.push([cardType, cardText, separatorLineNo]);

return cards;
}
1 change: 1 addition & 0 deletions src/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum CardType {
MultiLineBasic,
MultiLineReversed,
Cloze,
Note,
}

export function schedule(
Expand Down