mdast-range
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -6,2 +6,9 @@ --- | ||
0.4.0 / 2015-06-03 | ||
================== | ||
* Remove dollar signs of shell code in `readme.md` ([4c0cd17](https://github.com/wooorm/mdast-range/commit/4c0cd17)) | ||
* Add `positionToOffset` to turn a position into an offset ([bd54954](https://github.com/wooorm/mdast-range/commit/bd54954)) | ||
* Update mdast-toc ([497cbeb](https://github.com/wooorm/mdast-range/commit/497cbeb)) | ||
0.3.0 / 2015-05-25 | ||
@@ -8,0 +15,0 @@ ================== |
46
index.js
@@ -52,5 +52,4 @@ 'use strict'; | ||
*/ | ||
function addRange(position, offsets) { | ||
position.offset = ((offsets[position.line - 2] || 0) + | ||
position.column - 1) || 0; | ||
function addRange(position, fn) { | ||
position.offset = fn(position); | ||
} | ||
@@ -66,2 +65,32 @@ | ||
*/ | ||
function positionToOffsetFactory(offsets) { | ||
/** | ||
* Calculate offsets for `lines`. | ||
* | ||
* @param {Object} position - Position. | ||
* @return {Object} - Object with `line` and `colymn` | ||
* properties based on the bound `offsets`. | ||
*/ | ||
function positionToOffset(position) { | ||
var line = position && position.line; | ||
var column = position && position.column; | ||
if (!isNaN(line) && !isNaN(column)) { | ||
return ((offsets[line - 2] || 0) + column - 1) || 0; | ||
} | ||
return -1; | ||
} | ||
return positionToOffset; | ||
} | ||
/** | ||
* Factory to reverse an offset into a line--column | ||
* tuple. | ||
* | ||
* @param {Array.<number>} offsets - Offsets, as returned | ||
* by `toOffsets()`. | ||
* @return {Function} - Bound method. | ||
*/ | ||
function offsetToPositionFactory(offsets) { | ||
@@ -106,2 +135,3 @@ /** | ||
var contents = String(file).split('\n'); | ||
var positionToOffset; | ||
@@ -117,12 +147,14 @@ /* | ||
/* | ||
* Get length at each line. | ||
* Construct. | ||
*/ | ||
contents = toOffsets(contents); | ||
positionToOffset = positionToOffsetFactory(contents); | ||
/* | ||
* Expose `offsetToPosition`. | ||
* Expose methods. | ||
*/ | ||
file.offsetToPosition = offsetToPositionFactory(contents); | ||
file.positionToOffset = positionToOffset; | ||
@@ -137,7 +169,7 @@ /* | ||
if (position && position.start) { | ||
addRange(position.start, contents); | ||
addRange(position.start, positionToOffset); | ||
} | ||
if (position && position.end) { | ||
addRange(position.end, contents); | ||
addRange(position.end, positionToOffset); | ||
} | ||
@@ -144,0 +176,0 @@ }); |
{ | ||
"name": "mdast-range", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Range information to mdast", | ||
@@ -34,3 +34,3 @@ "license": "MIT", | ||
"mdast-github": "^0.3.0", | ||
"mdast-toc": "^0.3.0", | ||
"mdast-toc": "^0.4.1", | ||
"mdast-usage": "^0.2.0", | ||
@@ -37,0 +37,0 @@ "mdast-yaml-config": "^0.1.0", |
@@ -10,3 +10,3 @@ # mdast-range [![Build Status](https://img.shields.io/travis/wooorm/mdast-range.svg?style=flat)](https://travis-ci.org/wooorm/mdast-range) [![Coverage Status](https://img.shields.io/coveralls/wooorm/mdast-range.svg?style=flat)](https://coveralls.io/r/wooorm/mdast-range?branch=master) | ||
```bash | ||
$ npm install mdast-range | ||
npm install mdast-range | ||
``` | ||
@@ -17,3 +17,3 @@ | ||
```bash | ||
$ component install wooorm/mdast-range | ||
component install wooorm/mdast-range | ||
``` | ||
@@ -24,3 +24,3 @@ | ||
```bash | ||
$ bower install mdast-range | ||
bower install mdast-range | ||
``` | ||
@@ -47,4 +47,7 @@ | ||
* [Usage](#usage) | ||
* [API](#api) | ||
* [mdast.use(range)](#mdastuserange) | ||
* [License](#license) | ||
@@ -330,4 +333,15 @@ | ||
To turn any position object, use `file.positionToOffset()`: | ||
```javascript | ||
mdast.use(range).process('foo', function (err, doc, file) { | ||
var pos = file.offsetToPosition(0); | ||
file.positionToOffset(pos); | ||
// Yields: `0`. | ||
}); | ||
``` | ||
## License | ||
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15520
156
343