Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdast-range

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-range

Patch index-based ranges on mdast nodes

  • 1.0.1
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

mdast-range Build Status Coverage Status

Patch index-based ranges on mdast nodes, so you can slice sources!

Installation

npm

npm install mdast-range

Component.js

component install wooorm/mdast-range

Bower

bower install mdast-range

Duo

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>

Table of Contents

Usage

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
    }
  }
}

API

mdast.use(range)

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`.
});

License

MIT © Titus Wormer

Keywords

FAQs

Package last updated on 31 Aug 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc