Birch UI v0.1
Pricing

Markdown, HTML, RTF, Word 97–2003 and OpenDocumentDocument

Convert documents to and from Markdown and HTML, import RTF and Word 97–2003 (.doc) files, and read and write an OpenDocument subset with Birch UI Document Editor.

Open .md

Besides DOCX, the package converts six other formats. The converters are plain functions that take and return DocumentData; they need no DOM, so they run in the browser, in a worker and in Node.js.

TypeScript
import {
  documentToMarkdown,
  markdownToDocument,
  documentToHtml,
  htmlToDocument,
  rtfToDocument,
  docToDocument,
  documentToOdt,
  odtToDocument,
  ODT_MIME,
} from '@birchui/document';
Format Read Write
Markdown markdownToDocument(text) documentToMarkdown(data, { includeDeleted? })
HTML htmlToDocument(html), editor.importHtml(html) documentToHtml(data, { fullDocument?, title?, includeDeleted? }), editor.exportHtml(options)
RTF rtfToDocument(textOrBytes) not available
Word 97–2003 (.doc) docToDocument(bytes, { limits? }) not available
OpenDocument Text await odtToDocument(bytes) await documentToOdt(data) (a Uint8Array, MIME type ODT_MIME)

In the editor, File → Open and editor.openFile(file) read all of these formats and DOCX and pick the converter from the content of the file, and File → Download as and editor.exportFile(format) write them; see The File tab. To load a converted document yourself, pass it to setJSON:

TypeScript
editor.setJSON(markdownToDocument(await file.text()));

Markdown#

markdownToDocument reads CommonMark with the GitHub extensions for tables, task lists, strikethrough and footnotes. Headings become the Heading styles, fenced code becomes paragraphs with the Code style, inline code becomes monospaced text, and task list items start with ☐ or ☑.

documentToMarkdown writes the same subset back. Footnotes become [^1] references with their definitions at the end, page breaks become an HTML comment, and formatting Markdown cannot express (fonts, colours, alignment, page setup, headers and footers) is left out. Tracked insertions are written as normal text and tracked deletions are omitted unless includeDeleted is set.

HTML#

documentToHtml returns body markup with classes and inline styles, or with fullDocument: true a complete page with a stylesheet and the title. htmlToDocument accepts ordinary web markup and the clipboard HTML of Word and Google Docs, and keeps headings, lists, tables, links, pictures and character formatting. Pasting into the editor uses the same reader.

editor.importHtml(html) replaces the content as one undoable edit, and editor.exportHtml(options) exports the document that is open.

What the HTML export does with content that has no HTML counterpart:

  • A chart, SmartArt or other preserved drawing is written as its fallback picture when the DOCX file had one, otherwise as its text. An embedded object is written as its icon.
  • A merge field is written as its token, {{customer.name}}, not as Word's «customer.name», so that the HTML can be merged or read back.
  • A tab character is written as an em space ( ).
  • Text of a tracked deletion, including the source position of a tracked move, is left out unless you pass includeDeleted: true.
  • Links and picture addresses pass the allow-list of Addresses in imported files; a remote picture keeps its address in documentToHtml, because the result is yours to publish.

RTF#

rtfToDocument takes a string or bytes and reads text, character and paragraph formatting, lists, tables and code pages. Pictures and headers and footers in an RTF file are skipped, hyperlink fields become links and other fields keep their result as text. There is no RTF writer.

Word 97–2003 (.doc)#

docToDocument reads the binary format that Word 97 to Word 2003 wrote, and that LibreOffice and newer versions of Word still write as "Word 97–2003 document". It takes a Uint8Array or an ArrayBuffer, runs synchronously and needs no DOM. The reader is part of the package; nothing is sent to a server and no other program converts the file.

TypeScript
import { docToDocument, DocFormatError } from '@birchui/document';

try {
  editor.setJSON(docToDocument(new Uint8Array(await file.arrayBuffer())));
} catch (error) {
  if (error instanceof DocFormatError && error.code === 'encrypted') console.warn('The file has a password.');
  else throw error;
}
Read How it arrives
Text Both text encodings of the format (8-bit Windows-1252 and UTF-16), tabs, line breaks, non-breaking and optional hyphens
Character formatting Bold, italic, underline and its styles (double, thick, dotted, dashed, dot-dash, wavy, words only), strikethrough and double strikethrough, small capitals, all capitals, hidden text, superscript and subscript, font, size, colour and highlight
Paragraph formatting The built-in styles Title, Subtitle and Heading 1 to 6, alignment, left, right and first-line indents, space before and after, line spacing, borders, shading, tab stops with their alignment and leader, page break before, keep with next
Lists Bulleted and numbered lists with their levels, the number format (decimal, upper and lower Roman, upper and lower letters), the first number, and the round, hollow and square bullet
Tables Rows, cells, column widths, cells merged across columns and across rows, cell shading and vertical alignment, and tables inside cells
Pictures Inline and floating PNG and JPEG pictures with their size, as data: URLs; a floating picture keeps its position and its text wrapping
Shapes Rectangles, rounded rectangles, ellipses, lines and text boxes with their size, position, fill and outline. A shape the file stores as a free outline, which is how LibreOffice writes every shape, arrives as a rectangle
Links A HYPERLINK field becomes a link; every other field keeps its result as text
Footnotes and endnotes Each note arrives at the place of its reference, with its text
Comments The author and the text of each comment, and the text the comment is about. The format of Word 97 stores no usable date for a comment, so date is empty
Tracked changes Insertions and deletions with their author and their time to the minute
Sections Paper size, orientation, margins, header and footer distance, columns, the kind of break and different first page of every section; sections after the first arrive as section_break nodes
Headers and footers The header and the footer of every section, the first-page pair when the section uses one, and the even-page pair when it differs
Fast-saved files A file that Word saved with Allow fast saves keeps its changes as extra pieces of text with their own formatting changes. The pieces are read in document order and their changes are applied

The font and size of the Normal style become the document defaults. Formatting that a built-in style carries is not repeated on the text, so a Heading 1 paragraph takes the look of the editor's Heading 1 style. A paragraph in any other style keeps its own formatting as direct formatting.

Not read: pictures stored as metafiles (WMF, EMF) or bitmaps (BMP), shapes other than the ones above, group shapes, WordArt and charts, cell borders (a table has all its borders or none), character borders and shading, bookmarks, fields other than hyperlinks (their result is kept as text), formatting changes and moves among the tracked changes, replies to comments, footnote separators, and text boxes in headers. A manual page break is read as a page break; LibreOffice writes one as an empty paragraph followed by a paragraph with page break before, and that is how it arrives. There is no .doc writer.

Every failure is a DocFormatError with a code:

code Meaning
not-a-doc The bytes are not a compound file, or the file holds no Word document (an Excel workbook, for example)
encrypted The document is protected with a password
unsupported-version The file was written by Word 6 or Word 95
too-large The file is larger than limits.maxBytes
too-many-characters The body has more characters than limits.maxCharacters
corrupt A structure of the file is cut short, loops or points outside the file

The reader checks every offset it takes from the file against the stream it reads from, so a damaged or hostile file ends in one of these errors or in a shorter document, never in a read outside the file or a loop without end. limits bounds the work:

Limit Default Effect
maxBytes 256 MiB A larger file is refused with too-large
maxCharacters 10,000,000 A longer body is refused with too-many-characters
maxPictureBytes 64 MiB Pictures past the total are left out
maxTableCells 100,000 A table with more cells is read as paragraphs

DEFAULT_DOC_LIMITS holds the defaults. The unit tests read seven files written by LibreOffice and files built byte by byte in the test (pieces with their own property changes, as a fast save leaves them), and 5,600 damaged variants of them (cut short, with overwritten offsets, with bytes flipped). Each variant gives a document or a DocFormatError. The reader has not been run against files written by Word itself.

OpenDocument Text#

The OpenDocument converter covers a subset: paragraphs and headings, character formatting, lists, tables, pictures, footnotes, the page setup, and one header and one footer. Tracked changes, comments, sections, content controls, shapes and fields other than page numbers are not converted. Use DOCX when a document has to round-trip without loss.

↑ ↓ to move, Enter to open, Esc to close