UNPKG

2.49 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const formatQuillDelta_1 = __importDefault(require("./content/formatQuillDelta"));
7const url_1 = __importDefault(require("url"));
8const SPREAD = "$$CONVERTER_SPREAD";
9function convert(inputs, passThrough, instructions) {
10 return Object.keys(instructions).reduce(async (memo, key) => {
11 const [resolvedMemo, value] = await Promise.all([
12 memo,
13 instructions[key](inputs)
14 ]);
15 if (key === SPREAD) {
16 return {
17 ...resolvedMemo,
18 ...value
19 };
20 }
21 return {
22 ...resolvedMemo,
23 [key]: value
24 };
25 }, Promise.resolve(passThrough.reduce((memo, key) => {
26 memo[key] = inputs[key];
27 return memo;
28 }, {})));
29}
30class Converter {
31 constructor(passThrough, toHub = {}, fromHub = {}) {
32 this.passThroughKeys = passThrough;
33 this.toHubInstructions = toHub;
34 this.fromHubInstructions = fromHub;
35 this.toHub = this.toHub.bind(this);
36 this.fromHub = this.fromHub.bind(this);
37 }
38 toHub(input) {
39 return convert(input, this.passThroughKeys, this.toHubInstructions);
40 }
41 fromHub(input) {
42 return convert(input, this.passThroughKeys, this.fromHubInstructions);
43 }
44}
45Converter.SPREAD = SPREAD;
46function buildHelper(opts) {
47 const staticUrl = (opts.baseUrls || {}).media || "";
48 return {
49 richtextToHtml(delta) {
50 return formatQuillDelta_1.default(delta, "html");
51 },
52 Converter,
53 media: {
54 original(image) {
55 if (image.match(/^http/)) {
56 return image;
57 }
58 return url_1.default.resolve(staticUrl, `./${image}`);
59 },
60 fromOriginal(image) {
61 return image.replace(new RegExp(`^${staticUrl}`), "");
62 }
63 }
64 };
65}
66function provide(externalDataSources = [], opts) {
67 const helper = buildHelper(opts);
68 return externalDataSources.map(externalDataSource => {
69 if (typeof externalDataSource === "function") {
70 return externalDataSource(helper);
71 }
72 return externalDataSource;
73 });
74}
75exports.provide = provide;
76//# sourceMappingURL=externalDataSourceHelper.js.map
\No newline at end of file