prosemirror-docx
Export a prosemirror document to a Microsoft Word file, using docx.
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).
Curvenote uses this to export from @curvenote/editor to word docs, but this library currently only has dependence on docx
, prosemirror-model
and buffer-image-size
- and similar to prosemirror-markdown
, the serialization schema can be edited externally (see Extended usage
below).
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);
await writeDocx(wordDocument, (buffer) => {
writeFileSync('HelloWorld.docx', buffer);
});
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
.
Planned: