JSPM

  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q53841F
  • License MIT

Export from a prosemirror document to Microsoft word forked from curvenote/prosemirror-docx

Package Exports

  • prosemirror-docx-web
  • prosemirror-docx-web/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (prosemirror-docx-web) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

prosemirror-docx-web

This is a fork from prosemirror-docx

Export a prosemirror document to a Microsoft Word file, using docx.

image

Overview

prosemirror-docx has a similar structure to prosemirror-markdown, with a DocxSerializerState object that you write to as you walk the document. It is a light wrapper around https://docx.js.org/, which actually does the export. Currently prosemirror-docx is write only (i.e. can export to, but can’t read from *.docx), and has most of the basic nodes covered (see below).

Basic usage

import { defaultDocxSerializer, writeDocx } from 'prosemirror-docx';
import { EditorState } from 'prosemirror-state';
import { writeFileSync } from 'fs'; // Or some other way to write a file

// Set up your prosemirror state/document as you normally do
const state = EditorState.create({ schema: mySchema });

// If there are images, we will need to preload the buffers
const opts = {
  getImageBuffer(src: string) {
    return anImageBuffer;
  },
};

// Create a doc in memory, and then write it to disk
const wordDocument = defaultDocxSerializer.serialize(state.doc, opts);

writeDocx(wordDocument, (buffer) => {
  writeFileSync('HelloWorld.docx', buffer);
});

Extended usage

Instead of using the defaultDocxSerializer you can override or provide cusome serializers.

import { DocxSerializer, defaultNodes, defaultMarks } from 'prosemirror-docx';

const nodeSerializer = {
  ...defaultNodes,
  my_paragraph(state, node) {
    state.renderInline(node);
    state.closeBlock(node);
  },
};

export const myDocxSerializer = new DocxSerializer(nodeSerializer, defaultMarks);

The state is the DocxSerializerState and has helper methods to interact with docx.

Supported Nodes

  • text
  • paragraph
  • heading (levels)
    • TODO: Support numbering of headings
  • blockquote
    • TODO: No styles supported
  • code_block
    • TODO: No styles supported
  • horizontal_rule
  • hard_break
  • ordered_list
  • unordered_list
  • list_item
  • image
  • math
  • equations (numbered & unnumbered)

Planned:

  • Tables
  • Internal References (e.g. see Table 1)

Supported Marks

  • em
  • strong
  • link
    • Note: this is actually treated as a node in docx, so ignored as a prosemirror mark, but supported.
  • code
  • subscript
  • superscript
  • strikethrough
  • underline
  • smallcaps
  • allcaps

Resources