Skip to content

syntax-tree/unist-util-select

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unist-util-select Build Coverage Downloads Chat

Select unist nodes with CSS-like selectors.

View the list of supported selectors »

Installation

npm:

npm install unist-util-select

API

select.one(tree, selector)

select.one(tree)(selector)

Select the first node matching selector in the given tree (could be the tree itself). Returns the found node, if any. Throws an error if node is not found or not unique.

Usage

Say we have the following file, example.md:

1.  Step 1.
2.  TODO Step 2.
3.  Step 3.

    1. TODO Step 3.1.
    2. Step 3.2.
    3. TODO Step 3.3.

And our script, example.js, looks as follows:

var fs = require('fs')
var remark = require('remark')
var select = require('unist-util-select')

var tree = remark().parse(fs.readFileSync('example.md'))

var step = select.one(tree, 'list text[value*=3.2]')

console.log(step)

Now, running node example yields:

{ type: 'text', value: 'Step 3.2.' }

select(tree, selector)

select(tree)(selector)

Select all nodes matching selector in the given tree (could include the tree itself). Returns the found nodes, if any.

Usage

Say we have the following file, example.md:

1.  Step 1.
2.  TODO Step 2.
3.  Step 3.

    1. TODO Step 3.1.
    2. Step 3.2.
    3. TODO Step 3.3.

And our script, example.js, looks as follows:

var fs = require('fs')
var remark = require('remark')
var select = require('unist-util-select')

var tree = remark().parse(fs.readFileSync('example.md'))

var todos = select(tree, 'list text[value*=TODO]')

console.log(todos)

Now, running node example yields:

[ { type: 'text',
    value: 'TODO Step 2.' },
  { type: 'text',
    value: 'TODO Step 3.1.' },
  { type: 'text',
    value: 'TODO Step 3.3.' } ]

Support

Contribute

See contributing.md in syntax-tree/unist for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin

About

utility to select unist nodes with CSS-like selectors

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 5