Skip to content

Commit e6cb078

Browse files
committed
Initial commit
0 parents  commit e6cb078

File tree

8 files changed

+307
-0
lines changed

8 files changed

+307
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
unist-util-source.js
7+
unist-util-source.min.js

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: node_js
2+
node_js:
3+
- '0.11'
4+
- '6.0'
5+
after_success: bash <(curl -s https://codecov.io/bash)
6+
deploy:
7+
- provider: npm
8+
email: tituswormer@gmail.com
9+
api_key:
10+
secure: pxHatSGmro/OrE+BnXSwakDGwq8IaaCf0FhrdQj8TvAx3a2JJcmpHDVXQiK6QdDGDCMTrfaMUFMtWcpWYOVpUiSyDTvoRLRxEeGqEsDgLZkAxt1rGcU5EiCfaSdhDpMZHxAW586NLqqMvcrVAFMgM504GRWWrH7atYH8Xy9lMiefaJEDtc3a0lMkLnliMFg29nVgSp5KUKvHbY8i+pUsxvo11H0T4NGeiKgnkcwN5bVKsOWBY1zgbEHigF/I8XaUkOr/o94j9SGDD3qRVmoXbufZoav1YSIxuWDdkFxIvsUNje5yCMSxQFMyAQ5Sl7HMDbNkoPRB98A80SbJq+vggm14mmWdhTeiwwHWqoA7VbcRKKmChLZ5V/z26+JWA1dKcMruYGcfVBSxkQNs599jpXyJYtmIJ7KFLy5FJrKT6iMXYQIssZYk9HZ2hjebmXFdW9nzki5qvarRDI13aFLFC/81owC9Mk0Xyf0ofH5oybzvXX0IR6CrGPv0TIC2p+m/itRqHbX7TQVgvA2/i+K8UY1ByRkxCVfhU1GasBHkTZLgR7Qap8XrB6U7bGVjuOziPea3S/KVkwb964xtbYILrYfobcgmv7aMxYRbT+a41l468h5GFSHfPu8NDc/qH16xgMrytB5kAQvXV+217/Xe+aXjLA+lV1IqbAcXwVs5orY=
11+
on:
12+
tags: true
13+
node: '6.0'
14+
- provider: releases
15+
api_key:
16+
secure: nNUQKTPTsbRQKgV1mUQ9jt52w+lNGrQtEpd975K9jQGxWvBNJaHb0eTDbuJCO7+OgG77yKiGQcr9gc7oM4WjFFcLoR2qWxRXsgGuyAIiMU85RRBvHXp7+gv1Qa/ko15MKmJo0ACxizAEiT6SIpwdQpo0AaopQKxg26pNN9S52YK6WtUjbgbIjRbcntFXi2CByO6W5d3rXX2pWgoE+j+Qm2UpA8j09i6pghVUcBb+7ngy8D0PBYnXW3pTKzZiFuhdW93n2VPl553q0FQe9jQyoaC0DYkeT1JOyLCd7uME+mZaCsiue2gdWlY6Vbbitv4ccK0qvUblMPvusDV5LJGz2c8V3L9+uC90x30LO1sX/XXG26nyuN2YrKqdmuJJWVqr3cz6c6Z94yBdcnuReJqo1IOuUetwo1fKnB7G9k3qkhDKc3PPzB5Xz/VlKQTnzS5TrSrzog8wWYQhauU5EO/+Bivyyf7FUc+/+ykSIVS+ysfwZw65wOrNaAW3oPs0kJdouXsdotcWsSt+Xbp6FMkhju92xifdWr99MrXzsMpB/ToNdHgQ3LPp+JrU61ASfcb3ojH8k+SggVI5LnsDKtoBIQy8LyWr6FHAwxDadtjyuBAPul/+emLQiUalzG9Y3/+lmQDWT2piha0W0Fw643F/GdxNrdKqws6/MX70el2VhMY=
17+
file:
18+
- "unist-util-source.js"
19+
- "unist-util-source.min.js"
20+
on:
21+
tags: true
22+
node: '6.0'

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2016 Titus Wormer
4+
* @license MIT
5+
* @module unist:util:source
6+
* @fileoverview Get the source of a node or location.
7+
*/
8+
9+
'use strict';
10+
11+
var location = require('vfile-location');
12+
13+
module.exports = source;
14+
15+
function source(value, file) {
16+
var doc = String(file);
17+
var loc = location(file);
18+
var val = value && value.position ? value.position : value || {};
19+
var start;
20+
var end;
21+
var indents;
22+
var indent;
23+
var lines;
24+
var length;
25+
var index;
26+
27+
start = loc.toOffset(val.start);
28+
end = loc.toOffset(val.end);
29+
indents = val.indent || [];
30+
31+
if (start === -1 || end === -1) {
32+
return null;
33+
}
34+
35+
lines = doc.slice(start, end).split('\n');
36+
length = lines.length;
37+
index = 0;
38+
39+
while (++index < length) {
40+
indent = indents[index - 1];
41+
lines[index] = lines[index].slice(indent ? indent - 1 : 0);
42+
}
43+
44+
return lines.join('\n');
45+
}

package.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "unist-util-source",
3+
"version": "0.0.0",
4+
"description": "Utility to get the source of a value",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"node",
9+
"location",
10+
"source"
11+
],
12+
"repository": "https://github.com/wooorm/unist-util-source.git",
13+
"bugs": "https://github.com/wooorm/hast-util-to-html/issues",
14+
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
15+
"contributors": [
16+
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
17+
],
18+
"dependencies": {
19+
"vfile-location": "^2.0.1"
20+
},
21+
"files": [
22+
"index.js"
23+
],
24+
"devDependencies": {
25+
"browserify": "^13.0.1",
26+
"esmangle": "^1.0.1",
27+
"nyc": "^8.1.0",
28+
"remark": "^6.0.1",
29+
"remark-cli": "^2.0.0",
30+
"remark-preset-wooorm": "^1.0.0",
31+
"tape": "^4.0.0",
32+
"xo": "^0.16.0"
33+
},
34+
"scripts": {
35+
"build-md": "remark . --quiet --frail",
36+
"build-bundle": "browserify index.js --bare -s unistUtilSource > unist-util-source.js",
37+
"build-mangle": "esmangle < unist-util-source.js > unist-util-source.min.js",
38+
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
39+
"lint": "xo",
40+
"test-api": "node test",
41+
"test-coverage": "nyc --reporter lcov tape test",
42+
"test": "npm run build && npm run lint && npm run test-coverage"
43+
},
44+
"nyc": {
45+
"check-coverage": true,
46+
"lines": 100,
47+
"functions": 100,
48+
"branches": 100
49+
},
50+
"xo": {
51+
"space": true,
52+
"ignores": [
53+
"unist-util-source.js"
54+
]
55+
},
56+
"remarkConfig": {
57+
"output": true,
58+
"presets": "wooorm"
59+
}
60+
}

readme.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# unist-util-source [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
2+
3+
[**Unist**][unist] utility to get the source of a [Node][] or
4+
[Location][].
5+
6+
## Installation
7+
8+
[npm][npm-install]:
9+
10+
```bash
11+
npm install unist-util-source
12+
```
13+
14+
## Usage
15+
16+
```js
17+
var remark = require('remark');
18+
var source = require('./');
19+
20+
remark()
21+
.use(function () {
22+
return transformer;
23+
function transformer(tree, file) {
24+
var list = tree.children[0].children[0];
25+
console.log(source(list, file));
26+
}
27+
})
28+
.process('> + **[Hello](./example)**\n> world.');
29+
```
30+
31+
Yields:
32+
33+
```txt
34+
+ **[Hello](./example)**
35+
world.
36+
```
37+
38+
## API
39+
40+
### `source(value, file)`
41+
42+
###### Parameters
43+
44+
* `value` ([`Node`][node] or [`Location`][location]) — Value to get.
45+
* `file` ([`VFile`][vfile]) — Virtual file in which `value` exists.
46+
47+
###### Returns
48+
49+
`string?` — Source of `value` in `file`, if available.
50+
51+
## License
52+
53+
[MIT][license] © [Titus Wormer][author]
54+
55+
<!-- Definitions -->
56+
57+
[travis-badge]: https://img.shields.io/travis/wooorm/unist-util-source.svg
58+
59+
[travis]: https://travis-ci.org/wooorm/unist-util-source
60+
61+
[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/unist-util-source.svg
62+
63+
[codecov]: https://codecov.io/github/wooorm/unist-util-source
64+
65+
[npm-install]: https://docs.npmjs.com/cli/install
66+
67+
[license]: LICENSE
68+
69+
[author]: http://wooorm.com
70+
71+
[unist]: https://github.com/wooorm/unist
72+
73+
[node]: https://github.com/wooorm/unist#node
74+
75+
[location]: https://github.com/wooorm/unist#location
76+
77+
[vfile]: https://github.com/wooorm/vfile

test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2016 Titus Wormer
4+
* @license MIT
5+
* @module unist:util:source
6+
* @fileoverview Test suite for `unit-util-source`.
7+
*/
8+
9+
'use strict';
10+
11+
/* Dependencies. */
12+
var test = require('tape');
13+
var remark = require('remark');
14+
var source = require('./');
15+
16+
/* Tests. */
17+
test('unist-util-source', function (t) {
18+
t.plan(10);
19+
20+
remark().use(function () {
21+
return transformer;
22+
function transformer(tree, file) {
23+
var node = tree;
24+
25+
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.');
26+
27+
/* Blockquote. */
28+
node = tree.children[0];
29+
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.');
30+
31+
/* List. */
32+
node = node.children[0];
33+
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.');
34+
35+
/* List-item. */
36+
node = node.children[0];
37+
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.');
38+
39+
/* Paragraph. */
40+
node = node.children[0];
41+
t.equal(source(node, file), '**[Hello](./example)**\nworld.');
42+
43+
/* Strong. */
44+
node = node.children[0];
45+
t.equal(source(node, file), '**[Hello](./example)**');
46+
47+
/* Link. */
48+
node = node.children[0];
49+
t.equal(source(node, file), '[Hello](./example)');
50+
51+
/* Text. */
52+
node = node.children[0];
53+
t.equal(source(node, file), 'Hello');
54+
55+
/* Generated. */
56+
t.equal(source({type: node.type, value: node.value}, file), null);
57+
58+
/* Missing. */
59+
t.equal(source(null, file), null);
60+
}
61+
}).process([
62+
'> + **[Hello](./example)**',
63+
'> world.'
64+
].join('\n'));
65+
});

0 commit comments

Comments
 (0)