diff --git a/docs/changelog.md b/docs/changelog.md index 5b57e738..de58022d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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] diff --git a/src/parser.ts b/src/parser.ts index b1701089..aa366603 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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) { @@ -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("~~~"); +}