Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including unist-util-find with all npm packages installed. Try it out:

var unistUtilFind = require("unist-util-find")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

unist-util-find v1.0.1

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

unist-find Travis

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

Installation

npm install --save unist-util-find

Usage

Example

var remark = require('remark')
var find = require('unist-util-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) - 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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free