UNPKG

node-easy-docx

Version:

NodeJs library to parse docx file content into JSON format.

68 lines (64 loc) 2.13 kB
'use strict' const formats = require('../formats') function paragraph (data) { const result = [] const wp = data['w:document']['w:body']['w:p'] if (wp.constructor === [].constructor) { wp.forEach(pContent => { let pObj = {} if ('w:pPr' in pContent) { const format = formats.getParagraphFormat(pContent['w:pPr']) if (Object.keys(format).length > 0) { pObj['format'] = Object.assign({}, format) } } if ('w:r' in pContent) { if (pContent['w:r'].constructor === [].constructor) { // Multiple run content const items = [] pContent['w:r'].forEach((wr) => { let rObj = {} if ('w:t' in wr) { if (wr['w:t'].constructor === [].constructor) { // Multiple text content } else if(wr['w:t'].constructor === {}.constructor) { // Single text content if ('w:rPr' in wr) { const format = formats.getTextFormat(wr['w:rPr']) if (Object.keys(format).length > 0) { rObj['format'] = Object.assign({}, format) } } if ('w:t' in wr) { rObj['text'] = wr['w:t']['_text'] } } } items.push(rObj) }) if (items.length > 0) { pObj['items'] = Object.assign([], items) } } else if (pContent['w:r'].constructor === {}.constructor) { // Single run content if ('w:rPr' in pContent['w:r']) { const format = formats.getTextFormat(pContent['w:r']['w:rPr']) if (Object.keys(format).length > 0) { pObj['format'] = Object.assign({}, format) } } if ('w:t' in pContent['w:r']) { pObj['text'] = pContent['w:r']['w:t']['_text'] } } } else { pObj['lineBreak'] = true } if (Object.keys(pObj).length > 0) { result.push(pObj) } }) } return result } module.exports = paragraph