@cotype/core
Version:
<h2 align="center"> <img src="https://cotype.dev/logo.svg" alt="cotype" /> </h2>
76 lines • 2.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const formatQuillDelta_1 = __importDefault(require("./content/formatQuillDelta"));
const url_1 = __importDefault(require("url"));
const SPREAD = "$$CONVERTER_SPREAD";
function convert(inputs, passThrough, instructions) {
return Object.keys(instructions).reduce(async (memo, key) => {
const [resolvedMemo, value] = await Promise.all([
memo,
instructions[key](inputs)
]);
if (key === SPREAD) {
return {
...resolvedMemo,
...value
};
}
return {
...resolvedMemo,
[key]: value
};
}, Promise.resolve(passThrough.reduce((memo, key) => {
memo[key] = inputs[key];
return memo;
}, {})));
}
class Converter {
constructor(passThrough, toHub = {}, fromHub = {}) {
this.passThroughKeys = passThrough;
this.toHubInstructions = toHub;
this.fromHubInstructions = fromHub;
this.toHub = this.toHub.bind(this);
this.fromHub = this.fromHub.bind(this);
}
toHub(input) {
return convert(input, this.passThroughKeys, this.toHubInstructions);
}
fromHub(input) {
return convert(input, this.passThroughKeys, this.fromHubInstructions);
}
}
Converter.SPREAD = SPREAD;
function buildHelper(opts) {
const staticUrl = (opts.baseUrls || {}).media || "";
return {
richtextToHtml(delta) {
return formatQuillDelta_1.default(delta, "html");
},
Converter,
media: {
original(image) {
if (image.match(/^http/)) {
return image;
}
return url_1.default.resolve(staticUrl, `./${image}`);
},
fromOriginal(image) {
return image.replace(new RegExp(`^${staticUrl}`), "");
}
}
};
}
function provide(externalDataSources = [], opts) {
const helper = buildHelper(opts);
return externalDataSources.map(externalDataSource => {
if (typeof externalDataSource === "function") {
return externalDataSource(helper);
}
return externalDataSource;
});
}
exports.provide = provide;
//# sourceMappingURL=externalDataSourceHelper.js.map