UNPKG

prosemirror-docx

Version:

Export from a prosemirror document to Microsoft word

216 lines 6.72 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { HeadingLevel, ShadingType } from 'docx'; import { DocxSerializer, DocxSerializerAsync, } from './serializer'; import { getLatexFromNode } from './utils'; export const defaultNodes = { text(state, node) { var _a; state.text((_a = node.text) !== null && _a !== void 0 ? _a : ''); }, paragraph(state, node) { state.renderInline(node); state.closeBlock(node); }, heading(state, node) { state.renderInline(node); const heading = [ HeadingLevel.HEADING_1, HeadingLevel.HEADING_2, HeadingLevel.HEADING_3, HeadingLevel.HEADING_4, HeadingLevel.HEADING_5, HeadingLevel.HEADING_6, ][node.attrs.level - 1]; state.closeBlock(node, { heading }); }, blockquote(state, node) { state.renderContent(node, { style: 'IntenseQuote' }); }, code_block(state, node) { // TODO: something for code state.renderContent(node); state.closeBlock(node); }, horizontal_rule(state, node) { // Kinda hacky, but this works to insert two paragraphs, the first with a break state.closeBlock(node, { thematicBreak: true }); state.closeBlock(node); }, hard_break(state) { state.addRunOptions({ break: 1 }); }, ordered_list(state, node) { state.renderList(node, 'numbered'); }, bullet_list(state, node) { state.renderList(node, 'bullets'); }, list_item(state, node) { state.renderListItem(node); }, // Presentational image(state, node) { const { src } = node.attrs; state.image(src); state.closeBlock(node); }, // Technical math(state, node) { state.math(getLatexFromNode(node), { inline: true }); }, equation(state, node) { const { id, numbered } = node.attrs; state.math(getLatexFromNode(node), { inline: false, numbered, id }); state.closeBlock(node); }, table(state, node) { state.table(node); }, }; export const defaultAsyncNodes = { text(state, node) { var _a; state.text((_a = node.text) !== null && _a !== void 0 ? _a : ''); }, paragraph(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.renderInline(node); state.closeBlock(node); }); }, heading(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.renderInline(node); const heading = [ HeadingLevel.HEADING_1, HeadingLevel.HEADING_2, HeadingLevel.HEADING_3, HeadingLevel.HEADING_4, HeadingLevel.HEADING_5, HeadingLevel.HEADING_6, ][node.attrs.level - 1]; state.closeBlock(node, { heading }); }); }, blockquote(state, node) { state.renderContent(node, { style: 'IntenseQuote' }); }, code_block(state, node) { // TODO: something for code state.renderContent(node); state.closeBlock(node); }, horizontal_rule(state, node) { // Kinda hacky, but this works to insert two paragraphs, the first with a break state.closeBlock(node, { thematicBreak: true }); state.closeBlock(node); }, hard_break(state) { state.addRunOptions({ break: 1 }); }, ordered_list(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.renderList(node, 'numbered'); }); }, bullet_list(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.renderList(node, 'bullets'); }); }, list_item(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.renderListItem(node); }); }, // Presentational image(state, node) { return __awaiter(this, void 0, void 0, function* () { const { src } = node.attrs; yield state.image(src); state.closeBlock(node); }); }, // Technical math(state, node) { state.math(getLatexFromNode(node), { inline: true }); }, equation(state, node) { const { id, numbered } = node.attrs; state.math(getLatexFromNode(node), { inline: false, numbered, id }); state.closeBlock(node); }, table(state, node) { return __awaiter(this, void 0, void 0, function* () { yield state.table(node); }); }, }; export const defaultMarks = { em() { return { italics: true }; }, strong() { return { bold: true }; }, italic() { return { italics: true }; }, bold() { return { bold: true }; }, link() { // Note, this is handled specifically in the serializer // Word treats links more like a Node rather than a mark return {}; }, code() { return { font: { name: 'Monospace', }, color: '000000', shading: { type: ShadingType.SOLID, color: 'D2D3D2', fill: 'D2D3D2', }, }; }, abbr() { // TODO: abbreviation return {}; }, subscript() { return { subScript: true }; }, superscript() { return { superScript: true }; }, strikethrough() { // doubleStrike! return { strike: true }; }, underline() { return { underline: {}, }; }, smallcaps() { return { smallCaps: true }; }, allcaps() { return { allCaps: true }; }, }; export const defaultDocxSerializer = new DocxSerializer(defaultNodes, defaultMarks); export const defaultDocxSerializerAsync = new DocxSerializerAsync(defaultAsyncNodes, defaultMarks); //# sourceMappingURL=schema.js.map