@difizen/libro-rendermime 0.0.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/es/components/html-render.d.ts +6 -0
- package/es/components/html-render.d.ts.map +1 -0
- package/es/components/html-render.js +39 -0
- package/es/components/image-render.d.ts +6 -0
- package/es/components/image-render.d.ts.map +1 -0
- package/es/components/image-render.js +36 -0
- package/es/components/index.d.ts +7 -0
- package/es/components/index.d.ts.map +1 -0
- package/es/components/index.js +6 -0
- package/es/components/latex-render.d.ts +6 -0
- package/es/components/latex-render.d.ts.map +1 -0
- package/es/components/latex-render.js +23 -0
- package/es/components/markdown-render.d.ts +6 -0
- package/es/components/markdown-render.d.ts.map +1 -0
- package/es/components/markdown-render.js +38 -0
- package/es/components/svg-render.d.ts +6 -0
- package/es/components/svg-render.d.ts.map +1 -0
- package/es/components/svg-render.js +33 -0
- package/es/components/text-render.d.ts +9 -0
- package/es/components/text-render.d.ts.map +1 -0
- package/es/components/text-render.js +44 -0
- package/es/index.d.ts +7 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +6 -0
- package/es/renderers.d.ts +42 -0
- package/es/renderers.d.ts.map +1 -0
- package/es/renderers.js +327 -0
- package/es/rendermime-factory.d.ts +30 -0
- package/es/rendermime-factory.d.ts.map +1 -0
- package/es/rendermime-factory.js +73 -0
- package/es/rendermime-module.d.ts +3 -0
- package/es/rendermime-module.d.ts.map +1 -0
- package/es/rendermime-module.js +11 -0
- package/es/rendermime-protocol.d.ts +405 -0
- package/es/rendermime-protocol.d.ts.map +1 -0
- package/es/rendermime-protocol.js +60 -0
- package/es/rendermime-registry.d.ts +135 -0
- package/es/rendermime-registry.d.ts.map +1 -0
- package/es/rendermime-registry.js +392 -0
- package/es/rendermime-utils.d.ts +87 -0
- package/es/rendermime-utils.d.ts.map +1 -0
- package/es/rendermime-utils.js +603 -0
- package/package.json +61 -0
- package/src/components/html-render.tsx +42 -0
- package/src/components/image-render.tsx +46 -0
- package/src/components/index.ts +6 -0
- package/src/components/latex-render.tsx +30 -0
- package/src/components/markdown-render.tsx +42 -0
- package/src/components/svg-render.tsx +38 -0
- package/src/components/text-render.tsx +51 -0
- package/src/index.ts +6 -0
- package/src/renderers.ts +325 -0
- package/src/rendermime-factory.ts +92 -0
- package/src/rendermime-module.ts +19 -0
- package/src/rendermime-protocol.ts +516 -0
- package/src/rendermime-registry.ts +301 -0
- package/src/rendermime-utils.ts +665 -0
package/es/renderers.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
var _excluded = ["host", "source"];
|
|
3
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
4
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
6
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
7
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
8
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
9
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
10
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
11
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
12
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
13
|
+
import { concatMultilineString } from '@difizen/libro-common';
|
|
14
|
+
import { ansiSpan, autolink, evalInnerHTMLScriptTags, handleDefaults, handleUrls, splitShallowNode } from "./rendermime-utils.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Render text into a host node.
|
|
18
|
+
*
|
|
19
|
+
* @param options - The options for rendering.
|
|
20
|
+
*
|
|
21
|
+
* @returns A promise which resolves when rendering is complete.
|
|
22
|
+
*/
|
|
23
|
+
export function renderText(options) {
|
|
24
|
+
// Unpack the options.
|
|
25
|
+
var host = options.host,
|
|
26
|
+
sanitizer = options.sanitizer,
|
|
27
|
+
source = options.source,
|
|
28
|
+
mimeType = options.mimeType;
|
|
29
|
+
var data = concatMultilineString(JSON.parse(JSON.stringify(source)));
|
|
30
|
+
// Create the HTML content.
|
|
31
|
+
var content = sanitizer.sanitize(ansiSpan(data), {
|
|
32
|
+
allowedTags: ['span']
|
|
33
|
+
});
|
|
34
|
+
if (mimeType === 'application/vnd.jupyter.stderr') {
|
|
35
|
+
host.setAttribute('data-mime-type', 'application/vnd.jupyter.stderr');
|
|
36
|
+
}
|
|
37
|
+
// Set the sanitized content for the host node.
|
|
38
|
+
var pre = document.createElement('pre');
|
|
39
|
+
pre.innerHTML = content;
|
|
40
|
+
var preTextContent = pre.textContent;
|
|
41
|
+
if (preTextContent) {
|
|
42
|
+
// Note: only text nodes and span elements should be present after sanitization in the `<pre>` element.
|
|
43
|
+
var linkedNodes = autolink(preTextContent);
|
|
44
|
+
var inAnchorElement = false;
|
|
45
|
+
var combinedNodes = [];
|
|
46
|
+
var preNodes = Array.from(pre.childNodes);
|
|
47
|
+
while (preNodes.length && linkedNodes.length) {
|
|
48
|
+
var _preNode$textContent, _linkNode$textContent;
|
|
49
|
+
// Use non-null assertions to workaround TypeScript context awareness limitation
|
|
50
|
+
// (if any of the arrays were empty, we would not enter the body of the loop).
|
|
51
|
+
var preNode = preNodes.shift();
|
|
52
|
+
var linkNode = linkedNodes.shift();
|
|
53
|
+
|
|
54
|
+
// This should never happen because we modify the arrays in flight so they should end simultaneously,
|
|
55
|
+
// but this makes the coding assistance happy and might make it easier to conceptualize.
|
|
56
|
+
if (typeof preNode === 'undefined') {
|
|
57
|
+
combinedNodes.push(linkNode);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
if (typeof linkNode === 'undefined') {
|
|
61
|
+
combinedNodes.push(preNode);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
var preLen = (_preNode$textContent = preNode.textContent) === null || _preNode$textContent === void 0 ? void 0 : _preNode$textContent.length;
|
|
65
|
+
var linkLen = (_linkNode$textContent = linkNode.textContent) === null || _linkNode$textContent === void 0 ? void 0 : _linkNode$textContent.length;
|
|
66
|
+
if (preLen && linkLen) {
|
|
67
|
+
if (preLen > linkLen) {
|
|
68
|
+
// Split pre node and only keep the shorter part
|
|
69
|
+
var _splitShallowNode = splitShallowNode(preNode, linkLen),
|
|
70
|
+
keep = _splitShallowNode.pre,
|
|
71
|
+
postpone = _splitShallowNode.post;
|
|
72
|
+
preNodes.unshift(postpone);
|
|
73
|
+
preNode = keep;
|
|
74
|
+
} else if (linkLen > preLen) {
|
|
75
|
+
var _splitShallowNode2 = splitShallowNode(linkNode, preLen),
|
|
76
|
+
_keep = _splitShallowNode2.pre,
|
|
77
|
+
_postpone = _splitShallowNode2.post;
|
|
78
|
+
linkedNodes.unshift(_postpone);
|
|
79
|
+
linkNode = _keep;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
var lastCombined = combinedNodes[combinedNodes.length - 1];
|
|
83
|
+
|
|
84
|
+
// If we are already in an anchor element and the anchor element did not change,
|
|
85
|
+
// we should insert the node from <pre> which is either Text node or coloured span Element
|
|
86
|
+
// into the anchor content as a child
|
|
87
|
+
if (inAnchorElement && linkNode.href === lastCombined.href) {
|
|
88
|
+
lastCombined.appendChild(preNode);
|
|
89
|
+
} else {
|
|
90
|
+
// the `linkNode` is either Text or AnchorElement;
|
|
91
|
+
var isAnchor = linkNode.nodeType !== Node.TEXT_NODE;
|
|
92
|
+
// if we are NOT about to start an anchor element, just add the pre Node
|
|
93
|
+
if (!isAnchor) {
|
|
94
|
+
combinedNodes.push(preNode);
|
|
95
|
+
inAnchorElement = false;
|
|
96
|
+
} else {
|
|
97
|
+
// otherwise start a new anchor; the contents of the `linkNode` and `preNode` should be the same,
|
|
98
|
+
// so we just put the neatly formatted `preNode` inside the anchor node (`linkNode`)
|
|
99
|
+
// and append that to combined nodes.
|
|
100
|
+
linkNode.textContent = '';
|
|
101
|
+
linkNode.appendChild(preNode);
|
|
102
|
+
combinedNodes.push(linkNode);
|
|
103
|
+
inAnchorElement = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// TODO: replace with `.replaceChildren()` once the target ES version allows it
|
|
108
|
+
pre.innerHTML = '';
|
|
109
|
+
for (var _i = 0, _combinedNodes = combinedNodes; _i < _combinedNodes.length; _i++) {
|
|
110
|
+
var child = _combinedNodes[_i];
|
|
111
|
+
pre.appendChild(child);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
host.appendChild(pre);
|
|
115
|
+
host.classList.add('libro-text-render');
|
|
116
|
+
|
|
117
|
+
// Return the rendered promise.
|
|
118
|
+
return Promise.resolve(undefined);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Render an image into a host node.
|
|
123
|
+
*
|
|
124
|
+
* @param options - The options for rendering.
|
|
125
|
+
*
|
|
126
|
+
* @returns A promise which resolves when rendering is complete.
|
|
127
|
+
*/
|
|
128
|
+
export function renderImage(options) {
|
|
129
|
+
// Unpack the options.
|
|
130
|
+
var host = options.host,
|
|
131
|
+
mimeType = options.mimeType,
|
|
132
|
+
source = options.source,
|
|
133
|
+
width = options.width,
|
|
134
|
+
height = options.height,
|
|
135
|
+
needsBackground = options.needsBackground,
|
|
136
|
+
unconfined = options.unconfined;
|
|
137
|
+
|
|
138
|
+
// Clear the content in the host.
|
|
139
|
+
host.textContent = '';
|
|
140
|
+
|
|
141
|
+
// Create the image element.
|
|
142
|
+
var img = document.createElement('img');
|
|
143
|
+
|
|
144
|
+
// Set the source of the image.
|
|
145
|
+
img.src = "data:".concat(mimeType, ";base64,").concat(source);
|
|
146
|
+
|
|
147
|
+
// Set the size of the image if provided.
|
|
148
|
+
if (typeof height === 'number') {
|
|
149
|
+
img.height = height;
|
|
150
|
+
}
|
|
151
|
+
if (typeof width === 'number') {
|
|
152
|
+
img.width = width;
|
|
153
|
+
}
|
|
154
|
+
if (needsBackground === 'light') {
|
|
155
|
+
img.classList.add('jp-needs-light-background');
|
|
156
|
+
} else if (needsBackground === 'dark') {
|
|
157
|
+
img.classList.add('jp-needs-dark-background');
|
|
158
|
+
}
|
|
159
|
+
if (unconfined === true) {
|
|
160
|
+
img.classList.add('jp-mod-unconfined');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Add the image to the host.
|
|
164
|
+
host.appendChild(img);
|
|
165
|
+
|
|
166
|
+
// Return the rendered promise.
|
|
167
|
+
return Promise.resolve(undefined);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Render HTML into a host node.
|
|
172
|
+
*
|
|
173
|
+
* @param options - The options for rendering.
|
|
174
|
+
*
|
|
175
|
+
* @returns A promise which resolves when rendering is complete.
|
|
176
|
+
*/
|
|
177
|
+
export function renderHTML(options) {
|
|
178
|
+
// Unpack the options.
|
|
179
|
+
var host = options.host,
|
|
180
|
+
trusted = options.trusted,
|
|
181
|
+
sanitizer = options.sanitizer,
|
|
182
|
+
resolver = options.resolver,
|
|
183
|
+
linkHandler = options.linkHandler;
|
|
184
|
+
var source = options.source;
|
|
185
|
+
|
|
186
|
+
// translator = translator || nullTranslator;
|
|
187
|
+
// const trans = translator?.load('jupyterlab');
|
|
188
|
+
var originalSource = source;
|
|
189
|
+
|
|
190
|
+
// Bail early if the source is empty.
|
|
191
|
+
if (!source) {
|
|
192
|
+
host.textContent = '';
|
|
193
|
+
return Promise.resolve(undefined);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Sanitize the source if it is not trusted. This removes all
|
|
197
|
+
// `<script>` tags as well as other potentially harmful HTML.
|
|
198
|
+
if (!trusted) {
|
|
199
|
+
originalSource = "".concat(source);
|
|
200
|
+
source = sanitizer.sanitize(source);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Set the inner HTML of the host.
|
|
204
|
+
host.innerHTML = source;
|
|
205
|
+
if (host.getElementsByTagName('script').length > 0) {
|
|
206
|
+
// If output it trusted, eval any script tags contained in the HTML.
|
|
207
|
+
// This is not done automatically by the browser when script tags are
|
|
208
|
+
// created by setting `innerHTML`.
|
|
209
|
+
if (trusted) {
|
|
210
|
+
evalInnerHTMLScriptTags(host);
|
|
211
|
+
} else {
|
|
212
|
+
var container = document.createElement('div');
|
|
213
|
+
var warning = document.createElement('pre');
|
|
214
|
+
warning.textContent = 'This HTML output contains inline scripts. Are you sure that you want to run arbitrary Javascript within your JupyterLab session?';
|
|
215
|
+
var runButton = document.createElement('button');
|
|
216
|
+
runButton.textContent = 'Run';
|
|
217
|
+
runButton.onclick = function () {
|
|
218
|
+
host.innerHTML = originalSource;
|
|
219
|
+
evalInnerHTMLScriptTags(host);
|
|
220
|
+
if (host.firstChild) {
|
|
221
|
+
host.removeChild(host.firstChild);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
container.appendChild(warning);
|
|
225
|
+
container.appendChild(runButton);
|
|
226
|
+
host.insertBefore(container, host.firstChild);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Handle default behavior of nodes.
|
|
231
|
+
handleDefaults(host, resolver);
|
|
232
|
+
|
|
233
|
+
// Patch the urls if a resolver is available.
|
|
234
|
+
var promise;
|
|
235
|
+
if (resolver) {
|
|
236
|
+
promise = handleUrls(host, resolver, linkHandler);
|
|
237
|
+
} else {
|
|
238
|
+
promise = Promise.resolve(undefined);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Return the final rendered promise.
|
|
242
|
+
return promise;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Render SVG into a host node.
|
|
247
|
+
*
|
|
248
|
+
* @param options - The options for rendering.
|
|
249
|
+
*
|
|
250
|
+
* @returns A promise which resolves when rendering is complete.
|
|
251
|
+
*/
|
|
252
|
+
export function renderSVG(options) {
|
|
253
|
+
// Unpack the options.
|
|
254
|
+
var host = options.host,
|
|
255
|
+
unconfined = options.unconfined;
|
|
256
|
+
var source = options.source;
|
|
257
|
+
// Clear the content if there is no source.
|
|
258
|
+
if (!source) {
|
|
259
|
+
host.textContent = '';
|
|
260
|
+
return Promise.resolve(undefined);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// // Display a message if the source is not trusted.
|
|
264
|
+
// if (!trusted) {
|
|
265
|
+
// host.textContent = 'Cannot display an untrusted SVG. Maybe you need to run the cell?';
|
|
266
|
+
// return Promise.resolve(undefined);
|
|
267
|
+
// }
|
|
268
|
+
|
|
269
|
+
// Add missing SVG namespace (if actually missing)
|
|
270
|
+
var patt = '<svg[^>]+xmlns=[^>]+svg';
|
|
271
|
+
if (source.search(patt) < 0) {
|
|
272
|
+
source = source.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Render in img so that user can save it easily
|
|
276
|
+
var img = new Image();
|
|
277
|
+
img.src = "data:image/svg+xml,".concat(encodeURIComponent(source));
|
|
278
|
+
host.appendChild(img);
|
|
279
|
+
if (unconfined === true) {
|
|
280
|
+
host.classList.add('jp-mod-unconfined');
|
|
281
|
+
}
|
|
282
|
+
return Promise.resolve();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Render Markdown into a host node.
|
|
287
|
+
*
|
|
288
|
+
* @param options - The options for rendering.
|
|
289
|
+
*
|
|
290
|
+
* @returns A promise which resolves when rendering is complete.
|
|
291
|
+
*/
|
|
292
|
+
export function renderMarkdown(_x) {
|
|
293
|
+
return _renderMarkdown.apply(this, arguments);
|
|
294
|
+
}
|
|
295
|
+
function _renderMarkdown() {
|
|
296
|
+
_renderMarkdown = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
|
|
297
|
+
var host, source, others, html;
|
|
298
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
299
|
+
while (1) switch (_context.prev = _context.next) {
|
|
300
|
+
case 0:
|
|
301
|
+
// Unpack the options.
|
|
302
|
+
host = options.host, source = options.source, others = _objectWithoutProperties(options, _excluded); // Clear the content if there is no source.
|
|
303
|
+
if (source) {
|
|
304
|
+
_context.next = 4;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
host.textContent = '';
|
|
308
|
+
return _context.abrupt("return");
|
|
309
|
+
case 4:
|
|
310
|
+
html = ''; // Fallback if the application does not have any markdown parser.
|
|
311
|
+
html = "<pre>".concat(source, "</pre>");
|
|
312
|
+
|
|
313
|
+
// Render HTML.
|
|
314
|
+
_context.next = 8;
|
|
315
|
+
return renderHTML(_objectSpread({
|
|
316
|
+
host: host,
|
|
317
|
+
source: html,
|
|
318
|
+
shouldTypeset: false
|
|
319
|
+
}, others));
|
|
320
|
+
case 8:
|
|
321
|
+
case "end":
|
|
322
|
+
return _context.stop();
|
|
323
|
+
}
|
|
324
|
+
}, _callee);
|
|
325
|
+
}));
|
|
326
|
+
return _renderMarkdown.apply(this, arguments);
|
|
327
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { IRendererFactory } from './rendermime-protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* A mime renderer factory for raw html.
|
|
4
|
+
*/
|
|
5
|
+
export declare const htmlRendererFactory: IRendererFactory;
|
|
6
|
+
/**
|
|
7
|
+
* A mime renderer factory for images.
|
|
8
|
+
*/
|
|
9
|
+
export declare const imageRendererFactory: IRendererFactory;
|
|
10
|
+
/**
|
|
11
|
+
* A mime renderer factory for LaTeX.
|
|
12
|
+
*/
|
|
13
|
+
export declare const latexRendererFactory: IRendererFactory;
|
|
14
|
+
/**
|
|
15
|
+
* A mime renderer factory for Markdown.
|
|
16
|
+
*/
|
|
17
|
+
export declare const markdownRendererFactory: IRendererFactory;
|
|
18
|
+
/**
|
|
19
|
+
* A mime renderer factory for svg.
|
|
20
|
+
*/
|
|
21
|
+
export declare const svgRendererFactory: IRendererFactory;
|
|
22
|
+
/**
|
|
23
|
+
* A mime renderer factory for plain and jupyter console text data.
|
|
24
|
+
*/
|
|
25
|
+
export declare const textRendererFactory: IRendererFactory;
|
|
26
|
+
/**
|
|
27
|
+
* The standard factories provided by the rendermime package.
|
|
28
|
+
*/
|
|
29
|
+
export declare const standardRendererFactories: IRendererFactory[];
|
|
30
|
+
//# sourceMappingURL=rendermime-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rendermime-factory.d.ts","sourceRoot":"","sources":["../src/rendermime-factory.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAEjE;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,gBAMjC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,gBAMlC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,gBAMlC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,gBAMrC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,gBAMhC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,gBAUjC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,yBAAyB,EAAE,gBAAgB,EAQvD,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { HTMLRender, ImageRender, LatexRender, MarkdownRender, SVGRender, TextRender } from "./components/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* A mime renderer factory for raw html.
|
|
4
|
+
*/
|
|
5
|
+
export var htmlRendererFactory = {
|
|
6
|
+
renderType: 'htmlRenderer',
|
|
7
|
+
safe: true,
|
|
8
|
+
mimeTypes: ['text/html'],
|
|
9
|
+
defaultRank: 50,
|
|
10
|
+
render: HTMLRender
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A mime renderer factory for images.
|
|
15
|
+
*/
|
|
16
|
+
export var imageRendererFactory = {
|
|
17
|
+
renderType: 'imageRenderer',
|
|
18
|
+
safe: true,
|
|
19
|
+
mimeTypes: ['image/bmp', 'image/png', 'image/jpeg', 'image/gif', 'image/webp'],
|
|
20
|
+
defaultRank: 90,
|
|
21
|
+
render: ImageRender
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A mime renderer factory for LaTeX.
|
|
26
|
+
*/
|
|
27
|
+
export var latexRendererFactory = {
|
|
28
|
+
renderType: 'latexRenderer',
|
|
29
|
+
safe: true,
|
|
30
|
+
mimeTypes: ['text/latex'],
|
|
31
|
+
defaultRank: 70,
|
|
32
|
+
render: LatexRender
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A mime renderer factory for Markdown.
|
|
37
|
+
*/
|
|
38
|
+
export var markdownRendererFactory = {
|
|
39
|
+
renderType: 'markdownRenderer',
|
|
40
|
+
safe: true,
|
|
41
|
+
mimeTypes: ['text/markdown'],
|
|
42
|
+
defaultRank: 60,
|
|
43
|
+
render: MarkdownRender
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A mime renderer factory for svg.
|
|
48
|
+
*/
|
|
49
|
+
export var svgRendererFactory = {
|
|
50
|
+
renderType: 'svgRenderer',
|
|
51
|
+
safe: false,
|
|
52
|
+
mimeTypes: ['image/svg+xml'],
|
|
53
|
+
defaultRank: 80,
|
|
54
|
+
render: SVGRender
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A mime renderer factory for plain and jupyter console text data.
|
|
59
|
+
*/
|
|
60
|
+
export var textRendererFactory = {
|
|
61
|
+
renderType: 'textRenderer',
|
|
62
|
+
safe: true,
|
|
63
|
+
mimeTypes: ['text/plain', 'application/vnd.jupyter.stdout', 'application/vnd.jupyter.stderr'],
|
|
64
|
+
defaultRank: 120,
|
|
65
|
+
render: TextRender
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The standard factories provided by the rendermime package.
|
|
70
|
+
*/
|
|
71
|
+
export var standardRendererFactories = [htmlRendererFactory, markdownRendererFactory, latexRendererFactory, svgRendererFactory, imageRendererFactory,
|
|
72
|
+
// javaScriptRendererFactory,
|
|
73
|
+
textRendererFactory];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rendermime-module.d.ts","sourceRoot":"","sources":["../src/rendermime-module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAS/C,eAAO,MAAM,qBAAqB,YAQP,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MarkdownModule } from '@difizen/libro-markdown';
|
|
2
|
+
import { ManaModule } from '@difizen/mana-app';
|
|
3
|
+
import { standardRendererFactories as initialFactories } from "./rendermime-factory.js";
|
|
4
|
+
import { IRenderMimeRegistryOptions, RenderMimeContribution } from "./rendermime-protocol.js";
|
|
5
|
+
import { RenderMimeRegistry } from "./rendermime-registry.js";
|
|
6
|
+
export var LibroRenderMimeModule = ManaModule.create().contribution(RenderMimeContribution).register(RenderMimeRegistry, {
|
|
7
|
+
token: IRenderMimeRegistryOptions,
|
|
8
|
+
useValue: {
|
|
9
|
+
initialFactories: initialFactories
|
|
10
|
+
}
|
|
11
|
+
}).dependOn(MarkdownModule);
|