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

remark-usage

v11.0.1

Published

remark plugin to add a usage example to your readme

Downloads

14,593

Readme

remark-usage

Build Coverage Downloads Sponsors Backers Chat

remark plugin to add a usage example to a readme.

Contents

What is this?

This package is a unified (remark) plugin to add a usage section to markdown.

When should I use this?

You can use this on readmes of npm packages to keep the docs in sync with the project through an actual code sample.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install remark-usage

Use

This section is rendered by this module from example.js. Turtles all the way down. 🐢🐢🐢

Say we are making a module that exports just enough Pi (3.14159). We’re documenting it with a readme file, example/readme.md:

# PI

More than enough 🍰

## Usage

## License

MIT

…and an example script to document it example/example.js:

// Load dependencies:
import {pi} from './index.js'

// Logging `pi` yields:
console.log('txt', pi)

…if we use remark-usage, we can generate the Usage section

import {remark} from 'remark'
import remarkUsage from 'remark-usage'
import {read} from 'to-vfile'

const file = await read({path: 'readme.md', cwd: 'example'})

await remark().use(remarkUsage).process(file)

…then printing file (the newly generated readme) yields:

# PI

More than enough 🍰

## Usage

Load dependencies:

```javascript
import {pi} from 'pi'
```

Logging `pi` yields:

```txt
3.14159
```

## License

MIT

API

This package exports no identifiers. The default export is remarkUsage.

unified().use(remarkUsage[, options])

Add a usage example to a readme.

Looks for the first heading matching options.heading (case insensitive), removes everything between it and an equal or higher next heading, and replaces that with an example.

The example runs in Node.js (so no side effects!). Line comments (//) are turned into markdown. Calls to console.log() are exposed as code blocks, containing the logged values, so console.log(1 + 1) becomes 2. Use a string as the first argument to log to use as the language for the code.

You can ignore lines with remark-usage-ignore-next:

// remark-usage-ignore-next
const two = sum(1, 1)

// remark-usage-ignore-next 3
function sum(a, b) {
  return a + b
}

…if no skip is given, 1 line is skipped.

Parameters
  • options (Options, optional) — configuration
Returns

Transform (Transformer).

Options

Configuration (TypeScript type).

Fields
  • example (string, optional) — path to example file (optional); resolved from file.cwd; defaults to the first example that exists: 'example.js', 'example/index.js', 'examples.js', 'examples/index.js', 'doc/example.js', 'doc/example/index.js', 'docs/example.js', 'docs/example/index.js'
  • heading (string, default: 'usage') — heading to look for; wrapped in new RegExp('^(' + value + ')$', 'i');
  • main (string, default: pkg.exports, pkg.main, 'index.js') — path to the file; resolved from file.cwd; used to rewrite import x from './main.js' to import x from 'name'
  • name (string, default: pkg.name) — name of the module; used to rewrite import x from './main.js' to import x from 'name'

Types

This package is fully typed with TypeScript. It exports the additional type Options.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, remark-usage@^11, compatible with Node.js 16.

This plugin works with remark version 12+ and remark-cli version 8+.

Security

Use of remark-usage is unsafe because main and example are executed. This could become dangerous if an attacker was able to inject code into those files or their dependencies.

Related

Contribute

See contributing.md in remarkjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer