UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

111 lines 4.66 kB
import { NodeGroups } from './types'; import { getAttr, getNumberedAttrs, getNumberedDefaultAttrs, normalizeLabel, readBooleanAttr, setNumberedAttrs, } from './utils'; import { writeDirectiveOptions } from '../serialize/markdown/utils'; const DEFAULT_NUMBERED = false; const equation = { group: NodeGroups.top, // Content can have display elements inside of it for dynamic equations content: `(${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({}, getNumberedDefaultAttrs()), { title: { default: '' } }), toDOM: (node) => { const { title } = node.attrs; return ['r-equation', Object.assign(Object.assign({}, setNumberedAttrs(node.attrs)), { title: title || undefined }), 0]; }, parseDOM: [ { tag: 'r-equation:not([inline])', getAttrs(dom) { return Object.assign(Object.assign({}, getNumberedAttrs(dom)), { title: 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' }, normalizeLabel(localizedId)), { enumerated: readBooleanAttr(props.numbered), value: props.children[0].value || '' }); } throw new Error(`Equation node does not have one child`); }, }; export const equationNoDisplay = Object.assign(Object.assign({}, equation), { content: `${NodeGroups.text}*` }); export 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'); 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); }; export 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); }; export default equation; //# sourceMappingURL=equation.js.map