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

Added support for flashcards within Admonition style code blocks #885

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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]

- Fixes [BUG] Malformed card text during review, when multi-line card has space on Q/A separator line https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/853
- Add integration with the ad-monition plug-in to create cards within certain code blocks [Issue `#747`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/747 ) [@DanielChicoUGR](https://github.com/DanielChicoUGR)

#### [Unreleased]

Expand Down
28 changes: 21 additions & 7 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function parse(
let lineNo = 0;

const lines: string[] = text.replaceAll("\r\n", "\n").split("\n");
let readingCodeBlock = false;
for (let i = 0; i < lines.length; i++) {
const currentLine = lines[i];
if (currentLine.length === 0) {
Expand Down Expand Up @@ -77,20 +78,33 @@ export function parse(
} else if (currentLine.trim() === multilineReversedCardSeparator) {
cardType = CardType.MultiLineReversed;
lineNo = i;
} else if (currentLine.startsWith("```") || currentLine.startsWith("~~~")) {
const codeBlockClose = currentLine.match(/`+|~+/)[0];
while (i + 1 < lines.length && !lines[i + 1].startsWith(codeBlockClose)) {
} else if (isCodeBlock(currentLine) && !isAdmonition(currentLine)) {
if (!readingCodeBlock) {
const codeBlockClose = currentLine.match(/`+|~+/)[0];
while (i + 1 < lines.length && !lines[i + 1].startsWith(codeBlockClose)) {
i++;
cardText += "\n" + lines[i];
}
cardText += "\n" + codeBlockClose;
i++;
cardText += "\n" + lines[i];
} else {
readingCodeBlock = false;
}
cardText += "\n" + codeBlockClose;
i++;
} else if (isAdmonition(currentLine)) {
readingCodeBlock = true;
}
}

// } else if (currentLine.startsWith("```") || currentLine.startsWith("~~~")) {
if (cardType && cardText) {
cards.push([cardType, cardText, lineNo]);
}

return cards;
}

function isAdmonition(text: string): boolean {
return text.startsWith("```ad-") || text.startsWith("~~~ad-");
}
function isCodeBlock(text: string): boolean {
return text.startsWith("```") || text.startsWith("~~~");
}
Loading