From 58cae6030e51481ffb462e52ab6aeb479e0a705d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 19 Jan 2015 22:48:32 +0000 Subject: Operate over a more generic notion of note --- lib/documents.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'lib/documents.js') diff --git a/lib/documents.js b/lib/documents.js index b0959cc..aea2b39 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -1,3 +1,5 @@ +var _ = require("underscore"); + var types = { document: "document", paragraph: "paragraph", @@ -7,7 +9,7 @@ var types = { hyperlink: "hyperlink", footnoteReference: "footnoteReference", image: "image", - footnote: "footnote", + note: "note", table: "table", tableRow: "tableRow", tableCell: "tableCell", @@ -19,7 +21,7 @@ function Document(children, options) { return { type: types.document, children: children, - footnotes: options.footnotes || new Footnotes({}) + notes: options.notes || new Notes({}) }; } @@ -83,22 +85,33 @@ function FootnoteReference(options) { }; } -function Footnotes(footnotes) { - this._footnotes = footnotes; +function Notes(notes) { + this._notes = _.indexBy(notes, function(note) { + return noteKey(note.noteType, note.id); + }); } -Footnotes.prototype.findFootnoteById = function(id) { - return this._footnotes[id] || null; +Notes.prototype.findFootnoteById = function(id) { + return this.findNoteByKey(noteKey("footnote", id)); +}; + +Notes.prototype.findNoteByKey = function(key) { + return this._notes[key] || null; }; -function Footnote(options) { +function Note(options) { return { - type: types.footnote, - body: options.body, - id: options.id + type: types.note, + noteType: options.noteType, + id: options.id, + body: options.body }; } +function noteKey(noteType, id) { + return noteType + "-" + id; +} + function Image(options) { return { type: types.image, @@ -142,8 +155,8 @@ exports.Text = Text; exports.Tab = Tab; exports.Hyperlink = Hyperlink; exports.FootnoteReference = FootnoteReference; -exports.Footnotes = Footnotes; -exports.Footnote = Footnote; +exports.Notes = Notes; +exports.Note = Note; exports.Image = Image; exports.Table = Table; exports.TableRow = TableRow; -- cgit v1.2.3