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

Adding embeddable cards in a backward compatible way. #91

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
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 -->
Copy link
Owner

@jasonwilliams jasonwilliams Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move these comments above the function using /** */ syntax?
its just so then you would get the function description when hovering over the symbol

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, cool, thanks. I'll do it as soon as I can.
Yes, I am also planning to add an action (command) to start the embedded Anki card, which will paste the markers under a cursor and move the cursor inside the quotes.

<!-- BEGIN_ANKI_CARDS -->
|
<!-- END_ANKI_CARDS -->

Any suggestions for the naming of that action? Thought of Anki: Start Embedded Cards.

Copy link
Owner

@jasonwilliams jasonwilliams Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anki: Start Embedded Cards

Should this be offered as a snippet instead? It looks more like a snippet than a full-on-command. I think the extension can contribute snippets

// 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.");
}

return result;

}

Expand Down