diff --git a/src/model.js b/src/model.js index 88c724a7..aca6c1e3 100644 --- a/src/model.js +++ b/src/model.js @@ -217,7 +217,6 @@ my.DocumentList = Backbone.Collection.extend({ // * format: (optional) used to indicate how the data should be formatted. For example: // * type=date, format=yyyy-mm-dd // * type=float, format=percentage -// * type=string, format=link (render as hyperlink) // * type=string, format=markdown (render as markdown if Showdown available) // * is_derived: (default: false) attribute indicating this field has no backend data but is just derived from other fields (see below). // @@ -236,6 +235,15 @@ my.DocumentList = Backbone.Collection.extend({ // field. This provides support for a) 'derived/computed' fields: i.e. fields // whose data are functions of the data in other fields b) transforming the // value of this field prior to rendering. +// +// #### Default renderers +// +// * string +// * no format provided: pass through but convert http:// to hyperlinks +// * format = plain: do no processing on the source text +// * format = markdown: process as markdown (if Showdown library available) +// * float +// * format = percentage: format as a percentage my.Field = Backbone.Model.extend({ // ### defaults - define default values defaults: { @@ -278,9 +286,7 @@ my.Field = Backbone.Model.extend({ }, 'string': function(val, field, doc) { var format = field.get('format'); - if (format === 'link') { - return 'VAL'.replace(/VAL/g, val); - } else if (format === 'markdown') { + if (format === 'markdown') { if (typeof Showdown !== 'undefined') { var showdown = new Showdown.converter(); out = showdown.makeHtml(val); @@ -288,8 +294,16 @@ my.Field = Backbone.Model.extend({ } else { return val; } + } else if (format == 'plain') { + return val; + } else { + // as this is the default and default type is string may get things + // here that are not actually strings + if (val && typeof val === 'string') { + val = val.replace(/(https?:\/\/[^ ]+)/g, '$1'); + } + return val } - return val; } } }); diff --git a/test/model.test.js b/test/model.test.js index f4a4c623..b7a0e3d8 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -43,6 +43,7 @@ test('Field: default renderers', function () { x: 12.3, myobject: {a: 1, b: 2}, link: 'http://abc.com/', + link2: 'Some text then https://abc.com/', markdown: '### ABC' }); var field = new recline.Model.Field({id: 'myobject', type: 'object'}); @@ -55,11 +56,16 @@ test('Field: default renderers', function () { var exp = '12.3%'; equal(out, exp); - var field = new recline.Model.Field({id: 'link', type: 'string', format: 'link'}); + var field = new recline.Model.Field({id: 'link'}); var out = doc.getFieldValue(field); var exp = 'http://abc.com/'; equal(out, exp); + var field = new recline.Model.Field({id: 'link2'}); + var out = doc.getFieldValue(field); + var exp = 'Some text then https://abc.com/'; + equal(out, exp); + var field = new recline.Model.Field({id: 'markdown', type: 'string', format: 'markdown'}); var out = doc.getFieldValue(field); // Showdown is not installed so nothing should happen