UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

153 lines 6.09 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTex = exports.texSerializer = void 0; const prosemirror_markdown_1 = require("prosemirror-markdown"); const utils_1 = require("./utils"); const nodes = __importStar(require("../../nodes")); const utils_2 = require("../markdown/utils"); const types_1 = require("../../types"); const types_2 = require("../types"); const indent_1 = require("../indent"); function createMarkOpenClose(name) { return { open: name ? `\\${name}{` : '', close: name ? '}' : '', }; } exports.texSerializer = new prosemirror_markdown_1.MarkdownSerializer({ text(state, node, parent) { var _a, _b; let escaped; if (parent.type.name === types_1.nodeNames.equation || parent.type.name === types_1.nodeNames.math || parent.type.name === types_1.nodeNames.code_block) { escaped = (0, utils_1.stringToLatexMath)((_a = node.text) !== null && _a !== void 0 ? _a : ''); } else { escaped = (0, utils_1.stringToLatexText)((_b = node.text) !== null && _b !== void 0 ? _b : ''); } state.text(escaped, false); }, paragraph(state, node) { state.renderInline(node); state.closeBlock(node); }, heading: nodes.Heading.toTex, blockquote: (0, utils_1.createLatexStatement)('quote', (state, node) => { state.renderContent(node); }), code_block: nodes.Code.toTex, link_block: nodes.LinkBlock.toTex, horizontal_rule(state, node) { state.write('\n\\bigskip\n\\centerline{\\rule{13cm}{0.4pt}}\n\\bigskip'); state.closeBlock(node); }, hard_break(state, node, parent, index) { for (let i = index + 1; i < parent.childCount; i += 1) { if (parent.child(i).type !== node.type) { state.write('\\\\\n'); return; } } }, ordered_list: (0, utils_1.createLatexStatement)((state, node) => ({ command: 'enumerate', bracketOpts: node.attrs.order !== 1 ? 'resume' : undefined, }), (state, node) => { state.renderList(node, (0, indent_1.getIndent)(state), () => '\\item '); }), bullet_list: (0, utils_1.createLatexStatement)((state) => { if (state.isInTable) return null; return { command: 'itemize' }; }, (state, node) => { if (state.isInTable) { node.forEach((child) => { state.write('\\textbullet~~'); state.renderInline(child); state.write('\\newline'); state.ensureNewLine(); }); } else { state.renderList(node, (0, indent_1.getIndent)(state), () => '\\item '); } }), list_item(state, node) { state.renderInline(node); }, image: nodes.Image.toTex, figure: nodes.Figure.toTex, figcaption: nodes.Figcaption.toTex, footnote: nodes.Footnote.toTex, iframe: utils_1.blankTexLines, time: nodes.Time.toTex, cite: nodes.Cite.toTex, cite_group: nodes.CiteGroup.toTex, mention: nodes.Mention.toTex, math: nodes.Math.toTex, equation: nodes.Equation.toTex, table: nodes.Table.toTex, // \usepackage{framed} callout: nodes.Callout.toTex, aside: nodes.Aside.toTex, variable: utils_1.blankTexLines, display: utils_1.blankTex, dynamic: utils_1.blankTex, range: utils_1.blankTex, switch: utils_1.blankTex, button: utils_1.blankTex, }, { em: Object.assign(Object.assign({}, createMarkOpenClose('textit')), { mixable: true, expelEnclosingWhitespace: true }), strong: Object.assign(Object.assign({}, createMarkOpenClose('textbf')), { mixable: true, expelEnclosingWhitespace: true }), underline: Object.assign(Object.assign({}, createMarkOpenClose('uline')), { mixable: true }), link: { open(state, mark, parent, index) { var _a, _b; const { options } = state; const href = (_b = (_a = options.localizeLink) === null || _a === void 0 ? void 0 : _a.call(options, mark.attrs.href)) !== null && _b !== void 0 ? _b : mark.attrs.href; return (0, utils_2.isPlainURL)(mark, href, parent, index, 1) ? '\\url{' : `\\href{${href}}{`; }, close() { // TODO: no title? mark.attrs.title return '}'; }, }, code: createMarkOpenClose('texttt'), // https://www.overleaf.com/learn/latex/glossaries // \newacronym{gcd}{GCD}{Greatest Common Divisor} abbr: createMarkOpenClose(), subscript: createMarkOpenClose('textsubscript'), superscript: createMarkOpenClose('textsuperscript'), // \usepackage[normalem]{ulem} strikethrough: createMarkOpenClose('sout'), }); function toTex(doc, opts) { const defualtOpts = { tightLists: true, format: types_2.TexFormatTypes.tex }; return exports.texSerializer.serialize(doc, Object.assign(Object.assign({}, defualtOpts), opts)); } exports.toTex = toTex; //# sourceMappingURL=index.js.map