Skip to content

Commit

Permalink
feat: support remote urls.
Browse files Browse the repository at this point in the history
close #9.
  • Loading branch information
timgreen committed Nov 27, 2022
1 parent cf160eb commit 69136ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
7 changes: 5 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
"/oclif.manifest.json"
],
"dependencies": {
"@anki.md/core": "^0.10.0",
"@anki.md/core": "^0.10.1",
"@oclif/core": "^1.19.1",
"@oclif/plugin-autocomplete": "^1.3.6",
"@oclif/plugin-help": "^5",
"@oclif/plugin-not-found": "^2.3.9",
"@oclif/plugin-warn-if-update-available": "^2.0.14"
"@oclif/plugin-warn-if-update-available": "^2.0.14",
"node-fetch": "^3.3.0",
"valid-url": "^1.0.9"
},
"devDependencies": {
"@oclif/test": "^2.2.10",
"@types/chai": "^4",
"@types/mocha": "^9.0.0",
"@types/node": "^16.18.3",
"@types/valid-url": "^1.0.3",
"chai": "^4",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4",
Expand Down
34 changes: 23 additions & 11 deletions packages/cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ankiConnectSync, Parser, updateNoteId } from "@anki.md/core";
import { Command, Flags } from "@oclif/core";
import * as fs from "fs";
import fetch from "node-fetch";
import * as path from "path";
import { isUri } from "valid-url";

export default class Sync extends Command {
static description = "Sync to Anki Desktop via AnkiConnect.";
Expand All @@ -14,7 +16,7 @@ export default class Sync extends Command {
];

static args = [
{ name: "files", required: true, description: "Markdown files" },
{ name: "markdowns", required: true, description: "Markdown files" },
];

static flags = {
Expand Down Expand Up @@ -49,23 +51,33 @@ export default class Sync extends Command {

for (const input of argv) {
this.log(input);
const content = String(fs.readFileSync(input));
const deck = await parser.parse(content, path.dirname(input));
const isRemote = isUri(input);
const content = isRemote
? await (await fetch(input)).text()
: String(fs.readFileSync(input));
const deck = await parser.parse(
content,
isRemote ? input : path.dirname(input),
);
const noteIds = await ankiConnectSync(deck, {
updateModelTemplates: flags["update-model-templates"],
updateModelStyling: flags["update-model-styling"],
});

if (flags["save-note-ids"]) {
// TODO: check if the source if readonly or remote.
noteIds.forEach((noteId, index) => {
if (noteId) {
deck.notes[index].noteId = noteId;
if (isRemote) {
// TODO: warning about skip save note ids for remote files.
} else {
// TODO: check if the source if readonly.
noteIds.forEach((noteId, index) => {
if (noteId) {
deck.notes[index].noteId = noteId;
}
});
const updatedContent = updateNoteId(content, deck.notes);
if (content != updatedContent) {
fs.writeFileSync(input, updatedContent);
}
});
const updatedContent = updateNoteId(content, deck.notes);
if (content != updatedContent) {
fs.writeFileSync(input, updatedContent);
}
}
}
Expand Down

0 comments on commit 69136ec

Please sign in to comment.