mathpix-markdown-it
Version:
Mathpix-markdown-it is an open source implementation of the mathpix-markdown spec written in Typescript. It relies on the following open source libraries: MathJax v3 (to render math with SVGs), markdown-it (for standard Markdown parsing)
91 lines • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IncludeGraphics = exports.InlineDecimal = exports.CaptionTable = void 0;
var escapeHtml = require('markdown-it/lib/common/utils').escapeHtml;
var consts_1 = require("../common/consts");
var CaptionTable = function (tokens, idx, options, env, slf) {
var _a, _b;
var token = tokens[idx];
var className = token.attrGet('class') || 'caption_table';
var printText = (_a = token.print) !== null && _a !== void 0 ? _a : '';
var htmlCaption = ((_b = token.children) === null || _b === void 0 ? void 0 : _b.length)
? slf.renderInline(token.children, options, env)
: token.content;
return "<div class=\"".concat(className, "\">").concat(printText).concat(htmlCaption, "</div>");
};
exports.CaptionTable = CaptionTable;
var InlineDecimal = function (a, token) {
if (!token.content) {
return '';
}
var arr = token.content.split(';');
return "<span class=\"f\">".concat(arr[0], "</span><span class=\"decimal_left\">").concat(arr[1] ? arr[1] : '', "</span><span class=\"f\">.").concat(arr[2] ? arr[2] : '', "</span>");
};
exports.InlineDecimal = InlineDecimal;
var IncludeGraphics = function (tokens, idx, options, env, slf) {
var _a, _b;
var token = tokens[idx];
var RAW_WIDTH = token.attrGet('width') || '';
var RAW_MAX_WIDTH = token.attrGet('max width') || '';
var RAW_HEIGHT = token.attrGet('height') || '';
var SRC = token.attrGet('src') || '';
var ALT = token.attrGet('alt') || '';
var HILITE = token.attrGet('data-mmd-highlight') || '';
var TAG = token.tag || 'img';
// wrapper styles (centering, highlighting)
var wrapperStyles = [];
var align = token.attrGet('align') || ((options === null || options === void 0 ? void 0 : options.centerImages) ? 'center' : '');
if (align)
wrapperStyles.push("text-align: ".concat(align, ";"));
if (HILITE)
wrapperStyles.push(HILITE);
// ---- image styles ----
var imgStyles = [];
// height
if (RAW_HEIGHT) {
imgStyles.push("height: ".concat(RAW_HEIGHT, ";"));
}
var widthIsSet = false;
// width
// Support: 0.75\textwidth, \textwidth, 1\linewidth, etc.
// Grab the factor (can be empty), then \textwidth|\linewidth
var twMatch = RAW_WIDTH.match(consts_1.TEXTWIDTH_RE);
if (twMatch) {
var factor = Math.max(0, parseFloat((_a = twMatch[1]) !== null && _a !== void 0 ? _a : '1')) || 1;
var pct = Math.min(100, factor * 100);
imgStyles.push("width: ".concat(pct, "%;"));
widthIsSet = true;
}
else if (RAW_WIDTH) {
// Any other units (“300px”, “12cm”, “40%”, “10em”) — we give as is
imgStyles.push("width: ".concat(RAW_WIDTH, ";"));
widthIsSet = true;
}
// max width
// Support: 0.75\textwidth, \textwidth, 1\linewidth, etc.
// Grab the factor (can be empty), then \textwidth|\linewidth
var twMaxMatch = RAW_MAX_WIDTH.match(consts_1.TEXTWIDTH_RE);
if (twMaxMatch) {
var factor = Math.max(0, parseFloat((_b = twMaxMatch[1]) !== null && _b !== void 0 ? _b : '1')) || 1;
var pct = Math.min(100, factor * 100);
imgStyles.push("max-width: ".concat(pct, "%;"));
widthIsSet = true;
}
else if (RAW_MAX_WIDTH) {
// Any other units (“300px”, “12cm”, “40%”, “10em”) — we give as is
imgStyles.push("max-width: ".concat(RAW_MAX_WIDTH, ";"));
widthIsSet = true;
}
if (!widthIsSet) {
// Width not specified
/** max-width - prevent small images from being stretched */
imgStyles.push('max-width: 50%;');
}
var divStyleAttr = wrapperStyles.length ? " style=\"".concat(wrapperStyles.join(' '), "\"") : '';
var imgStyleAttr = imgStyles.length ? " style=\"".concat(imgStyles.join(' '), "\"") : '';
var srcAttr = SRC ? " src=\"".concat(SRC, "\"") : '';
var altAttr = " alt=\"".concat(escapeHtml(ALT), "\"");
return "<div class=\"figure_img\"".concat(divStyleAttr, "><").concat(TAG).concat(srcAttr).concat(altAttr).concat(imgStyleAttr).concat(TAG === 'img' ? '/' : '', ">").concat(TAG === 'img' ? '' : '</' + TAG + '>', "</div>");
};
exports.IncludeGraphics = IncludeGraphics;
//# sourceMappingURL=index.js.map