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

Range information to mdast

  • 0.2.0
  • npm
  • Socket score

Version published
Weekly downloads
43
decreased by-92.62%
Maintainers
1
Weekly downloads
 
Created
Source

mdast-range Build Status Coverage Status

Add range information to 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
            }
          }
        },
        {
          "type": "emphasis",
          "children": [
            {
              "type": "text",
              "value": "emphasis",
              "position": {
                "start": {
                  "line": 1,
                  "column": 7,
                  "offset": 6
                },
                "end": {
                  "line": 1,
                  "column": 15,
                  "offset": 14
                }
              }
            }
          ],
          "position": {
            "start": {
              "line": 1,
              "column": 6,
              "offset": 5
            },
            "end": {
              "line": 1,
              "column": 16,
              "offset": 15
            }
          }
        },
        {
          "type": "text",
          "value": ",\n",
          "position": {
            "start": {
              "line": 1,
              "column": 16,
              "offset": 15
            },
            "end": {
              "line": 2,
              "column": 1,
              "offset": 17
            }
          }
        },
        {
          "type": "strong",
          "children": [
            {
              "type": "text",
              "value": "strongness",
              "position": {
                "start": {
                  "line": 2,
                  "column": 3,
                  "offset": 19
                },
                "end": {
                  "line": 2,
                  "column": 13,
                  "offset": 29
                }
              }
            }
          ],
          "position": {
            "start": {
              "line": 2,
              "column": 1,
              "offset": 17
            },
            "end": {
              "line": 2,
              "column": 15,
              "offset": 31
            }
          }
        },
        {
          "type": "text",
          "value": ",\nand ",
          "position": {
            "start": {
              "line": 2,
              "column": 15,
              "offset": 31
            },
            "end": {
              "line": 3,
              "column": 5,
              "offset": 37
            }
          }
        },
        {
          "type": "inlineCode",
          "value": "code",
          "position": {
            "start": {
              "line": 3,
              "column": 5,
              "offset": 37
            },
            "end": {
              "line": 3,
              "column": 11,
              "offset": 43
            }
          }
        },
        {
          "type": "text",
          "value": ".",
          "position": {
            "start": {
              "line": 3,
              "column": 11,
              "offset": 43
            },
            "end": {
              "line": 3,
              "column": 12,
              "offset": 44
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 3,
          "column": 12,
          "offset": 44
        }
      }
    }
  ],
  "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"`

License

MIT © Titus Wormer

Keywords

FAQs

Package last updated on 25 Apr 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