Skip to content

Commit

Permalink
allow pdf_options.margin to be passed as a CSS-like string
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed May 29, 2018
1 parent 157ff02 commit ad4854b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const readFile = require('./util/read-file');
const getHtml = require('./util/get-html');
const writePdf = require('./util/write-pdf');
const config = require('./util/config');
const { getMarginObject } = require('./util/helpers');

// --
// Configure CLI Arguments
Expand Down Expand Up @@ -97,6 +98,11 @@ async function main(args, config) {
config[key] = jsonArgs.includes(argKey) ? JSON.parse(argValue) : argValue;
}

// sanitize the margin in pdf_options
if (typeof config.pdf_options.margin === 'string') {
config.pdf_options.margin = getMarginObject(config.pdf_options.margin);
}

const highlightStylesheet = path.resolve(
__dirname,
'node_modules',
Expand Down
13 changes: 12 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = require('./util/config');
const getHtml = require('./util/get-html');
const getMarkedWithHighlighter = require('./util/get-marked-with-highlighter');
const getPdfFilePath = require('./util/get-pdf-file-path');
const { getDir } = require('./util/helpers');
const { getDir, getMarginObject } = require('./util/helpers');
const readFile = require('./util/read-file');
const waitForLocalhost = require('./util/wait-for-localhost');

Expand Down Expand Up @@ -59,6 +59,17 @@ test('getDir should get the directory the given file is in', t => {
t.is('/var/foo', getDir(filePath));
});

test('getMarginObject should be able to handle all valid CSS margin inputs', t => {
t.deepEqual(getMarginObject('1em'), { top: '1em', right: '1em', bottom: '1em', left: '1em' });
t.deepEqual(getMarginObject('1px 2px'), { top: '1px', right: '2px', bottom: '1px', left: '2px' });
t.deepEqual(getMarginObject('1mm 2mm 3mm'), { top: '1mm', right: '2mm', bottom: '3mm', left: '2mm' });
t.deepEqual(getMarginObject('1in 2in 3in 4in'), { top: '1in', right: '2in', bottom: '3in', left: '4in' });
t.is(getMarginObject(null), null);
t.throws(() => getMarginObject({}));
t.throws(() => getMarginObject(0));
t.throws(() => getMarginObject('1em 2em 3em 4em 5em'));
});

// --
// read-file

Expand Down
32 changes: 32 additions & 0 deletions util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,35 @@ const { parse, resolve } = require('path');
* file.
*/
module.exports.getDir = filePath => resolve(parse(filePath).dir);

/**
* Get a margin object from a string.
* @param {String} margin A CSS-like margin setting
* @returns object with keys 'top', 'right', 'bottom', 'left'
*/
module.exports.getMarginObject = margin => {
if (margin === null) {
return null;
}

if (typeof margin === 'string') {
const margins = margin.split(' ');
const [top, right, bottom, left] = margins;

switch (margins.length) {
case 1:
return { top, right: top, bottom: top, left: top };
case 2:
return { top, right, bottom: top, left: right };
case 3:
return { top, right, bottom, left: right };
case 4:
return { top, right, bottom, left };

default:
break; // will throw the error below
}
}

throw new Error(`invalid margin input: ${margin}`);
};

0 comments on commit ad4854b

Please sign in to comment.