The AstQuery class is a wrapper around The MDAST AST Tree
It provides methods for querying nodes in the document.
const doc = collection.document("epics/authentication")
const { astQuery } = doc
// equivalent to doc.astQuery but can be used against any AST
const query = doc.queryAst(document.ast)
const other = new AstQuery(doc.ast)
You can use any of the selectors documented in unist-util-select
*
(universal selector),
(multiple selector)paragraph
(type selector)blockquote paragraph
(combinator: descendant selector)blockquote > paragraph
(combinator: child selector)code + paragraph
(combinator: adjacent sibling selector)code ~ paragraph
(combinator: general sibling selector)[attr]
(attribute existence, checks that the value on the tree is not
nullish)[attr=value]
(attribute equality, this stringifies values on the tree)[attr^=value]
(attribute begins with, only works on strings)[attr$=value]
(attribute ends with, only works on strings)[attr*=value]
(attribute contains, only works on strings)[attr~=value]
(attribute contains, checks if value
is in the array,
if there’s an array on the tree, otherwise same as attribute equality):any()
(functional pseudo-class, use :matches
instead):has()
(functional pseudo-class)
Relative selectors (:has(> img)
) are not supported, but :scope
is:matches()
(functional pseudo-class):not()
(functional pseudo-class):blank
(pseudo-class, blank and empty are the same: a parent without
children, or a node without value):empty
(pseudo-class, blank and empty are the same: a parent without
children, or a node without value):root
(pseudo-class, matches the given node):scope
(pseudo-class, matches the given node):first-child
(pseudo-class):first-of-type
(pseudo-class):last-child
(pseudo-class):last-of-type
(pseudo-class):only-child
(pseudo-class):only-of-type
(pseudo-class):nth-child()
(functional pseudo-class):nth-last-child()
(functional pseudo-class):nth-last-of-type()
(functional pseudo-class):nth-of-type()
(functional pseudo-class)const h1 = astQuery.selectAll("heading[depth=1]")
const h2 = astQuery.selectAll("heading[depth=2]")
const codeBlocks = astQuery.selectAll("code")
There is also astQuery.select
which will only return one node instead of an array of nodes.
const nodes = astQuery.findBetweenHeadings(
astQuery.select("heading")[0],
astQuery.select("heading")[1]
)
const afterNodes = astQuery.findAllAfter(astQuery.select("heading")[0])
There is also astQuery.findAfter
which just finds a single node.
const beforeNodes = astQuery.findAllBefore(astQuery.select("heading")[0])
There is also astQuery.findBefore
which just finds a single node.
Find all nodes in the document that come before the given node. Passing an optional selector will filter the results.
Find all nodes in the document that come after the given node. Passing an optional selector will filter the results.
Find the first node before the given node which matches the selector
{AstNode} node
a node in the AST to start searching from{String|Function} selector
a selector string from unist-util-select, or a function which will be passed the node and return true if it matchesFind the next node after the given node which matches the given elector
{AstNode} node
a node in the AST to start searching from{Function} selector
a function which returns true if the node matchesReturns {AstNode} the first node to match the selector
Find all the nodes between the given nodes
{AstNode} nodeOne
a node in the AST to start searching from{AstNode} nodeTwo
a node in the AST to stop searching atFind the first node which matches the selector. See unist-util-select
{String|Function} selector
a string from unist-util-select or a function which should return true if the node matchesReturns {AstNode} the first node to match the selector
Find all nodes which match the selector. See unist-util-select
{String|Function} selector
a string from unist-util-select or a function which should return true if the node matchesReturns {Array[AstNode]} all nodes to match the selector
Run the given visitor function for each node in the document.
{Function} visitor
a function which will be passed a node from the tree.Get the node at a given line number.
{Number} lineNumber
the line number to findReturns {AstNode} the node at the given line number
Find the next heading node with the same depth as the given node.
{AstNode} headingNode
a heading node to start searching fromReturns {AstNode} the next heading node with the same depth as the given node
Get all headings at a given depth.
{Number} depth
the heading depth to find (1-6)Returns {Array[AstNode]} all headings at the given depth
Finds all headings where the content matches the given string. Not case sensitive. Passing false as a second argument will use a substring match.
{String} text
the text of the heading to match{Boolean} exact
whether to use an exact match or a substring matchReturns {Array[AstNode]} all headings with the given text
Find a heading where the content matches the given string. Not case sensitive. Passing false as a second argument will use a substring match.
{String} text
the text of the heading to match{Boolean} exact
whether to use an exact match or a substring matchReturns {AstNode} the first heading with the given text
[]
The AstQuery class is a wrapper around The MDAST AST Tree
It provides methods for querying nodes in the document.
const doc = collection.document("epics/authentication")
const { astQuery } = doc
// equivalent to doc.astQuery but can be used against any AST
const query = doc.queryAst(document.ast)
const other = new AstQuery(doc.ast)
You can use any of the selectors documented in unist-util-select
*
(universal selector),
(multiple selector)paragraph
(type selector)blockquote paragraph
(combinator: descendant selector)blockquote > paragraph
(combinator: child selector)code + paragraph
(combinator: adjacent sibling selector)code ~ paragraph
(combinator: general sibling selector)[attr]
(attribute existence, checks that the value on the tree is not
nullish)[attr=value]
(attribute equality, this stringifies values on the tree)[attr^=value]
(attribute begins with, only works on strings)[attr$=value]
(attribute ends with, only works on strings)[attr*=value]
(attribute contains, only works on strings)[attr~=value]
(attribute contains, checks if value
is in the array,
if there’s an array on the tree, otherwise same as attribute equality):any()
(functional pseudo-class, use :matches
instead):has()
(functional pseudo-class)
Relative selectors (:has(> img)
) are not supported, but :scope
is:matches()
(functional pseudo-class):not()
(functional pseudo-class):blank
(pseudo-class, blank and empty are the same: a parent without
children, or a node without value):empty
(pseudo-class, blank and empty are the same: a parent without
children, or a node without value):root
(pseudo-class, matches the given node):scope
(pseudo-class, matches the given node):first-child
(pseudo-class):first-of-type
(pseudo-class):last-child
(pseudo-class):last-of-type
(pseudo-class):only-child
(pseudo-class):only-of-type
(pseudo-class):nth-child()
(functional pseudo-class):nth-last-child()
(functional pseudo-class):nth-last-of-type()
(functional pseudo-class):nth-of-type()
(functional pseudo-class)const h1 = astQuery.selectAll("heading[depth=1]")
const h2 = astQuery.selectAll("heading[depth=2]")
const codeBlocks = astQuery.selectAll("code")
There is also astQuery.select
which will only return one node instead of an array of nodes.
const nodes = astQuery.findBetweenHeadings(
astQuery.select("heading")[0],
astQuery.select("heading")[1]
)
const afterNodes = astQuery.findAllAfter(astQuery.select("heading")[0])
There is also astQuery.findAfter
which just finds a single node.
const beforeNodes = astQuery.findAllBefore(astQuery.select("heading")[0])
There is also astQuery.findBefore
which just finds a single node.
Find all nodes in the document that come before the given node. Passing an optional selector will filter the results.
Find all nodes in the document that come after the given node. Passing an optional selector will filter the results.
Find the first node before the given node which matches the selector
{AstNode} node
a node in the AST to start searching from{String|Function} selector
a selector string from unist-util-select, or a function which will be passed the node and return true if it matchesFind the next node after the given node which matches the given elector
{AstNode} node
a node in the AST to start searching from{Function} selector
a function which returns true if the node matchesReturns {AstNode} the first node to match the selector
Find all the nodes between the given nodes
{AstNode} nodeOne
a node in the AST to start searching from{AstNode} nodeTwo
a node in the AST to stop searching atFind the first node which matches the selector. See unist-util-select
{String|Function} selector
a string from unist-util-select or a function which should return true if the node matchesReturns {AstNode} the first node to match the selector
Find all nodes which match the selector. See unist-util-select
{String|Function} selector
a string from unist-util-select or a function which should return true if the node matchesReturns {Array[AstNode]} all nodes to match the selector
Run the given visitor function for each node in the document.
{Function} visitor
a function which will be passed a node from the tree.Get the node at a given line number.
{Number} lineNumber
the line number to findReturns {AstNode} the node at the given line number
Find the next heading node with the same depth as the given node.
{AstNode} headingNode
a heading node to start searching fromReturns {AstNode} the next heading node with the same depth as the given node
Get all headings at a given depth.
{Number} depth
the heading depth to find (1-6)Returns {Array[AstNode]} all headings at the given depth
Finds all headings where the content matches the given string. Not case sensitive. Passing false as a second argument will use a substring match.
{String} text
the text of the heading to match{Boolean} exact
whether to use an exact match or a substring matchReturns {Array[AstNode]} all headings with the given text
Find a heading where the content matches the given string. Not case sensitive. Passing false as a second argument will use a substring match.
{String} text
the text of the heading to match{Boolean} exact
whether to use an exact match or a substring matchReturns {AstNode} the first heading with the given text
[]