diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b6221c..b77602e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ 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.7] + +- Fix bug that LaTeX curly brackets do not convert correctly [#63](https://github.com/jasonwilliams/anki/issues/63) +- Fix bug when user sets the `saveStrategy` to `useDirStructure`, the actual deck does not match the displayed deck when executing the `Send To Deck` command + ## [1.2.6] - Add a new feature: Use the current directory structure as deck name [#71](https://github.com/jasonwilliams/anki/issues/71) diff --git a/README.md b/README.md index e7bfaa9..7d95937 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ echo "Hello, World!" - `Anki: Sync Anki`: This will run Sync on your Anki Instance - `Anki: Anki: Send To Deck`: This will attempt to send your (markdown) card into Anki - More info above - `Anki: Anki: Send To Own Deck`: Sends to a new deck using the Markdown's title (# example) as a deck name +- `Anki: Anki: Send To DirName Deck`: Sends to a new deck using the directory structure as a deck name - `Anki: Force Re-install`: This will attempt to re-setup the extension on Anki (Anki needs to be running). You shouldn't need this unless there's an issue. ## Extension Settings @@ -153,6 +154,7 @@ echo "Hello, World!" - `anki.api.hostname`: API Hostname. | _127.0.0.1_ - `anki.api.port`: API Port. | _8765_ - `anki.api.schema`: Schema. | _http_ +- `anki.saveStrategy`: the behavior of the command `Anki: Send To Deck` | _default_ I don't recommend messing with the following settings diff --git a/package.json b/package.json index 5ffb4ec..bed6cb4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "anki", "displayName": "Anki for VSCode", "description": "Sync notes with your locally running Anki", - "version": "1.2.6", + "version": "1.2.7", "publisher": "jasew", "engines": { "vscode": "^1.66.0" diff --git a/src/commands.ts b/src/commands.ts index e5536d7..cbd9562 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -32,20 +32,19 @@ export const registerCommands = (ctx: IContext) => { "anki.sendToDeck", async () => { // The code you place here will be executed every time your command is executed + let strategyStr = workspace.getConfiguration("anki").get("saveStrategy") as string; + let processInfo = strategyStr === "default" ? `Sending to Deck: ${ctx.config.defaultDeck}...` : `Sending to dirname deck...`; + let strategy = strategyStr === "default" ? DeckNameStrategy.UseDefault : DeckNameStrategy.ParseDirStru; window.withProgress( { location: ProgressLocation.Notification, - title: `Sending to Deck: ${ctx.config.defaultDeck}...`, + title: processInfo, cancellable: false, }, async () => { try { getLogger().info("active Editor.."); - if (workspace.getConfiguration("anki").get("saveStrategy") as string === "default") { - await new Transformer(MarkdownFile.fromActiveTextEditor(), ctx.ankiService, DeckNameStrategy.UseDefault).transform(); - } else { - await new Transformer(MarkdownFile.fromActiveTextEditor(), ctx.ankiService, DeckNameStrategy.ParseDirStru).transform(); - } + await new Transformer(MarkdownFile.fromActiveTextEditor(), ctx.ankiService, strategy).transform(); } catch (e) { window.showErrorMessage(e.toString()); } diff --git a/src/markdown/__tests__/Latex.test.ts b/src/markdown/__tests__/Latex.test.ts new file mode 100644 index 0000000..20816c9 --- /dev/null +++ b/src/markdown/__tests__/Latex.test.ts @@ -0,0 +1,55 @@ +import { CardParser } from "../parsers/cardParser"; + +describe("Latex", () => { + it("Should keep the content of 'Latex inline' unchanged.", async () => { + // linesToHtml always return a newline at the end of string + const latexs = ["a=\\%1", "b=\\#1"]; + const input = [ + `extra stuff before the latex`, + `extra stuff before the latex $${latexs[0]}$ extra stuff after the latex`, + `$${latexs[1]}$`, + `extra stuff after the latex` + ]; + const cardParser = new CardParser(); + // Act + const result = await cardParser.linesToHtml(input); + // Assert + for (let latex of latexs) { + expect(result).toMatch(latex); + } + }); + it("Should keep the content of 'Latex block' unchanged.", async () => { + // linesToHtml always return a newline at the end of string + const latexs = ["a=\\%1", "b=\\#1", + "\\before{align}", "c=\\{1,2\\} \\\\", "d=\\$2", "\\end{align}"]; + const input = [ + `extra stuff before the latex`, + `$$${latexs[0]} ${latexs[1]}$$`, // one line latex block + `extra stuff`, + `$$`, // multiline latex block + `${latexs[2]}`, + `${latexs[3]}`, + `${latexs[4]}`, + `${latexs[5]}`, + `$$` + ]; + const cardParser = new CardParser(); + // Act + const result = await cardParser.linesToHtml(input); + // Assert + for (let latex of latexs) { + expect(result).toMatch(latex); + } + }); + it("Should not affect the conversion outside of latex", async () => { + const htmlStr = "

%

\n"; + const input = [ + "\\%" + ]; + const cardParser = new CardParser(); + // Act + const result = (await cardParser.linesToHtml(input)); + // Assert + expect(result).toEqual(htmlStr); + }); +}); diff --git a/src/markdown/parsers/cardParser.ts b/src/markdown/parsers/cardParser.ts index be6f963..2f2d311 100644 --- a/src/markdown/parsers/cardParser.ts +++ b/src/markdown/parsers/cardParser.ts @@ -135,7 +135,16 @@ export class CardParser extends BaseParser { * @private */ async linesToHtml(lines: string[]) { - const string = lines.join("\n"); + const fixLatex = (match: string) => ( + /\n\n/.test(match) ? + match : // If there is an empty line, return directly + match.replace(/\\[{}%#&$_\\]/g, str => str === "\\\\" ? "\\\\\\\\" : ("\\" + str)) + ); + const string = lines.join("\n") + // $$\{1,2\} \%100$$ => $$\\{1,2\\} \\%100$$ + .replace(/(? $\\{1,2\\} \\%100$ + .replace(/(? { + // (?\(100\$\) return string - .replace(/\$\$(.+?)\$\$/g, "\\[$1\\]") // $equation$ -> \(equation\) - .replace(/\$(.+?)\$/g, "\\($1\\)"); // $$equation$$ -> \[equation\] + .replace(/(? \[equation\] + .replace(/(? \(equation\) } } diff --git a/src/markdown/transformer.ts b/src/markdown/transformer.ts index 1987743..87f19b6 100644 --- a/src/markdown/transformer.ts +++ b/src/markdown/transformer.ts @@ -101,7 +101,7 @@ export class Transformer { const filePath = fileUri.path; let deckName: string = ""; if (rootPath && filePath) { - deckName = relative(rootPath, dirname(filePath)).replace(/\\/g, '::'); + deckName = relative(rootPath, dirname(filePath)).replace(/[\/\\]/g, '::'); } return deckName || this.defaultDeck; }