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)
177 lines • 7.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubTabular = exports.pushSubTabular = exports.getSubTabularBracket = exports.ClearSubTableLists = void 0;
var tslib_1 = require("tslib");
var common_1 = require("./common");
var consts_1 = require("../../common/consts");
var sub_cell_1 = require("./sub-cell");
var sub_code_1 = require("./sub-code");
var placeholder_utils_1 = require("./placeholder-utils");
var subTabular = new Map();
// Guards direct lookup: keys are generateUniqueId() UUIDs, cell text should match exactly.
var SUB_TABULAR_KEY_RE = new RegExp("^".concat(consts_1.uuidPatternNoCapture, "$"));
var ClearSubTableLists = function () {
subTabular.clear();
};
exports.ClearSubTableLists = ClearSubTableLists;
// Bracket of the nested tabular referenced by the placeholder `<<id>>` (or bare uuid).
var getSubTabularBracket = function (placeholderOrId) {
var _a;
var id = placeholderOrId.replace(consts_1.ANGLE_BRACKETS_RE, '');
return (_a = subTabular.get(id)) === null || _a === void 0 ? void 0 : _a.bracket;
};
exports.getSubTabularBracket = getSubTabularBracket;
/**
* Extracts child placeholder IDs from a sub-tabular content string.
* Returns a list of raw placeholder IDs without angle brackets.
*/
var extractChildIds = function (content) {
var _a, _b;
var placeholderMatches = (_b = (_a = content.match(consts_1.doubleAngleBracketUuidPattern)) !== null && _a !== void 0 ? _a : content.match(consts_1.singleAngleBracketPattern)) !== null && _b !== void 0 ? _b : [];
return placeholderMatches
.map(function (match) { return match.replace(consts_1.ANGLE_BRACKETS_RE, ""); })
.filter(function (s) { return s.length > 0; });
};
var pushSubTabular = function (str, subTabularContent, subRes, posBegin, posEnd, i, level, bracket) {
var e_1, _a;
var _b;
if (subRes === void 0) { subRes = []; }
if (posBegin === void 0) { posBegin = 0; }
if (i === void 0) { i = 0; }
if (level === void 0) { level = 0; }
var id = (0, common_1.generateUniqueId)();
var childIds = extractChildIds(subTabularContent);
try {
for (var childIds_1 = tslib_1.__values(childIds), childIds_1_1 = childIds_1.next(); !childIds_1_1.done; childIds_1_1 = childIds_1.next()) {
var childId = childIds_1_1.value;
var child = subTabular.get(childId);
if (child) {
((_b = child.parents) !== null && _b !== void 0 ? _b : (child.parents = [])).push(id);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (childIds_1_1 && !childIds_1_1.done && (_a = childIds_1.return)) _a.call(childIds_1);
}
finally { if (e_1) throw e_1.error; }
}
var isBlockLocal = (0, common_1.detectLocalBlock)(subTabularContent);
var childBlock = childIds.some(function (cid) {
var child = subTabular.get(cid);
return child ? !!child.isBlock : false;
});
subTabular.set(id, {
content: subTabularContent,
parsed: subRes,
children: childIds,
isBlock: isBlockLocal || childBlock,
bracket: bracket,
});
if (posBegin > 0) {
return str.slice(i, posBegin) + "<<".concat(id, ">>") + str.slice(posEnd + '\\end{tabular}'.length, str.length);
}
else {
return "<<".concat(id, ">>") + str.slice(posEnd + '\\end{tabular}'.length, str.length);
}
};
exports.pushSubTabular = pushSubTabular;
/**
* Expands <...> / <<...>> placeholders inside a tabular cell by replacing them with cached
* sub-tabular content (or diagbox fallback). If injected content contains a list begin
* (or other block-ish LaTeX), it may be newline-wrapped to keep downstream block parsing stable.
*/
var getSubTabular = function (sub, i, isCell, forLatex) {
var _a, _b;
if (isCell === void 0) { isCell = true; }
if (forLatex === void 0) { forLatex = false; }
// first expand any extracted code-block placeholders (may add newlines)
sub = (0, sub_code_1.getExtractedCodeBlockContent)(sub, 0);
sub = sub.trim();
if (isCell) {
sub = (0, common_1.getContent)(sub);
}
if (SUB_TABULAR_KEY_RE.test(sub)) {
var directEntry = subTabular.get(sub);
if ((_a = directEntry === null || directEntry === void 0 ? void 0 : directEntry.parsed) === null || _a === void 0 ? void 0 : _a.length) {
return directEntry.parsed;
}
}
// find placeholders
var cellM = (0, placeholder_utils_1.findPlaceholders)(sub, i);
if (!cellM) {
return null;
}
var parents = null;
var cursor = 0;
var contentFragments = [];
var hasDiagbox = false;
for (var j = 0; j < cellM.length; j++) {
var placeholder = cellM[j];
var id = (0, placeholder_utils_1.placeholderToId)(placeholder);
if (!id) {
continue;
}
var start = sub.indexOf(placeholder, cursor);
if (start === -1) {
continue;
}
var end = start + placeholder.length;
// prefix text between placeholders
var prefix = sub.slice(cursor, start);
// Avoid trimming around list-begin tokens to keep `\begin{itemize}` detectable.
var entry = subTabular.get(id);
var isBlockRule = false;
if (entry) {
isBlockRule = !!entry.isBlock || (0, common_1.detectLocalBlock)(prefix) || (0, common_1.detectLocalBlock)(entry.content);
if (!isBlockRule || prefix.trim() === "") {
prefix = prefix.trim();
}
}
else {
isBlockRule = (0, common_1.detectLocalBlock)(prefix);
if (!isBlockRule || prefix.trim() === "") {
prefix = prefix.trim();
}
}
var injected = "";
if (entry) {
parents = entry.parents;
injected = (_b = entry.content) !== null && _b !== void 0 ? _b : "";
}
else {
var diagboxMatch = (0, sub_cell_1.findInDiagboxTable)(id);
if (diagboxMatch) {
hasDiagbox = true;
injected = diagboxMatch;
}
}
// decide wrapping using non-space neighbors around placeholder
var _c = (0, placeholder_utils_1.getInlineContextAroundSpan)(sub, start, end), beforeNonSpace = _c.beforeNonSpace, afterNonSpace = _c.afterNonSpace;
// If injected content starts a list env, wrap with newlines so block parsing stays stable
if (isBlockRule) {
injected = (0, placeholder_utils_1.wrapWithNewlinesIfInline)(injected, beforeNonSpace, afterNonSpace);
}
var st = prefix + injected;
contentFragments.push(st);
cursor = end;
}
if (cursor < sub.length) {
contentFragments.push(sub.slice(cursor));
}
return [
{
token: 'inline',
tag: '',
n: 0,
content: contentFragments.join(''),
type: forLatex ? "inline" : "subTabular",
parents: parents,
isSubTabular: true,
hasDiagbox: hasDiagbox,
},
];
};
exports.getSubTabular = getSubTabular;
//# sourceMappingURL=sub-tabular.js.map