Select unist nodes using css-like selectors.
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.
mdast
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 = mdast.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!
- 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)
Applies selector
to ast
, returns array of matching nodes.
npm install unist-util-select
MIT