all files / keystone/fields/types/markdown/ MarkdownType.js

78.26% Statements 36/46
40% Branches 10/25
57.14% Functions 4/7
78.26% Lines 36/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                                                                                                                    
var FieldType = require('../Type');
var marked = require('marked');
var util = require('util');
var TextType = require('../text/TextType');
 
/**
 * Markdown FieldType Constructor
 * @extends Field
 * @api public
 */
function markdown (list, path, options) {
	this._defaultSize = 'full';
 
	this.toolbarOptions = options.toolbarOptions || {};
	this.markedOptions = options.markedOptions || {};
	this.height = options.height || 90;
	this.wysiwyg = ('wysiwyg' in options) ? options.wysiwyg : true;
 
	this._properties = ['wysiwyg', 'height', 'toolbarOptions'];
	markdown.super_.call(this, list, path, options);
}
util.inherits(markdown, FieldType);
 
 
markdown.prototype.validateInput = TextType.prototype.validateInput;
markdown.prototype.validateRequiredInput = TextType.prototype.validateRequiredInput;
 
/**
 * Registers the field on the List's Mongoose Schema.
 *
 * Adds String properties for .md and .html markdown, and a setter for .md
 * that generates html when it is updated.
 */
markdown.prototype.addToSchema = function () {
 
	var schema = this.list.schema;
 
	var paths = this.paths = {
		md: this._path.append('.md'),
		html: this._path.append('.html'),
	};
 
	var markedOptions = this.markedOptions;
 
	var setMarkdown = function (value) {
		Iif (value === this.get(paths.md)) {
			return value;
		}
		Eif (typeof value === 'string') {
			this.set(paths.html, marked(value, markedOptions));
			return value;
		} else {
			this.set(paths.html, undefined);
			return undefined;
		}
	};
 
	schema.nested[this.path] = true;
	schema.add({
		html: { type: String },
		md: { type: String, set: setMarkdown },
	}, this.path + '.');
 
	this.bindUnderscoreMethods();
};
 
/**
 * Formats the field value
 */
markdown.prototype.format = function (item) {
	return item.get(this.paths.html);
};
 
/**
 * Validates that a value for this field has been provided in a data object
 *
 * Deprecated
 */
markdown.prototype.inputIsValid = function (data, required, item) {
	if (!(this.path in data) && item && item.get(this.paths.md)) {
		return true;
	}
	return (!required || data[this.path]) ? true : false;
};
 
/**
 * Detects whether the field has been modified
 */
markdown.prototype.isModified = function (item) {
	return item.isModified(this.paths.md);
};
 
/**
 * Updates the value for this field in the item from a data object
 *
 * Will accept either the field path, or paths.md
 */
markdown.prototype.updateItem = function (item, data, callback) {
	var value = this.getValueFromData(data);
	Eif (value !== undefined) {
		item.set(this.paths.md, value);
	}	else if (this.paths.md in data) {
		item.set(this.paths.md, data[this.paths.md]);
	}
	process.nextTick(callback);
};
 
/* Export Field Type */
module.exports = markdown;