Fix broken update #76

This commit is contained in:
alexcolucci@protonmail.com 2022-02-04 14:30:05 +01:00
parent 907b844f44
commit 0c2d5f61c4
4 changed files with 102 additions and 2 deletions

View File

@ -1,7 +1,7 @@
{ {
"id": "flashcards-obsidian", "id": "flashcards-obsidian",
"name": "Flashcards", "name": "Flashcards",
"version": "1.5.5", "version": "1.5.6",
"minAppVersion": "0.9.17", "minAppVersion": "0.9.17",
"description": "Anki integration", "description": "Anki integration",
"author": "Alex Colucci", "author": "Alex Colucci",

72
src/entities/clozecard.ts Normal file
View File

@ -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<string, string>,
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";
}
}

View File

@ -5,6 +5,7 @@ export class Regex {
wikiImageLinks: RegExp; wikiImageLinks: RegExp;
markdownImageLinks: RegExp; markdownImageLinks: RegExp;
wikiAudioLinks: RegExp; wikiAudioLinks: RegExp;
obsidianCodeBlock: RegExp;
codeBlock: RegExp; codeBlock: RegExp;
cardsDeckLine: RegExp; cardsDeckLine: RegExp;
cardsToDelete: RegExp; cardsToDelete: RegExp;
@ -13,6 +14,10 @@ export class Regex {
flashscardsWithTag: RegExp; flashscardsWithTag: RegExp;
cardsInlineStyle: RegExp; cardsInlineStyle: RegExp;
cardsSpacedStyle: RegExp; cardsSpacedStyle: RegExp;
cardsClozeWholeLine: RegExp;
singleClozeCurly: RegExp;
singleClozeHighlight: RegExp;
clozeHighlight: RegExp;
constructor(settings: ISettings) { constructor(settings: ISettings) {
this.update(settings); this.update(settings);
@ -31,6 +36,9 @@ export class Regex {
this.wikiAudioLinks = this.wikiAudioLinks =
/!\[\[(.*\.(?:mp3|webm|wav|m4a|ogg|3gp|flac)).*?\]\]/gim; /!\[\[(.*\.(?:mp3|webm|wav|m4a|ogg|3gp|flac)).*?\]\]/gim;
// https://regex101.com/r/eqnJeW/1
this.obsidianCodeBlock = /(?:```(?:.*?\n?)+?```)(?:\n|$)/gim;
this.codeBlock = /<code\b[^>]*>(.*?)<\/code>/gims; this.codeBlock = /<code\b[^>]*>(.*?)<\/code>/gims;
this.cardsDeckLine = /cards-deck: [\p{L}]+/giu; this.cardsDeckLine = /cards-deck: [\p{L}]+/giu;
@ -70,5 +78,15 @@ export class Regex {
settings.flashcardsTag + settings.flashcardsTag +
"[/-]spaced)((?: *#[\\p{Letter}-]+)*) *\\n?(?:\\^(\\d{13}))?"; "[/-]spaced)((?: *#[\\p{Letter}-]+)*) *\\n?(?:\\^(\\d{13}))?";
this.cardsSpacedStyle = new RegExp(str, flags); 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;
} }
} }

View File

@ -4,7 +4,9 @@ import { Regex } from "src/regex";
import { Flashcard } from "../entities/flashcard"; import { Flashcard } from "../entities/flashcard";
import { Inlinecard } from "src/entities/inlinecard"; import { Inlinecard } from "src/entities/inlinecard";
import { Spacedcard } from "src/entities/spacedcard"; import { Spacedcard } from "src/entities/spacedcard";
import { Clozecard } from "src/entities/clozecard";
import { escapeMarkdown } from "src/utils"; import { escapeMarkdown } from "src/utils";
import { Card } from "src/entities/card";
export class Parser { export class Parser {
private regex: Regex; private regex: Regex;
@ -40,6 +42,11 @@ export class Parser {
headings = [...file.matchAll(this.regex.headingsRegex)]; 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); note = this.substituteObsidianLinks(`[[${note}]]`, vault);
cards = cards.concat( cards = cards.concat(
this.generateCardsWithTag(file, headings, deck, vault, note, globalTags) this.generateCardsWithTag(file, headings, deck, vault, note, globalTags)
@ -50,6 +57,9 @@ export class Parser {
cards = cards.concat( cards = cards.concat(
this.generateSpacedCards(file, headings, deck, vault, note, globalTags) 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); cards.sort((a, b) => a.endOffset - b.endOffset);
const defaultAnkiTag = this.settings.defaultAnkiTag; const defaultAnkiTag = this.settings.defaultAnkiTag;