Skip to content

Commit df8b91b

Browse files
Handle newlines in cards
1 parent 23cf334 commit df8b91b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/main.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AnkiLinkSettingsTab,
66
} from "./settings";
77
import { TARGET_DECK, sendAddNoteRequest, buildNote, sendCreateDeckRequest, sendDeckNamesRequest, ConnNoteFields } from "./ankiConnectUtil";
8+
import { FLASHCARD_PATTERN, splitCalloutBody } from "./regexUtil";
89

910
export default class AnkiLink extends Plugin {
1011
settings!: AnkiLinkSettings;
@@ -58,10 +59,8 @@ export default class AnkiLink extends Plugin {
5859
const fileContents = await Promise.all(
5960
vault.getMarkdownFiles().map((file) => vault.read(file)),
6061
);
61-
const pattern: RegExp =
62-
/\[!flashcard\]\s*([^\n]*)\n((?:>(?:[^\n]*)\n?)*)/;
6362
fileContents.forEach((c) => {
64-
const values = pattern.exec(c);
63+
const values = FLASHCARD_PATTERN.exec(c);
6564
values?.shift();
6665
});
6766
const deckNamesRes = await sendDeckNamesRequest();
@@ -72,11 +71,14 @@ export default class AnkiLink extends Plugin {
7271
if (createDeckRes.error) throw new Error(`AnkiConnect: ${createDeckRes.error}`)
7372
}
7473
const cards = fileContents.reduce<ConnNoteFields[]>((acc, s) => {
75-
const matches = pattern.exec(s);
74+
const matches = FLASHCARD_PATTERN.exec(s);
7675
if (!matches || matches.length < 3) {
7776
return acc;
7877
}
79-
acc.push({ Front: matches[1]!, Back: matches[2]! });
78+
const title = matches[1]!;
79+
const body = matches[2]!;
80+
const back = splitCalloutBody(body);
81+
acc.push({ Front: title, Back: back });
8082
return acc;
8183
}, []);
8284
console.log(cards)

src/regexUtil.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
export const FLASHCARD_PATTERN: RegExp = /\[!flashcard\]\s*([^\n]*)\n((?:>(?:[^\n]*)\n?)*)/;
3+
4+
export function splitCalloutBody(body: string) {
5+
const lines = body.split(">");
6+
return lines.join("<br>");
7+
}

0 commit comments

Comments
 (0)