Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
markgoodyear committed Jun 4, 2015
1 parent 8583c41 commit 5797052
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## v1.1.0
- Use [`node.error`](https://github.com/postcss/postcss/blob/master/docs/guidelines/plugin.md#31-use-nodeerror-on-css-relevant-errors) for errors.
- Correct readme for installing.

## v1.0.0
- Initial release.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# postcss-vertical-ryhthm [![Build Status][ci-img]][ci]
# postcss-vertical-rhythm [![Build Status][ci-img]][ci]
[PostCSS]: https://github.com/postcss/postcss
[ci-img]: https://travis-ci.org/markgoodyear/postcss-vertical-rhythm.svg?branch=master
[ci]: https://travis-ci.org/markgoodyear/postcss-vertical-rhythm
Expand Down Expand Up @@ -46,16 +46,15 @@ Default:
## Usage
Install:
```
npm install postcss-vertical-ryhthm --save-dev
npm install postcss-vertical-rhythm --save-dev
```

Then include the plugin:
```js
postcss([ require('vertical-ryhthm')({ options }) ])
postcss([ require('postcss-vertical-rhythm')(options) ])
```

See [PostCSS] docs for examples for your environment.


## Licence
Released under the MIT license.
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var postcss = require('postcss');
var spacingValue;

/**
* Convert unit to unitless px value.
Expand Down Expand Up @@ -34,55 +33,56 @@ var calcLineHeight = function (fontProps) {

/**
* Gets the font declaration properties.
* @param {String} declValue
* @param {Object} decl
* @return {Array}
*/
var getProps = function (declValue) {
var getProps = function (decl) {

// Matches {$1:font-size}{$2:unit}/{$3:line-height}.
var fontProps = declValue.match(/(\d+|\d+?\.\d+)(r?em|px|%)(?:\s*\/\s*)(\d+|\d+?\.\d+)\s+/);
var fontProps = decl.value.match(/(\d+|\d+?\.\d+)(r?em|px|%)(?:\s*\/\s*)(\d+|\d+?\.\d+)\s+/);

// Make sure the line-height value is declared.
if (!fontProps) {
throw new Error('Font declaration is invalid. Make sure line-height is set.');
throw decl.error('Font declaration is invalid. Make sure line-height is set.');
}

return fontProps;
};

/**
* Gets the spacing value.
* @param {String} declValue
* Gets the rhythm value.
* @param {Object} decl
* @return {Number}
*/
var getSpacingValue = function (declValue) {
var val = parseFloat(declValue) || 1;
var getRhythmValue = function (decl, rhythmValue) {
var val = parseFloat(decl.value) || 1;

return spacingValue * val;
return rhythmValue * val;
};

module.exports = postcss.plugin('postcss-vertical-rhythm', function (opts) {
opts = opts || {};
var rootSelector = opts.rootSelector || 'body';
var rhythmUnit = 'vr';
var rhythmValue;

return function (css) {
css.eachDecl(function transformDecl (decl) {

// Check for root font-size.
if (decl.parent.selector === rootSelector) {
if (decl.prop === 'font') {
var props = getProps(decl.value);
var props = getProps(decl);

spacingValue = calcLineHeight(props);
rhythmValue = calcLineHeight(props);
} else {
throw new Error('font declaration not found in ' + rootSelector);
throw decl.error('Font declaration not found in ' + rootSelector);
}
}

// Calculate spacing value.
// Calculate ryhthm value.
if (decl.value.indexOf(rhythmUnit) !== -1) {
decl.value = getSpacingValue(decl.value) + 'px';
decl.value = getRhythmValue(decl, rhythmValue) + 'px';
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-vertical-rhythm",
"version": "1.0.0",
"version": "1.1.0",
"description": "A PostCSS plugin to create a custom vertical rhythm unit from the base font-size and line-height.",
"keywords": [
"postcss",
Expand Down

0 comments on commit 5797052

Please sign in to comment.