Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/build/plugins/typeset/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions app/build/tests/plugins/typeset.js
Original file line number Diff line number Diff line change
@@ -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 = '<p>"NASA"</p>';

this.buildAndCheck({ path, contents }, { html }, done);
});
});
19 changes: 12 additions & 7 deletions app/dashboard/site/save/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions app/helper/parseBoolean.js
Original file line number Diff line number Diff line change
@@ -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;
Loading