UNPKG

node-easy-docx

Version:

NodeJs library to parse docx file content into JSON format.

74 lines (63 loc) 1.63 kB
'use strict' const fs = require('fs') const JSZip = require('jszip') const lib = require('./lib') const utils = require('./utils') function parseDocx (props) { return new Promise((resolve, reject) => { let options = {} if ( props && props.constructor === {}.constructor ) { // TODO: Save options } try { fs.readFile(this.path, function (err, data) { if (err) reject(err) JSZip.loadAsync(data).then(zip => { zip .file('word/document.xml') .async('text') .then(data => { let result = {} const xmlJSON = utils.parseXML(data) // TODO: Get document meta info // TODO: Get document properties // Get run content const paragraphs = lib.paragraph(xmlJSON) resolve(paragraphs) }) .catch(err => { reject(err) }) }) }) } catch (err) { reject(err) } }) } const EasyDocx = function (props) { if (!(this instanceof EasyDocx)) { throw new Error('EasyDocx is a class.') } if (!( props && props.constructor === {}.constructor && 'path' in props && props.path && typeof props.path === 'string' )) { throw new Error('File "Path" is required.') } if (!fs.existsSync(props.path)) { throw new Error(`File at path "${props.path}" does not exists.`) } this.path = props.path } /** * Parse Docx file into EasyDocx XML file. */ EasyDocx.prototype.parseDocx = parseDocx module.exports = EasyDocx