Extra contents beyond WaniKani
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

82 lines
2.1 KiB

import path from 'path';
import { AnkiConnect, IAnkiConnectActions } from '@/ankiconnect';
import {
ensureDirSync,
readFileSync,
readdirSync,
writeFileSync,
} from 'fs-extra';
export class AnkiTemplateEditor {
$connect = new AnkiConnect();
templateDir = 'template';
rootDir = path.resolve(this.templateDir, this.modelName);
styleFilename = 'style.css';
constructor(public modelName: string) {
ensureDirSync(this.rootDir);
}
async get() {
const d = await this.$connect.api('modelTemplates', {
modelName: this.modelName,
});
Object.entries(d).map(([cardName, sideDict]) => {
const folderDir = path.resolve(this.rootDir, cardName);
ensureDirSync(folderDir);
Object.entries(sideDict).map(([side, html]) => {
writeFileSync(path.resolve(folderDir, side + '.html'), html);
});
});
const s = await this.$connect.api('modelStyling', {
modelName: this.modelName,
});
writeFileSync(path.resolve(this.rootDir, this.styleFilename), s.css);
}
async set() {
const templates: IAnkiConnectActions['updateModelTemplates']['params']['model']['templates'] =
{};
for (const p of readdirSync(this.rootDir)) {
if (p === this.styleFilename) {
await this.$connect.api('updateModelStyling', {
model: {
name: this.modelName,
css: readFileSync(
path.resolve(this.rootDir, this.styleFilename),
'utf-8',
),
},
});
continue;
}
readdirSync(path.resolve(this.rootDir, p)).map((p1) => {
const side = p1.replace(/\.[^\.]+$/, '') as 'Front' | 'Back';
const sideMap = templates[p] || {};
sideMap[side] = readFileSync(
path.resolve(this.rootDir, p, p1),
'utf-8',
);
templates[p] = sideMap;
});
}
await this.$connect.api('updateModelTemplates', {
model: {
name: this.modelName,
templates,
},
});
}
}
if (require.main === module) {
new AnkiTemplateEditor('jp.takoboto').set();
}