diff --git a/manifest.json b/manifest.json index 2cc2651..3458100 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "flashcards-obsidian", "name": "Flashcards", - "version": "1.5.5", + "version": "1.5.6", "minAppVersion": "0.9.17", "description": "Anki integration", "author": "Alex Colucci", diff --git a/src/entities/clozecard.ts b/src/entities/clozecard.ts new file mode 100644 index 0000000..493c90f --- /dev/null +++ b/src/entities/clozecard.ts @@ -0,0 +1,72 @@ +import { codeDeckExtension, sourceDeckExtension } from "src/constants"; +import { Card } from "src/entities/card"; + +export class Clozecard extends Card { + constructor( + id = -1, + deckName: string, + initialContent: string, + fields: Record, + reversed: boolean, + endOffset: number, + tags: string[] = [], + inserted = false, + mediaNames: string[], + containsCode: boolean + ) { + super( + id, + deckName, + initialContent, + fields, + reversed, + endOffset, + tags, + inserted, + mediaNames, + containsCode + ); + this.modelName = `Obsidian-clozed`; + if (fields["Source"]) { + this.modelName += sourceDeckExtension; + } + if (containsCode) { + this.modelName += codeDeckExtension; + } + } + + public getCard(update = false): object { + const card: any = { + deckName: this.deckName, + modelName: this.modelName, + fields: this.fields, + tags: this.tags, + }; + + if (update) { + card["id"] = this.id; + } + + return card; + } + + public getMedias(): object[] { + const medias: object[] = []; + this.mediaBase64Encoded.forEach((data, index) => { + medias.push({ + filename: this.mediaNames[index], + data: data, + }); + }); + + return medias; + } + + public toString = (): string => { + return `Cloze: ${this.fields[0]}`; + }; + + public getIdFormat(): string { + return "^" + this.id.toString() + "\n"; + } +} diff --git a/src/regex.ts b/src/regex.ts index 1b79447..d1ad679 100644 --- a/src/regex.ts +++ b/src/regex.ts @@ -5,6 +5,7 @@ export class Regex { wikiImageLinks: RegExp; markdownImageLinks: RegExp; wikiAudioLinks: RegExp; + obsidianCodeBlock: RegExp; codeBlock: RegExp; cardsDeckLine: RegExp; cardsToDelete: RegExp; @@ -13,6 +14,10 @@ export class Regex { flashscardsWithTag: RegExp; cardsInlineStyle: RegExp; cardsSpacedStyle: RegExp; + cardsClozeWholeLine: RegExp; + singleClozeCurly: RegExp; + singleClozeHighlight: RegExp; + clozeHighlight: RegExp; constructor(settings: ISettings) { this.update(settings); @@ -31,6 +36,9 @@ export class Regex { this.wikiAudioLinks = /!\[\[(.*\.(?:mp3|webm|wav|m4a|ogg|3gp|flac)).*?\]\]/gim; + // https://regex101.com/r/eqnJeW/1 + this.obsidianCodeBlock = /(?:```(?:.*?\n?)+?```)(?:\n|$)/gim; + this.codeBlock = /]*>(.*?)<\/code>/gims; this.cardsDeckLine = /cards-deck: [\p{L}]+/giu; @@ -70,5 +78,15 @@ export class Regex { settings.flashcardsTag + "[/-]spaced)((?: *#[\\p{Letter}-]+)*) *\\n?(?:\\^(\\d{13}))?"; this.cardsSpacedStyle = new RegExp(str, flags); + + // https://regex101.com/r/cgtnLf/1 + // str = "(?:.*?(==(.*?)==).*)(?:\n?^(d{13}))?"; + // this.clozeHighlight = /((==)(.*?)(==))/gm; + + // str = "( {0,3}[#]{0,6})?(?:(?:[\t ]*)(?:\\d.|[-+*]|#{1,6}))?(.*?(==.+?==|{.+?}).*?)((?: *#[\\w-]+)+|$)(?:\n\\^(?:\\d{13}))?" + // this.cardsClozeWholeLine = new RegExp(str, flags); + + // this.singleClozeCurly = /((?:{)(?:(\d):?)?(.+?)(?:}))/g; + // this.singleClozeHighlight = /((?:==)(.+?)(?:==))/g; } -} +} \ No newline at end of file diff --git a/src/services/parser.ts b/src/services/parser.ts index d50cc81..a14e095 100644 --- a/src/services/parser.ts +++ b/src/services/parser.ts @@ -4,7 +4,9 @@ import { Regex } from "src/regex"; import { Flashcard } from "../entities/flashcard"; import { Inlinecard } from "src/entities/inlinecard"; import { Spacedcard } from "src/entities/spacedcard"; +import { Clozecard } from "src/entities/clozecard"; import { escapeMarkdown } from "src/utils"; +import { Card } from "src/entities/card"; export class Parser { private regex: Regex; @@ -40,6 +42,11 @@ 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) @@ -50,6 +57,9 @@ export class Parser { cards = cards.concat( this.generateSpacedCards(file, headings, deck, vault, note, globalTags) ); + // cards = cards.concat( + // this.generateClozeCards(file, headings, deck, vault, note, globalTags) + // ); cards.sort((a, b) => a.endOffset - b.endOffset); const defaultAnkiTag = this.settings.defaultAnkiTag;