diff options
| author | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-06 15:35:05 -0500 |
|---|---|---|
| committer | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-06 15:35:05 -0500 |
| commit | 66e054e0f686dad8cd282dd01660e17b156b58e3 (patch) | |
| tree | b404d433dcd05bb1b48b17b807344c700bad526c /lib | |
| parent | ee0baa36779e3ae2f2da9abe7e1c1c83fdcdf8fb (diff) | |
Refactor creating hyperlinks from complex fields
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/body-reader.js | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 8e71aa6..a247e93 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -6,10 +6,10 @@ var documents = require("../documents"); var Result = require("../results").Result; var warning = require("../results").warning; var uriToZipEntryName = require("./uris").uriToZipEntryName; -var lastInstrText = null; function BodyReader(options) { + var complexFieldData = []; var relationships = options.relationships; var contentTypes = options.contentTypes; var docxFile = options.docxFile; @@ -136,8 +136,11 @@ function BodyReader(options) { var properties = _.find(children, isRunProperties); var filteredChildren = children.filter(negate(isRunProperties)); - if (properties && properties.styleName === 'Hyperlink' && lastInstrText) { - return new documents.Hyperlink(filteredChildren, {href: parseHyperlink(lastInstrText)}); + if (properties && properties.styleId === 'Hyperlink' && complexFieldData.length === 1) { + return new documents.Run( + [new documents.Hyperlink(filteredChildren, {href: parseHyperlinkFieldCode(complexFieldData)})], + properties + ); } return new documents.Run(filteredChildren, properties); @@ -147,12 +150,12 @@ function BodyReader(options) { "w:fldChar": function(element) { var type = element.attributes["w:fldCharType"]; if (type === 'end') { - clearState(); + complexFieldData = []; } return emptyResult(); }, "w:instrText": function(element) { - lastInstrText = (element.children[0] || {}).value || ''; + complexFieldData.push(element.children[0].value); return emptyResult(); }, "w:t": function(element) { @@ -506,10 +509,6 @@ function identity(value) { return value; } -function clearState() { - lastInstrText = null; -} - -function parseHyperlink(instrText) { - return (instrText.match(/HYPERLINK "(.*)"/) || [])[1]; +function parseHyperlinkFieldCode(complexFieldData) { + return (complexFieldData[0].match(/HYPERLINK "(.*)"/) || [])[1]; } |
