aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-03 19:34:51 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-03 19:34:51 +0100
commitd98f3862193c6bb9b88735c131531ac6b1f7b5c0 (patch)
tree761a998bcd1711e49aeeefee3797e709d8a0f34a
parentb5fef15290ef976620315eacd4cb762d07e1e7c3 (diff)
Add basic comments reader
-rw-r--r--lib/documents.js10
-rw-r--r--lib/docx/comments-reader.js21
-rw-r--r--test/docx/comments-reader.tests.js21
3 files changed, 52 insertions, 0 deletions
diff --git a/lib/documents.js b/lib/documents.js
index 5665198..a8312e9 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -10,6 +10,7 @@ var types = exports.types = {
noteReference: "noteReference",
image: "image",
note: "note",
+ comment: "comment",
table: "table",
tableRow: "tableRow",
tableCell: "tableCell",
@@ -112,6 +113,14 @@ function Note(options) {
};
}
+function comment(options) {
+ return {
+ type: types.comment,
+ commentId: options.commentId,
+ body: options.body
+ };
+}
+
function noteKey(noteType, id) {
return noteType + "-" + id;
}
@@ -171,6 +180,7 @@ exports.Hyperlink = Hyperlink;
exports.NoteReference = NoteReference;
exports.Notes = Notes;
exports.Note = Note;
+exports.comment = comment;
exports.Image = Image;
exports.Table = Table;
exports.TableRow = TableRow;
diff --git a/lib/docx/comments-reader.js b/lib/docx/comments-reader.js
new file mode 100644
index 0000000..be98bac
--- /dev/null
+++ b/lib/docx/comments-reader.js
@@ -0,0 +1,21 @@
+var documents = require("../documents");
+var Result = require("../results").Result;
+
+function createCommentsReader(bodyReader) {
+ function readCommentsXml(xml) {
+ return Result.combine(xml.root.getElementsByTagName("w:comment")
+ .map(readCommentElement));
+ }
+
+ function readCommentElement(element) {
+ var id = element.attributes["w:id"];
+ return bodyReader.readXmlElements(element.children)
+ .map(function(body) {
+ return documents.comment({commentId: id, body: body});
+ });
+ }
+
+ return readCommentsXml;
+}
+
+exports.createCommentsReader = createCommentsReader;
diff --git a/test/docx/comments-reader.tests.js b/test/docx/comments-reader.tests.js
new file mode 100644
index 0000000..55cffeb
--- /dev/null
+++ b/test/docx/comments-reader.tests.js
@@ -0,0 +1,21 @@
+var assert = require("assert");
+
+var createCommentsReader = require("../../lib/docx/comments-reader").createCommentsReader;
+var BodyReader = require("../../lib/docx/body-reader").BodyReader;
+var documents = require("../../lib/documents");
+var xml = require("../../lib/xml");
+var test = require("../test")(module);
+
+
+test('ID and body of comment are read', function() {
+ var bodyReader = new BodyReader({});
+ var body = [xml.element("w:p")];
+ var comments = createCommentsReader(bodyReader)({
+ root: xml.element("w:comments", {}, [
+ xml.element("w:comment", {"w:id": "1"}, body)
+ ])
+ });
+ assert.equal(comments.value.length, 1);
+ assert.deepEqual(comments.value[0].body, [new documents.Paragraph([])]);
+ assert.deepEqual(comments.value[0].commentId, "1");
+});