UNPKG

@rep2recall/r2r-format

Version:

R2r (offline/online) abstract class for Rep2Recall

48 lines (47 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function ankiMustache(s, d = {}, front = "") { if (Array.isArray(d)) { d = fromSortedData(d).data; } s = s.replace(/\{\{FrontSide}}/g, front.replace(/@html\n/g, "")); for (const [k, v] of Object.entries(d)) { if (typeof v === "string") { s = s.replace(new RegExp(`\\{\\{(\\S+:)?${escapeRegExp(k)}}}`, "g"), v.replace(/^@[^\n]+\n/gs, "")); } } s = s.replace(/\{\{#(\S+)}}([^]*)\{\{\1}}/gs, (m, p1, p2) => { return Object.keys(d).includes(p1) ? p2 : ""; }); s = s.replace(/{{[^}]+}}/g, ""); return s; } exports.ankiMustache = ankiMustache; function escapeRegExp(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } exports.escapeRegExp = escapeRegExp; function fromSortedData(sd) { const data = {}; const order = {}; let index = 1; for (const { key, value } of sd) { data[key] = value; order[key] = index; index++; } return { data, order }; } exports.fromSortedData = fromSortedData; function toSortedData(d) { const { data, order } = d; return Object.keys(order).sort((a, b) => { return order[b] - order[a]; }).map((key) => { return { key, value: data[key] }; }); } exports.toSortedData = toSortedData;