npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mkql

v1.0.9

Published

Query language for markdown documents

Downloads

3,695

Readme

Query Language

Build Status npm version Coverage Status

Query a document tree with selectors

Extracts nodes using a selector syntax that is a subset of the CSS selectors specification.

Install

npm i mkql --save

For the command line interface install mkdoc globally (npm i -g mkdoc).



Usage

Pass selectors when creating the stream:

var ql = require('mkql')
  , ast = require('mkast');

ast.src('Paragraph\n\n* 1\n* 2\n* 3\n\n```javascript\nvar foo;\n```')
  .pipe(ql('p, ul, pre[info^=javascript]'))
  .pipe(ast.stringify({indent: 2}))
  .pipe(process.stdout);

Example

mkcat README.md | mkql 'p, ul, pre[info^=javascript]' | mkout
printf 'Para 1\n\nPara 2\n\n* List item\n\n' | mkcat | mkql '*' | mkout -y

Selectors

Implemented selectors work like their CSS counterparts and in some cases extensions have been added specific to markdown tree nodes.

Type Selectors

Types are based on the equivalent HTML element name, so to select a node of paragraph type use p; the universal selector * will select nodes of any type.

The map of standard HTML tag names to node types is:

  • p: paragraph
  • ul: list
  • ol: list
  • li: item
  • h1-h6: heading
  • pre: code_block
  • blockquote: block_quote
  • hr: thematic_break
  • code: code
  • em: emph
  • strong: strong
  • a: link
  • br: linebreak
  • img: image

Extensions for markdown specific types:

  • nl: softbreak
  • text: text
  • html: html_block
  • inline: html_inline

Descendant Combinator

Use whitespace for a descendant combinator or if you prefer use the explicit >> notation from CSS4:

ol li
ol >> li

Child Combinator

A selector such as ol li will find all descendants use the child combinator operator when you just want direct children:

ol > li

Adjacent Sibling Combinator

The adjacent sibling combinator is supported; select all lists that are directly preceeded by a paragraph:

p + ul

Following Sibling Combinator

The following sibling combinator is supported; select code that is preceeded by a text node:

p text ~ code

Attribute Selectors

You can match on attributes in the same way as usual but attributes are matched against tree nodes not HTML elements so the attribute names are often different.

a[href^=http://domain.com]

See attribute selectors (@mdn) for more information on the available operators.

The operator =~ (not to be confused with ~=) is a non-standard operator that may be used to match by regular expression pattern:

img[src=~\.(png|jpg)$]

Literal Attribute

For all nodes that have a literal property you may match on the attribute.

p text[literal~=example]

Nodes that have a literal property include:

  • pre: code_block
  • code: code
  • text: text
  • html: html_block
  • inline: html_inline

Content Attribute

The content attribute is available for containers that can contain text nodes. This is a more powerful (but slower) method to match on the text content.

Consider the document:

Paragraph with some *emphasis* and *italic*.

If we select on the literal attribute we would get a text node, for example:

p [literal^=emph]

Results in the child text node with a literal value of emphasis. Often we may wish to match the parent element instead to do so use the content attribute:

p [content^=emph]

Which returns the emph node containing the text node matched with the previous literal query.

The value for the content attribute is all the child text nodes concatenated together which is why it will always be less performant than matching on the literal.

Anchor Attributes

Links support the href and title attributes.

a[href^=http://]
a[title^=Example]

Image Attributes

Images support the src and title attributes.

img[src$=.jpg]
img[title^=Example]

Code Block Attributes

Code blocks support the info and fenced attributes.

pre[info^=javascript]
pre[fenced]

List Attributes

The list and item types (ul, ol and li) support the bullet and delimiter attributes.

So you can select elements depending upon the bullet character used (unordered lists) or the delimiter (ordered lists). For the bullet attribute valid values are +, * and -; for the delimiter attribute valid values are . or ).

This selector will match lists declared using the * character:

ul[bullet=*]

Or for all ordered lists declared using the 1) style:

ol[delimiter=)]

Use a child selector to get list items:

ul li[bullet=+]

Pseudo Classes

The pseudo classes :first-child, :last-child, :only-child and :nth-child are supported.

p a:first-child
p a:last-child
ul li:nth-child(5)
ul li:nth-child(2n+1)
ul li:nth-child(odd) /* same as above */
ul li:nth-child(2n)
ul li:nth-child(even)  /* same as above */
ul li:only-child

See the :nth-child docs (@mdn) for more information.

Relational

The relational pseudo-class :has is useful for selecting parents based on a condition:

p:has(em)
a:has(> img)

Negation

The negation pseudo-class :not is also available:

p:not(:first-child)

Empty

Use the :empty pseudo-class to select nodes with no children:

p :empty

Pseudo Elements

Use the pseudo element prefix :: to select elements not directly in the tree.

HTML

The pseudo elements used to select the html_block and html_inline nodes by type are:

  • ::comment Select comments <!-- -->
  • ::pi Select processing instructions <? ?>
  • ::doctype Select doctype declarations <!doctype html>
  • ::cdata Select CDATA declarations <![CDATA[]]>
  • ::element Select block and inline elements <div></div>
::doctype           /* select doctype declarations */
p ::comment         /* select inline html comments */

Help

Usage: mkql [-dprmnh] [--delete] [--preserve] [--range] [--multiple]
            [--newline] [--help] [--version] <selector...>
       mkql [-dprmnh] [--delete] [--preserve] [--multiple] [--newline] [--help]
            [--version] --range <start-selector> [end-selector]

  Query documents with selectors.

Options
  -d, --delete            Remove matched nodes
  -p, --preserve          Preserve text when deleting
  -r, --range             Execute a range query
  -m, --multiple          Include multiple ranges
  -n, --newline           Add line break between matches
  -h, --help              Display help and exit
  --version               Print the version and exit

[email protected]

API

compile

compile(source)

Compile a source selector string to a tree representation.

Returns Object result tree.

  • source String input selector.

range

range(start[, end])

Compile a range query.

When an end selector is given it must have the same number of selectors in the list as the start selector.

If the end selector is not given the range will end when the start selector matches again or the end of file is reached.

  • start String selector to start the range match.
  • end String selector to end the range match.

slice

slice(source[, opts])

Execute a range query on the input nodes.

Returns Range query execution object.

  • source Object compiled range query.
  • opts Object range query options.

query

query(markdown, source[, opts])

Query a markdown document tree with a source selector.

If the markdown parameter is a string it is parsed into a document tree.

If the given source selector is a string it is compiled otherwise it should be a previously compiled result tree.

If the source selector appears to be a range query the slice function is called with the range query.

Returns Array list of matched nodes.

  • markdown Array|Object|String input data.
  • source String|Object input selector.
  • opts Object query options.

ql

ql([opts][, cb])

Run queries on an input stream.

Returns an output stream.

  • opts Object processing options.
  • cb Function callback function.

Options

  • input Readable input stream.
  • output Writable output stream.

License

MIT


Created by mkdoc on April 24, 2016