UNPKG

prosemirror-docx

Version:

Export from a prosemirror document to Microsoft word

77 lines 2.76 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 { Document, Packer, SectionType, } from 'docx'; export function createShortId() { return Math.random().toString(36).slice(2, 11); } export 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: SectionType.CONTINUOUS, }, headers: section.config.headers, footers: section.config.footers, children: section.children, })); if (!sections) { sections = [ { headers: undefined, footers: undefined, properties: { type: SectionType.CONTINUOUS, }, children: (state === null || state === void 0 ? void 0 : state.children) || [], }, ]; } const doc = new Document(Object.assign({ footnotes: state.footnotes, numbering: { config: state.numbering, }, sections }, (opts || {}))); return doc; } /** * @deprecated - use `buildDoc` instead * Creates a docx document from the given state. * */ export function createDocFromState(state) { return buildDoc({ numbering: state.numbering, sections: [ { config: {}, children: state.children, }, ], footnotes: state.footnotes, }); } export function writeDocx(doc, /** * @deprecated use `.then()` or `await` instead */ write) { return __awaiter(this, void 0, void 0, function* () { const buffer = yield Packer.toBuffer(doc); yield (write === null || write === void 0 ? void 0 : write(buffer)); return buffer; }); } export 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; } //# sourceMappingURL=utils.js.map