fix template
This commit is contained in:
1 parent
c8207d6ee5
commit
00efde8f22
8 files changed
+538
No files matched your search
@@ -0,0 +1,82 @@
|
||||
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();
|
||||
}
|
||||
Reference in new issue
Block a user