unist-util-select
Select Unist nodes with CSS-like selectors.
Example
example.md:
Get all TODO items from this list:
1. Step 1.
2. TODO Step 2.
3. Step 3.
1. TODO Step 3.1.
2. Step 3.2.
3. TODO Step 3.3.
remark takes this Markdown as an input and returns unist syntax tree. After that, we use unist-util-select to extract the required parts:
var select = require('unist-util-select');
var markdown = fs.readFileSync('example.md', 'utf8');
var ast = remark.parse(markdown);
select(ast, 'list text[value*=TODO]')
//=> [ { type: 'text', value: 'TODO Step 2.' },
// { type: 'text', value: 'TODO Step 3.1.' },
// { type: 'text', value: 'TODO Step 3.3.' } ]
That's it!
Features
All the relevant parts of Selectors Level 3:
- Type selectors:
paragraph - Descendant selectors:
paragraph text - Child selectors:
paragraph > text - Sibling selectors:
paragraph ~ text - Adjacent sibling selectors:
paragraph + text - Group selectors:
paragraph, text - Universal selector:
* - Attribute selectors:
text[value*="substr"]- Existence:
[value] - Equality:
[value="foo"] - Begins with:
[value^="prefix"] - Containment:
[value*="substr"] - Ends with:
[value$="suffix"]
- Existence:
- Structural pseudo-classes:
paragraph:first-of-type:root:nth-child(2n+1):nth-last-child(2n+1):nth-of-type(2n+1):nth-last-of-type(2n+1):first-child:last-child:first-of-type:last-of-type:only-child:only-of-type:empty
- Negation pseudo-class:
*:not(paragraph)
API
select(ast, selector)
Curried form: select(ast)(selector)
Applies selector to ast, returns array of matching nodes.
select.one(ast, selector)
Curried form: select.one(ast)(selector)
Returns a single node matching selector.
Throws an error if node is not found or not unique.
Install
npm install unist-util-select
License
MIT

