Skip to content

Commit

Permalink
Add filtering out of cards inside code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Feb 4, 2022
1 parent fb29478 commit 2092d4c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/entities/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export abstract class Card {
initialContent: string;
fields: Record<string, string>;
reversed: boolean;
initialOffset: number;
endOffset: number;
tags: string[];
inserted: boolean;
Expand All @@ -22,6 +23,7 @@ export abstract class Card {
initialContent: string,
fields: Record<string, string>,
reversed: boolean,
initialOffset: number,
endOffset: number,
tags: string[],
inserted: boolean,
Expand All @@ -33,6 +35,7 @@ export abstract class Card {
this.initialContent = initialContent;
this.fields = fields;
this.reversed = reversed;
this.initialOffset = initialOffset
this.endOffset = endOffset;
this.tags = tags;
this.inserted = inserted;
Expand Down
2 changes: 2 additions & 0 deletions src/entities/clozecard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Clozecard extends Card {
initialContent: string,
fields: Record<string, string>,
reversed: boolean,
initialOffset: number,
endOffset: number,
tags: string[] = [],
inserted = false,
Expand All @@ -20,6 +21,7 @@ export class Clozecard extends Card {
initialContent,
fields,
reversed,
initialOffset,
endOffset,
tags,
inserted,
Expand Down
2 changes: 2 additions & 0 deletions src/entities/flashcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Flashcard extends Card {
initialContent: string,
fields: Record<string, string>,
reversed: boolean,
initialOffset: number,
endOffset: number,
tags: string[] = [],
inserted = false,
Expand All @@ -20,6 +21,7 @@ export class Flashcard extends Card {
initialContent,
fields,
reversed,
initialOffset,
endOffset,
tags,
inserted,
Expand Down
2 changes: 2 additions & 0 deletions src/entities/inlinecard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Inlinecard extends Card {
initialContent: string,
fields: Record<string, string>,
reversed: boolean,
initialOffset: number,
endOffset: number,
tags: string[] = [],
inserted = false,
Expand All @@ -20,6 +21,7 @@ export class Inlinecard extends Card {
initialContent,
fields,
reversed,
initialOffset,
endOffset,
tags,
inserted,
Expand Down
2 changes: 2 additions & 0 deletions src/entities/spacedcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Spacedcard extends Card {
initialContent: string,
fields: Record<string, string>,
reversed: boolean,
initialOffset: number,
endOffset: number,
tags: string[] = [],
inserted = false,
Expand All @@ -20,6 +21,7 @@ export class Spacedcard extends Card {
initialContent,
fields,
reversed,
initialOffset,
endOffset,
tags,
inserted,
Expand Down
28 changes: 22 additions & 6 deletions src/services/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ export class Parser {
headings = [...file.matchAll(this.regex.headingsRegex)];
}

// filter in cacheCodeSections only the objects with type "code"
// the line is 0-indexed
// const codeSections = this.app.metadataCache.getFileCache(this.app.workspace.getActiveFile()).sections.filter(section => section.type === "code")


note = this.substituteObsidianLinks(`[[${note}]]`, vault);
cards = cards.concat(
this.generateCardsWithTag(file, headings, deck, vault, note, globalTags)
Expand All @@ -57,9 +52,24 @@ export class Parser {
cards = cards.concat(
this.generateSpacedCards(file, headings, deck, vault, note, globalTags)
);
// cards = cards.concat(
// cards = cards.concat(
// this.generateClozeCards(file, headings, deck, vault, note, globalTags)
// );


// Filter out cards that are fully inside a code block
const codeBlocks = [...file.matchAll(this.regex.obsidianCodeBlock)];
const rangesToDiscard = codeBlocks.map(x=>([x.index, x.index+x[0].length]))
cards = cards.filter(card => {
const cardRange = [card.initialOffset, card.endOffset];
const isInRangeToDiscard = rangesToDiscard.some(range => {
return (
cardRange[0] >= range[0] && cardRange[1] <= range[1]
);
});
return !isInRangeToDiscard;
});

cards.sort((a, b) => a.endOffset - b.endOffset);

const defaultAnkiTag = this.settings.defaultAnkiTag;
Expand Down Expand Up @@ -154,6 +164,7 @@ export class Parser {
medias = medias.concat(this.getAudioLinks(prompt));
prompt = this.parseLine(prompt, vault);

const initialOffset = match.index;
const endingLine = match.index + match[0].length;
const tags: string[] = this.parseTags(match[4], globalTags);
const id: number = match[5] ? Number(match[5]) : -1;
Expand All @@ -170,6 +181,7 @@ export class Parser {
originalPrompt,
fields,
reversed,
initialOffset,
endingLine,
tags,
inserted,
Expand Down Expand Up @@ -226,6 +238,7 @@ export class Parser {
question = this.parseLine(question, vault);
answer = this.parseLine(answer, vault);

const initialOffset = match.index
const endingLine = match.index + match[0].length;
const tags: string[] = this.parseTags(match[5], globalTags);
const id: number = match[6] ? Number(match[6]) : -1;
Expand All @@ -242,6 +255,7 @@ export class Parser {
originalQuestion,
fields,
reversed,
initialOffset,
endingLine,
tags,
inserted,
Expand Down Expand Up @@ -291,6 +305,7 @@ export class Parser {
question = this.parseLine(question, vault);
answer = this.parseLine(answer, vault);

const initialOffset = match.index
const endingLine = match.index + match[0].length;
const tags: string[] = this.parseTags(match[4], globalTags);
const id: number = match[6] ? Number(match[6]) : -1;
Expand All @@ -307,6 +322,7 @@ export class Parser {
originalQuestion,
fields,
reversed,
initialOffset,
endingLine,
tags,
inserted,
Expand Down

0 comments on commit 2092d4c

Please sign in to comment.