Code Monkey home page Code Monkey logo

docxml's Introduction

DOCX Markup Language

This is a Deno module for making .docx files from scratch or from an existing DOCX/DOTX template. It will probably be published for NodeJS soonβ„’ too.

docxml is designed to be used in vanilla JavaScript using class instances, or using JSX if you're on Deno or want to use a transpiler like Babel:

const para = new Paragraph(
	{ alignment: 'center' },
	new Text({}, 'I want a cookie')
);
/** @jsx Docx.jsx */
const para = (
	<Paragraph alignment="center">
		<Text>I want a cookie</Text>
	</Paragraph>
);

πŸ‘‰ More on using class instances

πŸ‘‰ More on using JSX

docxml is also designed to be used from scratch/entirely programmatically, or using a more ergonomic API to transform from an XML document. Both modes work equally well with vanilla JS or JSX.

await Docx.fromJsx(
	<Paragraph alignment="center">
		<Text>I want a cookie</Text>
	</Paragraph>,
).toFile('example-1.docx');
await Docx.fromNothing()
	.withXmlRule(
		'self::text()',
		({ node }) => <Text>{node.nodeValue}</Text>
	)
	.withXmlRule(
		'self::p',
		({ traverse, node }) => (
			<Paragraph alignment={node.getAttribute('align')}>
				{traverse()}
			</Paragraph>
		)
	)
	.withXml(`<p align="center">I want a cookie</p>`, {})
	.toFile('example-2.docx');

πŸ‘‰ More on XML rendering rules

πŸ‘‰ Go to the API docs that Deno generates for docxml

Features

To great or small extend, the following features work in the current version of docxml. Some items are not ticked off yet -- they are not available, but hopefully soon.

πŸ‘‰ See code examples of some or the more intricate features

API features:

  • Asynchronous components
  • Component composition

Custom styles:

  • Font size and color
  • Bold, italic, underline styles, strike-through
  • Subscript, superscript, small caps
  • Paragraph spacing and indentation
  • Left/right/center/justified alignment
  • Aligning text on tabs
  • Font family
  • Embed TTF in the DOCX file

References:

  • Cross references
  • Table of contents

Tables:

Images:

  • From any UInt8Array source
  • Alternative and title text
  • Width and height

Sections:

  • Width and height
  • Orientation

Comments:

  • Point comment
  • Range comment
  • Comment reply

Change tracking:

  • Text additions and deletions
  • Style changes
  • Table row additions and deletions

Differences with actual MS Word DOCX

Obviously docxml is a TypeScript project, which is already very different from how you would normally interact with a DOCX document. More meaningfully however, docxml is meant to make writing DOCX easier than going straight to OOXML. For example;

  • All sizes are of type Length, which means it doesn't matter wether you input them as points, centimeters, inches, 1/2, 1/8th or 1/20th points, English Metric Units, and so on.
  • The JSX pragma will try to correct components that would lead to invalid XML structures, by splitting the parents of invalidly placed components recursively until the new position is valid. Moreover, string content in unexpected places is automatically wrapped in <Text> when using JSX. This makes the configuration of a new DOCX a little more forgiving.
  • Using the <Image> or <Comment> components will automatically create all required relationships etc.
  • Some of the words have changed, generally speaking docxml is more verbose than the DOCX verbiage.
  • Generally speaking docxml prefers formal (JS) references over references-by-identifier. In those cases the identifiers are randomly generated for you when the .docx file is written.
  • Especially in tables and images, a lot of formatting details are automatically applied. In a lot of cases there is no API yet to change them.

For contributors

# Run all unit tests
deno task test


# Run all linting
deno task lint

docxml's People

Contributors

wvbe avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.