diff --git a/app/build/plugins/typeset/index.js b/app/build/plugins/typeset/index.js index ec76fd289a8..f0247a7457c 100644 --- a/app/build/plugins/typeset/index.js +++ b/app/build/plugins/typeset/index.js @@ -1,4 +1,5 @@ var typeset = require("typeset"); +var parseBoolean = require("helper/parseBoolean"); function prerender(html, callback, options) { // would be nice to add options. hyphenate in future @@ -10,6 +11,15 @@ function prerender(html, callback, options) { var disable = ["ligatures", "hyphenate"]; + options = options || {}; + + for (var key in options) { + if (Object.prototype.hasOwnProperty.call(options, key)) { + var parsed = parseBoolean(options[key]); + options[key] = typeof parsed === "boolean" ? parsed : options[key]; + } + } + options.spaces = options.quotes = options.punctuation; for (var i in options) if (options[i] === false) disable.push(i); diff --git a/app/build/tests/plugins/typeset.js b/app/build/tests/plugins/typeset.js new file mode 100644 index 00000000000..b90f50ddeb0 --- /dev/null +++ b/app/build/tests/plugins/typeset.js @@ -0,0 +1,33 @@ +describe("typeset plugin", function () { + require("./util/setup")(); + + beforeEach(function () { + this.blog.plugins.typeset = { + enabled: true, + options: { + punctuation: "off", + smallCaps: "false", + hangingPunctuation: true, + }, + }; + }); + + afterEach(function () { + this.blog.plugins.typeset = { + enabled: true, + options: { + hangingPunctuation: true, + punctuation: true, + smallCaps: true, + }, + }; + }); + + it("disables punctuation and small caps when option strings are falsey", function (done) { + const path = "/typeset.txt"; + const contents = '"NASA"'; + const html = '

"NASA"

'; + + this.buildAndCheck({ path, contents }, { html }, done); + }); +}); diff --git a/app/dashboard/site/save/format.js b/app/dashboard/site/save/format.js index 47b8b02d785..0c93ca9f939 100644 --- a/app/dashboard/site/save/format.js +++ b/app/dashboard/site/save/format.js @@ -2,6 +2,7 @@ var Blog = require("models/blog"); var formJSON = require("helper/formJSON"); var extend = require("helper/extend"); var normalizeImageExif = require("models/blog/util/imageExif").normalize; +var parseBoolean = require("helper/parseBoolean"); module.exports = function (req, res, next) { try { @@ -46,14 +47,18 @@ module.exports = function (req, res, next) { // this bullshit below is because I haven't properly declared // the model for blog.plugins so formJSON needs a little help... for (var i in req.updates.plugins) { - req.updates.plugins[i].enabled = req.updates.plugins[i].enabled === "on"; - if (!req.updates.plugins[i].options) req.updates.plugins[i].options = {}; - } + var plugin = req.updates.plugins[i]; + + plugin.enabled = parseBoolean(plugin.enabled) === true; + + if (!plugin.options) plugin.options = {}; - if (req.updates.plugins.typeset) { - for (var x in req.updates.plugins.typeset.options) - req.updates.plugins.typeset.options[x] = - req.updates.plugins.typeset.options[x] === "on"; + for (var option in plugin.options) { + if (Object.prototype.hasOwnProperty.call(plugin.options, option)) { + var parsed = parseBoolean(plugin.options[option]); + if (typeof parsed === "boolean") plugin.options[option] = parsed; + } + } } extend(req.updates.plugins).and(req.blog.plugins); diff --git a/app/helper/parseBoolean.js b/app/helper/parseBoolean.js new file mode 100644 index 00000000000..686d4dba2ff --- /dev/null +++ b/app/helper/parseBoolean.js @@ -0,0 +1,37 @@ +function parseBoolean(value) { + if (typeof value === "boolean") return value; + if (value === null) return false; + if (typeof value === "undefined") return value; + + if (typeof value === "number") { + if (value === 1) return true; + if (value === 0) return false; + return value; + } + + if (typeof value === "string") { + var normalized = value.trim().toLowerCase(); + + if (!normalized) return false; + + if (normalized === "true" || normalized === "on" || normalized === "1") + return true; + + if ( + normalized === "false" || + normalized === "off" || + normalized === "0" || + normalized === "no" || + normalized === "n" || + normalized === "null" || + normalized === "undefined" + ) + return false; + + return value; + } + + return value; +} + +module.exports = parseBoolean;