Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
mdast-range
Advanced tools
Patch index-based ranges on mdast nodes, so you can slice sources!
npm install mdast-range
component install wooorm/mdast-range
bower install mdast-range
var range = require('wooorm/mdast-range');
UMD: globals, AMD, and CommonJS (uncompressed and compressed):
<script src="path/to/mdast.js"></script>
<script src="path/to/mdast-range.js"></script>
<script>
mdast.use(mdastRange);
</script>
Dependencies and input:
var range = require('mdast-range');
var mdast = require('mdast').use(range);
var doc = 'Some *emphasis*,\n**strongness**,\nand `code`.';
And when the plugin is run, the ast looks as follows:
mdast.process(doc);
Note the offset
properties.
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Some ",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 6,
"offset": 5
},
"indent": []
}
},
{
"type": "emphasis",
"children": [
{
"type": "text",
"value": "emphasis",
"position": {
"start": {
"line": 1,
"column": 7,
"offset": 6
},
"end": {
"line": 1,
"column": 15,
"offset": 14
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 6,
"offset": 5
},
"end": {
"line": 1,
"column": 16,
"offset": 15
},
"indent": []
}
},
{
"type": "text",
"value": ",\n",
"position": {
"start": {
"line": 1,
"column": 16,
"offset": 15
},
"end": {
"line": 2,
"column": 1,
"offset": 17
},
"indent": [
1
]
}
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "strongness",
"position": {
"start": {
"line": 2,
"column": 3,
"offset": 19
},
"end": {
"line": 2,
"column": 13,
"offset": 29
},
"indent": []
}
}
],
"position": {
"start": {
"line": 2,
"column": 1,
"offset": 17
},
"end": {
"line": 2,
"column": 15,
"offset": 31
},
"indent": []
}
},
{
"type": "text",
"value": ",\nand ",
"position": {
"start": {
"line": 2,
"column": 15,
"offset": 31
},
"end": {
"line": 3,
"column": 5,
"offset": 37
},
"indent": [
1
]
}
},
{
"type": "inlineCode",
"value": "code",
"position": {
"start": {
"line": 3,
"column": 5,
"offset": 37
},
"end": {
"line": 3,
"column": 11,
"offset": 43
},
"indent": []
}
},
{
"type": "text",
"value": ".",
"position": {
"start": {
"line": 3,
"column": 11,
"offset": 43
},
"end": {
"line": 3,
"column": 12,
"offset": 44
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 3,
"column": 12,
"offset": 44
},
"indent": [
1,
1
]
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 3,
"column": 12,
"offset": 44
}
}
}
Adds offset
to each node’s Position.
Where normally nodes have a line based positional information, such as:
{
"type": "break",
"position": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 3,
"column": 1
}
}
}
...this plugin adds absolute positional information:
{
"type": "break",
"position": {
"start": {
"line": 2,
"column": 5,
"offset": 25
},
"end": {
"line": 3,
"column": 1,
"offset": 26
}
}
}
...so you can slice (or syntax highlight) the source:
var line = doc.slice(node.position.start.offset, node.position.end.offset);
// Yields: `"\n"`
To reverse an offset
into a position, pass it into file.offsetToPosition()
:
mdast.use(range).process('foo', function (err, doc, file) {
file.offsetToPosition(0);
// Yields: `{line: 1, column: 1}`
});
To turn any position object, use file.positionToOffset()
:
mdast.use(range).process('foo', function (err, doc, file) {
var pos = file.offsetToPosition(0);
file.positionToOffset(pos);
// Yields: `0`.
});
FAQs
Renamed to remark-range
The npm package mdast-range receives a total of 32 weekly downloads. As such, mdast-range popularity was classified as not popular.
We found that mdast-range demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.