UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

160 lines 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTex = exports.toMarkdown = void 0; const types_1 = require("./types"); const utils_1 = require("./utils"); const cite = { attrs: { key: { default: null }, title: { default: '' }, kind: { default: null }, label: { default: null }, // TODO: This has been renamed to text - need to deprecate inline: { default: null }, text: { default: '' }, }, inline: true, marks: '', group: types_1.NodeGroups.inline, draggable: true, parseDOM: [ { tag: 'cite', getAttrs(dom) { var _a, _b, _c, _d, _e; return { key: (_b = (_a = dom.getAttribute('key')) !== null && _a !== void 0 ? _a : dom.getAttribute('data-key')) !== null && _b !== void 0 ? _b : dom.getAttribute('data-cite'), title: (_c = dom.getAttribute('title')) !== null && _c !== void 0 ? _c : '', kind: dom.getAttribute('kind') || 'cite', label: dom.getAttribute('label') || null, // inline is for legacy inline: undefined, // `text` is the rendered text e.g. "Jon et. al, 2020" OR is "Table %s" text: (_e = (_d = dom.getAttribute('inline')) !== null && _d !== void 0 ? _d : dom.textContent) !== null && _e !== void 0 ? _e : '', }; }, }, ], toDOM(node) { const { key, kind, text, label, title } = node.attrs; return [ 'cite', { key: key || undefined, kind: kind !== null && kind !== void 0 ? kind : 'cite', title: title || undefined, label: label || undefined, // Should we remove this?!?! }, text || '', ]; }, attrsFromMyst: (node) => { var _a, _b, _c, _d; if (node.type === 'crossReference') { const crossRef = node; return { key: (_a = crossRef.identifier) !== null && _a !== void 0 ? _a : null, kind: types_1.ReferenceKind.fig, label: null, title: null, inline: undefined, text: ((_c = (_b = node.children) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.value) || node.label || node.identifier || '', }; } return { key: (_d = node.identifier) !== null && _d !== void 0 ? _d : null, kind: types_1.ReferenceKind.cite, label: null, title: null, inline: undefined, text: (0, utils_1.flattenValues)(node), }; }, toMyst: (props, options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; if (props.kind === types_1.ReferenceKind.cite) { const citeKey = (_d = (_c = (_a = options.localizeCitation) === null || _a === void 0 ? void 0 : _a.call(options, (_b = props.key) !== null && _b !== void 0 ? _b : '')) !== null && _c !== void 0 ? _c : props.key) !== null && _d !== void 0 ? _d : ''; const { identifier, label } = (_e = (0, utils_1.normalizeLabel)(citeKey)) !== null && _e !== void 0 ? _e : {}; return { type: 'cite', identifier, label, }; } const localizedId = (_j = (_h = (_f = options.localizeId) === null || _f === void 0 ? void 0 : _f.call(options, (_g = props.key) !== null && _g !== void 0 ? _g : '')) !== null && _h !== void 0 ? _h : props.key) !== null && _j !== void 0 ? _j : ''; const { identifier, label } = (_k = (0, utils_1.normalizeLabel)(localizedId)) !== null && _k !== void 0 ? _k : {}; return { type: 'crossReference', identifier, label, children: [{ type: 'text', value: props.text || '' }], }; }, }; function getPrependedText(kind) { switch (kind) { case types_1.ReferenceKind.sec: return 'Section'; case types_1.ReferenceKind.fig: return 'Figure'; case types_1.ReferenceKind.eq: return 'Equation'; case types_1.ReferenceKind.table: return 'Table'; case types_1.ReferenceKind.code: return 'Program'; default: return ''; } } const toMarkdown = (state, node) => { var _a, _b, _c, _d, _e, _f, _g; const { kind, key } = node.attrs; const id = (_c = (_b = (_a = state.options).localizeId) === null || _b === void 0 ? void 0 : _b.call(_a, key !== null && key !== void 0 ? key : '')) !== null && _c !== void 0 ? _c : key; switch (kind) { case types_1.ReferenceKind.cite: { const citeKey = (_g = (_f = (_e = (_d = state.options).localizeCitation) === null || _e === void 0 ? void 0 : _e.call(_d, key !== null && key !== void 0 ? key : '')) !== null && _f !== void 0 ? _f : key) !== null && _g !== void 0 ? _g : ''; if (state.nextCitationInGroup) { state.write(state.nextCitationInGroup > 1 ? `${citeKey}; ` : citeKey); state.nextCitationInGroup -= 1; } else { state.write(`{cite:t}\`${citeKey}\``); } return; } case types_1.ReferenceKind.eq: state.write(`{eq}\`${id}\``); return; case types_1.ReferenceKind.sec: case types_1.ReferenceKind.fig: case types_1.ReferenceKind.table: case types_1.ReferenceKind.code: default: state.write(`{numref}\`${getPrependedText(kind)} %s <${id}>\``); } }; exports.toMarkdown = toMarkdown; const toTex = (state, node) => { var _a, _b, _c, _d, _e, _f, _g; const { kind, key } = node.attrs; let prepend = getPrependedText(kind); if (kind === types_1.ReferenceKind.cite) { const citeKey = (_d = (_c = (_b = (_a = state.options).localizeCitation) === null || _b === void 0 ? void 0 : _b.call(_a, key !== null && key !== void 0 ? key : '')) !== null && _c !== void 0 ? _c : key) !== null && _d !== void 0 ? _d : ''; if (state.nextCitationInGroup) { state.write(state.nextCitationInGroup > 1 ? `${citeKey}, ` : citeKey); state.nextCitationInGroup -= 1; } else { state.write(`\\cite{${citeKey}}`); } return; } prepend = prepend ? `${prepend}~` : prepend; const id = (_g = (_f = (_e = state.options).localizeId) === null || _f === void 0 ? void 0 : _f.call(_e, key !== null && key !== void 0 ? key : '')) !== null && _g !== void 0 ? _g : key; if (id) state.write(`${prepend}\\ref{${id}}`); }; exports.toTex = toTex; exports.default = cite; //# sourceMappingURL=cite.js.map