scrapboxでdeno-ankiを動かすテスト
2023-02-09
20:38:50 cloze問題が認識されるところまで確認
19:56:58 成功した!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!takker.icon*20
code:script.js
import { installCDN } from "../scrapbox-install-CDN/script.js";
import { makeAnkiDB, makeApkg } from "../../takker-dist/deno-anki/mod.js";
const version = "1.8.0"
await Promise.all([
installCDN(https://cdnjs.cloudflare.com/ajax/libs/sql.js/${version}/sql-wasm.min.js, {
id: "sqljs-cdn",
}),
installCDN(https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js, {
id: "jszip-cdn",
}),
])
// We must specify this locateFile function if we are loading a wasm file from anywhere other than the current html page's folder.
const sql = await initSqlJs({
locateFile: (file) => file === "sql-wasm.wasm" ?
https://cdnjs.cloudflare.com/ajax/libs/sql.js/${version}/${file},
});
export const makeNormalExampleDeck = async () => {
const deck = {
name: "normal-deck-example",
description: "Scrapbox上で通常のAnki deckを作成してapkgとしてimportするテスト",
created: new Date().getTime(),
};
const model = {
name: "Basic-Scrapbox",
created: new Date().getTime(),
deckId: deck.created,
templates: [{
name: "Card 1",
question: "{{Front}}",
answer: '{{FrontSide}}\n\n<hr id="answer">\n\n{{Back}}',
}],
};
const cards = [
{ front: "card #1 front", back: "card #1 back" }, { front: "card #2 front", back: "card #2 back" }, {
front: 'card #3 with image <img src="anki.png" />', },
];
// Create deck as in previous example
const id = new Date().getTime();
const ankiDB = await makeAnkiDB(
{
models: [{
...model,
notes: cards.map(({ front, back }) => ({
created: id,
})),
}],
},
sql,
);
const apkg = await makeApkg(ankiDB, {}, new JSZip());
const url = URL.createObjectURL(apkg);
const a = document.createElement("a");
a.href = url;
a.download = "normal.apkg";
a.style.display = "none";
a.click();
a.remove();
URL.revokeObjectURL(url);
};
export const makeClozeExampleDeck = async () => {
const deck = {
name: "cloze-deck-example",
description: "Scrapbox上でcloze問題のAnki deckを作成してapkgとしてimportするテスト",
created: new Date().getTime(),
};
const model = {
name: "Cloze-Scrapbox",
created: new Date().getTime(),
deckId: deck.created,
isCloze: true,
templates: [{
name: "Cloze",
question: "{{cloze:Front}}",
answer: '{{cloze:Front}}\n\n<hr id="answer">\n\n{{Hint}}',
}],
};
const cards = [
["Card {{c1::front}} side 1", "Cloze hint 1", [
"some",
"tag",
"tags with multiple words",
]],
[
"{{c1::Card}} {{c1::front}} side {{c2::2}}",
"Cloze hint 2",
],
[
"{{c1::Card}} {{c2::front}}} {{c3::side}} {{c4::3::数字}}",
"Card back side 3",
],
];
const id = new Date().getTime();
const ankiDB = await makeAnkiDB(
{
models: [{
...model,
tags,
created: id,
})),
}],
},
sql,
);
const apkg = await makeApkg(ankiDB, {}, new JSZip());
const url = URL.createObjectURL(apkg);
const a = document.createElement("a");
a.href = url;
a.download = "cloze.apkg";
a.style.display = "none";
a.click();
a.remove();
URL.revokeObjectURL(url);
};