UNPKG

prosemirror-docx

Version:

Export from a prosemirror document to Microsoft word

85 lines 3.13 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLatexFromNode = exports.writeDocx = exports.createDocFromState = exports.buildDoc = exports.createShortId = void 0; const docx_1 = require("docx"); function createShortId() { return Math.random().toString(36).slice(2, 11); } exports.createShortId = createShortId; function buildDoc(state, opts) { var _a; let sections = (_a = state === null || state === void 0 ? void 0 : state.sections) === null || _a === void 0 ? void 0 : _a.map((section) => ({ properties: section.config.properties || { type: docx_1.SectionType.CONTINUOUS, }, headers: section.config.headers, footers: section.config.footers, children: section.children, })); if (!sections) { sections = [ { headers: undefined, footers: undefined, properties: { type: docx_1.SectionType.CONTINUOUS, }, children: (state === null || state === void 0 ? void 0 : state.children) || [], }, ]; } const doc = new docx_1.Document(Object.assign({ footnotes: state.footnotes, numbering: { config: state.numbering, }, sections }, (opts || {}))); return doc; } exports.buildDoc = buildDoc; /** * @deprecated - use `buildDoc` instead * Creates a docx document from the given state. * */ function createDocFromState(state) { return buildDoc({ numbering: state.numbering, sections: [ { config: {}, children: state.children, }, ], footnotes: state.footnotes, }); } exports.createDocFromState = createDocFromState; function writeDocx(doc, /** * @deprecated use `.then()` or `await` instead */ write) { return __awaiter(this, void 0, void 0, function* () { const buffer = yield docx_1.Packer.toBuffer(doc); yield (write === null || write === void 0 ? void 0 : write(buffer)); return buffer; }); } exports.writeDocx = writeDocx; function getLatexFromNode(node) { let math = ''; node.forEach((child) => { if (child.isText) math += child.text; // TODO: improve this as we may have other things in the future }); return math; } exports.getLatexFromNode = getLatexFromNode; //# sourceMappingURL=utils.js.map