diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index ddc4fceb9619a6c97e0086e23a252a43d03d995e..451511f162b33fea42e94103b3d1f9b7378a5b20 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -369,6 +369,9 @@ function BodyReader(options) { "w:footnoteReference": noteReferenceReader("footnote"), "w:endnoteReference": noteReferenceReader("endnote"), "w:commentReference": readCommentReference, + "w:cr": function() { + return elementResult(documents.lineBreak); + }, "w:br": function(element) { var breakType = element.attributes["w:type"]; if (breakType == null || breakType === "textWrapping") { diff --git a/lib/raw-text.js b/lib/raw-text.js index 138b73c9c5a4830625d94e9df8fa437f27dbe57c..bf755d9408ecee499fb34032e172b05c7279d010 100644 --- a/lib/raw-text.js +++ b/lib/raw-text.js @@ -1,14 +1,57 @@ var documents = require("./documents"); function convertElementToRawText(element) { - if (element.type === "text") { - return element.value; - } else if (element.type === documents.types.tab) { - return "\t"; - } else { - var tail = element.type === "paragraph" ? "\n\n" : ""; - return (element.children || []).map(convertElementToRawText).join("") + tail; + var notes = []; + var seen = Object.create(null); + var counts = {footnote: 0, endnote: 0}; + var documentNotes = element.notes; + + function convert(current, insideNote) { + if (current.type === "text") { + return current.value; + } else if (current.type === documents.types.tab) { + return "\t"; + } else if (current.type === documents.types["break"] && current.breakType === "line") { + return "\n"; + } else if (current.type === documents.types.noteReference) { + if (insideNote) { + throw new Error("Nested DOCX footnote or endnote references are unsupported."); + } + if (current.noteType !== "footnote" && current.noteType !== "endnote") { + throw new Error("Unsupported DOCX note reference type."); + } + var key = current.noteType + "-" + current.noteId; + if (!seen[key]) { + if (notes.length >= 2000) { + throw new Error("DOCX text extraction supports at most 2,000 referenced notes."); + } + var note = documentNotes && documentNotes.resolve(current); + if (!note || !Array.isArray(note.body)) { + throw new Error("A DOCX footnote or endnote reference has no matching note."); + } + var title = current.noteType === "footnote" ? "Footnote" : "Endnote"; + var marker = "[" + title + " " + (++counts[current.noteType]) + "]"; + seen[key] = marker; + notes.push({marker: marker, body: note.body}); + } + return seen[key]; + } else { + var tail = current.type === "paragraph" ? "\n\n" : ""; + return (current.children || []).map(function(child) { + return convert(child, insideNote); + }).join("") + tail; + } + } + + var body = convert(element, false); + if (!notes.length) { + return body; } + return body + (body.slice(-2) === "\n\n" ? "" : "\n\n") + notes.map(function(note) { + return note.marker + "\n" + note.body.map(function(child) { + return convert(child, true); + }).join(""); + }).join("\n"); } exports.convertElementToRawText = convertElementToRawText; diff --git a/mammoth.browser.js b/mammoth.browser.js index 74d66684379be46bf9e14dda407d2129430ad14f..88cb582e0e1792e449ad1b0647d517cd1428501e 100644 --- a/mammoth.browser.js +++ b/mammoth.browser.js @@ -1168,6 +1168,9 @@ function BodyReader(options) { "w:footnoteReference": noteReferenceReader("footnote"), "w:endnoteReference": noteReferenceReader("endnote"), "w:commentReference": readCommentReference, + "w:cr": function() { + return elementResult(documents.lineBreak); + }, "w:br": function(element) { var breakType = element.attributes["w:type"]; if (breakType == null || breakType === "textWrapping") { @@ -3048,14 +3051,57 @@ function isNode() { var documents = require("./documents"); function convertElementToRawText(element) { - if (element.type === "text") { - return element.value; - } else if (element.type === documents.types.tab) { - return "\t"; - } else { - var tail = element.type === "paragraph" ? "\n\n" : ""; - return (element.children || []).map(convertElementToRawText).join("") + tail; + var notes = []; + var seen = Object.create(null); + var counts = {footnote: 0, endnote: 0}; + var documentNotes = element.notes; + + function convert(current, insideNote) { + if (current.type === "text") { + return current.value; + } else if (current.type === documents.types.tab) { + return "\t"; + } else if (current.type === documents.types["break"] && current.breakType === "line") { + return "\n"; + } else if (current.type === documents.types.noteReference) { + if (insideNote) { + throw new Error("Nested DOCX footnote or endnote references are unsupported."); + } + if (current.noteType !== "footnote" && current.noteType !== "endnote") { + throw new Error("Unsupported DOCX note reference type."); + } + var key = current.noteType + "-" + current.noteId; + if (!seen[key]) { + if (notes.length >= 2000) { + throw new Error("DOCX text extraction supports at most 2,000 referenced notes."); + } + var note = documentNotes && documentNotes.resolve(current); + if (!note || !Array.isArray(note.body)) { + throw new Error("A DOCX footnote or endnote reference has no matching note."); + } + var title = current.noteType === "footnote" ? "Footnote" : "Endnote"; + var marker = "[" + title + " " + (++counts[current.noteType]) + "]"; + seen[key] = marker; + notes.push({marker: marker, body: note.body}); + } + return seen[key]; + } else { + var tail = current.type === "paragraph" ? "\n\n" : ""; + return (current.children || []).map(function(child) { + return convert(child, insideNote); + }).join("") + tail; + } } + + var body = convert(element, false); + if (!notes.length) { + return body; + } + return body + (body.slice(-2) === "\n\n" ? "" : "\n\n") + notes.map(function(note) { + return note.marker + "\n" + note.body.map(function(child) { + return convert(child, true); + }).join(""); + }).join("\n"); } exports.convertElementToRawText = convertElementToRawText;