UNPKG

@qtikit/mathml-to-latex

Version:

A JavaScript tool to convert mathml string to LaTeX string

37 lines (36 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementsToMathMLAdapter = void 0; var ElementsToMathMLAdapter = /** @class */ (function () { function ElementsToMathMLAdapter() { } ElementsToMathMLAdapter.prototype.convert = function (els) { var _this = this; return els.filter(function (el) { return el.tagName !== undefined; }).map(function (el) { return _this._convertElement(el); }); }; ElementsToMathMLAdapter.prototype._convertElement = function (el) { return { name: el.tagName, attributes: this._convertElementAttributes(el.attributes), value: this._hasElementChild(el) ? '' : el.textContent || '', children: this._hasElementChild(el) ? this.convert(Array.from(el.childNodes)) : [], }; }; ElementsToMathMLAdapter.prototype._convertElementAttributes = function (attributes) { return Array.from(attributes).reduce(function (acc, attr) { var _a; return Object.assign((_a = {}, _a[attr.nodeName] = attr.nodeValue === attr.nodeName ? '' : attr.nodeValue, _a), acc); }, {}); }; ElementsToMathMLAdapter.prototype._hasElementChild = function (el) { var childNodes = el.childNodes; return !!childNodes && childNodes.length !== 0 && this._isThereAnyNoTextNode(childNodes); }; ElementsToMathMLAdapter.prototype._isThereAnyNoTextNode = function (children) { return Array.from(children).some(function (child) { return child.nodeName !== '#text'; }); }; return ElementsToMathMLAdapter; }()); exports.ElementsToMathMLAdapter = ElementsToMathMLAdapter;