UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

116 lines 4.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTex = exports.toMarkdown = exports.equationNoDisplay = void 0; const types_1 = require("./types"); const utils_1 = require("./utils"); const utils_2 = require("../serialize/markdown/utils"); const DEFAULT_NUMBERED = false; const equation = { group: types_1.NodeGroups.top, // Content can have display elements inside of it for dynamic equations content: `(${types_1.NodeGroups.text} | display)*`, draggable: false, marks: '', // The view treat the node as a leaf, even though it technically has content atom: true, whitespace: 'pre', code: true, attrs: Object.assign(Object.assign({}, (0, utils_1.getNumberedDefaultAttrs)()), { title: { default: '' } }), toDOM: (node) => { const { title } = node.attrs; return ['r-equation', Object.assign(Object.assign({}, (0, utils_1.setNumberedAttrs)(node.attrs)), { title: title || undefined }), 0]; }, parseDOM: [ { tag: 'r-equation:not([inline])', getAttrs(dom) { return Object.assign(Object.assign({}, (0, utils_1.getNumberedAttrs)(dom)), { title: (0, utils_1.getAttr)(dom, 'title') }); }, }, ], attrsFromMyst: (token) => { var _a; return ({ id: token.identifier || null, label: null, numbered: (_a = token.enumerated) !== null && _a !== void 0 ? _a : DEFAULT_NUMBERED, title: '', }); }, toMyst: (props, options) => { var _a, _b, _c, _d, _e; if (((_a = props.children) === null || _a === void 0 ? void 0 : _a.length) === 1 && props.children[0].type === 'text') { const localizedId = (_e = (_d = (_b = options.localizeId) === null || _b === void 0 ? void 0 : _b.call(options, (_c = props.id) !== null && _c !== void 0 ? _c : '')) !== null && _d !== void 0 ? _d : props.id) !== null && _e !== void 0 ? _e : ''; return Object.assign(Object.assign({ type: 'math' }, (0, utils_1.normalizeLabel)(localizedId)), { enumerated: (0, utils_1.readBooleanAttr)(props.numbered), value: props.children[0].value || '' }); } throw new Error(`Equation node does not have one child`); }, }; exports.equationNoDisplay = Object.assign(Object.assign({}, equation), { content: `${types_1.NodeGroups.text}*` }); const toMarkdown = (state, node) => { var _a, _b, _c; const { numbered, id } = node.attrs; const localId = (_c = (_b = (_a = state.options).localizeId) === null || _b === void 0 ? void 0 : _b.call(_a, id !== null && id !== void 0 ? id : '')) !== null && _c !== void 0 ? _c : id; const amsBegin = node.textContent.startsWith('\\begin{'); const amsEnd = node.textContent.match(/\\end{([a-z*]+)}$/); // TODO: this should be more specific, see amsmath const aligned = node.textContent.startsWith('\\begin{aligned}'); const ams = amsBegin && amsEnd && !aligned; if (ams) { state.text(node.textContent, false); } else if (numbered) { state.write('```{math}\n'); (0, utils_2.writeDirectiveOptions)(state, { label: localId }); state.text(node.textContent, false); state.ensureNewLine(); state.write('```'); } else if (node.textContent.includes('\n')) { // New lines in the $$ state.write('$$\n'); state.text(node.textContent, false); state.ensureNewLine(); state.write('$$'); } else { state.write('$$'); state.text(node.textContent, false); state.write('$$'); } state.closeBlock(node); }; exports.toMarkdown = toMarkdown; const toTex = (state, node) => { var _a, _b, _c; const { numbered, id } = node.attrs; const localId = (_c = (_b = (_a = state.options).localizeId) === null || _b === void 0 ? void 0 : _b.call(_a, id !== null && id !== void 0 ? id : '')) !== null && _c !== void 0 ? _c : id; const text = node.textContent.trim(); const amsBegin = text.startsWith('\\begin{'); const amsEnd = text.match(/\\end{([a-z*]+)}$/); const ams = amsBegin && amsEnd; if (state.isInTable) { // NOTE: if this is also AMS, this will likely fail. state.write('\\(\\displaystyle '); state.renderInline(node); state.write(' \\)'); } else if (ams) { state.renderInline(node); } else { state.write('\\begin{equation}\n'); if (numbered && id) { state.write(`\\label{${localId}}`); } state.ensureNewLine(); state.renderInline(node); state.ensureNewLine(); state.write('\\end{equation}'); } if (!state.isInTable) state.closeBlock(node); }; exports.toTex = toTex; exports.default = equation; //# sourceMappingURL=equation.js.map