unist-find

Unist node finder. Useful for working with remark, rehype and retext.

WARNING: This project has been renamed to unist-util-find. Install using unist-util-find instead.
Last updated 4 years ago by blahah .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install unist-find 
SYNC missed versions from official npm registry.

unist-find Travis

Unist node finder. Useful for working with remark, rehype and retext.

Installation

npm install --save unist-find

Usage

Example

var remark = require('remark')
var find = require('unist-find')

remark()
  .use(function () {
    return function (tree) {
      // string condition
      console.log(find(tree, 'value'))

      // object condition
      console.log(find(tree, { value: 'emphasis' }))

      // function condition
      console.log(find(tree, function (node) {
        return node.type === 'inlineCode'
      }))
    }
  })
  .process('Some _emphasis_, **strongness**, and `code`.')

Result:

// string condition: 'value'
{ type: 'text',
  value: 'Some ',
  position:
   Position {
     start: { line: 1, column: 1, offset: 0 },
     end: { line: 1, column: 6, offset: 5 },
     indent: [] } }

// object condition: { value: 'emphasis' }
{ type: 'text',
  value: 'emphasis',
  position:
   Position {
     start: { line: 1, column: 7, offset: 6 },
     end: { line: 1, column: 15, offset: 14 },
     indent: [] } }

// function condition: function (node) { return node.type === 'inlineCode' }
{ type: 'inlineCode',
  value: 'code',
  position:
   Position {
     start: { line: 1, column: 38, offset: 37 },
     end: { line: 1, column: 44, offset: 43 },
     indent: [] } }

API

find(node, condition)

Return the first node that matches condition, or undefined if no node matches.

  • node ([Node](https://github.com/wooorm/unist#node)) - Node to search
  • condition (string, object or function) - Condition used to test each node. Behaviour depends on the type of the condition:
  • string finds first node with a truthy property matching string
  • object finds first node that has matching values for all properties of object
  • function finds first node for which function returns true when passed node as argument

License

MIT

Current Tags

  • 1.0.1                                ...           latest (4 years ago)

2 Versions

  • 1.0.1 [deprecated]           ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (3)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |