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
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to the "anki" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.2.8]
- Embeddable Cards definitions. Extract `text` for processing from `<!-- BEGIN_ANKI_CARDS -->text<!-- END_ANKI_CARDS -->` match in the Markdown file.

## [1.2.7]

- Fix bug that LaTeX curly brackets do not convert correctly [#63](https://github.com/jasonwilliams/anki/issues/63)
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ necessary.

When parsing only one markdown file, the title of the deck could be generated based on the top-level headline (`#`).

To embed cards definitions in the existing document, one can limit the scope of the document to be processed between one or more matching pairs of
vavdoshka marked this conversation as resolved.
Show resolved Hide resolved
`<!-- BEGIN_ANKI_CARDS -->` and `<!-- END_ANKI_CARDS -->` markdown comments.
```
# Anki for VSCode

This is a VSCode extension for interacting and sending cards to Anki.
It uses AnkiConnect for communication so you will need this extension installed and running before installing the VSCode extension.

<!-- BEGIN_ANKI_CARDS -->

## What is the name of the most awesome VSCode plugin?

It is `Anki for VSCode` of course.

## What "Anki for VSCode" requires to function?

It requires AnkiConnect

<!-- END_ANKI_CARDS -->
```

### Cloze

You can make Cloze deletions in the card title `## A bit like {{c1::this}}`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "anki",
"displayName": "Anki for VSCode",
"description": "Sync notes with your locally running Anki",
"version": "1.2.7",
"version": "1.2.8",
"publisher": "jasew",
"engines": {
"vscode": "^1.66.0"
Expand Down
37 changes: 36 additions & 1 deletion src/models/MarkdownFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class MarkdownFile {
private loadContent(content: string) {
this.noteIds = this.readNoteIds(content);
this.autoSend = this.readAutoSendMeta(content);
this.cachedContent = this.removeMeta(content.toString());
this.cachedContent = this.removeMeta(
this.extractMarkedText(content.toString())
);
}

async load() {
Expand All @@ -51,6 +53,39 @@ export class MarkdownFile {
return path.dirname(this.uri.fsPath);
}



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

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

}

private removeMeta(content: string) {
const before = content.split(this.metaStart)[0];
const endSplit = content.split(this.metaEnd);
Expand Down