Skip to content

Commit

Permalink
optimized path for primary use-case, simplified logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vavdoshka committed Dec 13, 2022
1 parent 0a93e5b commit 5f5813b
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/models/MarkdownFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,30 @@ export class MarkdownFile {


private extractMarkedText(content: string): string {
// extract the text between markers - markdown comments <!-- BEGIN_ANKI_CARDS --> text <!-- END_ANKI_CARDS -->
// if the markers are not present then return the entire content
// Extract the text between markers - markdown comments <!-- BEGIN_ANKI_CARDS --> text <!-- END_ANKI_CARDS -->
// Do a qucik test to see if the markers are present, otherwise return the whole content

const beginMarker = "<!-- BEGIN_ANKI_CARDS -->";
const endMarker = "<!-- END_ANKI_CARDS -->";

const begin = content.indexOf(beginMarker);
const end = content.indexOf(endMarker);

// test if least one full pair of markets is present
if (begin === -1 || end === -1) {
// but if there is only one marker, show an error message
if (begin !== -1 || end !== -1) {
window.showErrorMessage("Anki Markdown: Found only one of the BEGIN_ANKI_CARDS or END_ANKI_CARDS markers. Ignoring.");
}
if (begin === -1) {
return content;
}

const regex = /<!-- BEGIN_ANKI_CARDS -->([\s\S]*?)<!-- END_ANKI_CARDS -->/gi;
const matches = content.matchAll(regex);

// If there are matches, concatenate the text between the markers and return the result. Otherwise, return conent intact.
let result = "";
for (const match of matches) {
result += match[1] + EOL;
}
return result ?? content;

if (result === "") {
window.showErrorMessage("Anki Markdown: No text found between BEGIN_ANKI_CARDS and END_ANKI_CARDS markers. Ignoring.");
}

return result;

}

Expand Down

0 comments on commit 5f5813b

Please sign in to comment.