GithubHelp home page GithubHelp logo

remarkjs / remark-usage Goto Github PK

View Code? Open in Web Editor NEW
42.0 10.0 17.0 256 KB

plugin to add a usage example to your readme

Home Page: https://remark.js.org

License: MIT License

JavaScript 100.00%
remark remark-plugin markdown usage

remark-usage's Introduction

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

remark-usage's People

Contributors

ben-eb avatar greenkeeperio-bot avatar niftylettuce avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

remark-usage's Issues

How to prevent a line from rendering?

For example, it's not so easy to define this one liner, and it'd be great if you could ignore lines from being rendered somehow.

console.log('sh', require('child_process').execSync('tree new-project').toString());

Multi-line console logging

console.log(
  'sh',
  require('child_process')
    .execSync('tree new-package -I "node_modules|media|coverage"')
    .toString()
);

Outputs this unfortunately instead of the second arg invoked:

```javascript

console.log(
  'sh',
  require('child_process')
    .execSync('tree new-package -I "node_modules|media|coverage"')
    .toString()
);

Bug with [email protected]

README.md
            1:1  error    TypeError: Cannot read property 'example' of undefined
    at /Users/niftylettuce/Public/lass/node_modules/remark-usage/index.js:261:35
    at String.replace (<anonymous>)
    at script (/Users/niftylettuce/Public/lass/node_modules/remark-usage/index.js:260:19)
    at postprocess (/Users/niftylettuce/Public/lass/node_modules/remark-usage/index.js:81:28)
    at run (/Users/niftylettuce/Public/lass/node_modules/remark-usage/index.js:240:7)
    at search (/Users/niftylettuce/Public/lass/node_modules/remark-usage/node_modules/mdast-util-heading-range/index.js:82:13)
    at headingRange (/Users/niftylettuce/Public/lass/node_modules/remark-usage/node_modules/mdast-util-heading-range/index.js:36:3)
    at /Users/niftylettuce/Public/lass/node_modules/remark-usage/index.js:175:5
    at wrapped (/Users/niftylettuce/Public/lass/node_modules/trough/index.js:93:19)
    at next (/Users/niftylettuce/Public/lass/node_modules/trough/index.js:56:24)

Compile asynchronous console logs?

Would it be possible to evaluate calls that use the Promise API?

function add (a, b) {
  return new Promise(resolve => {
    return resolve((a + b).toString());
  });
}

// Add two numbers together
add(2, 8).then(console.log);

Output:

function add (a, b) {
  return new Promise(resolve => {
    return resolve((a + b).toString());
  });
}

Add two numbers together

10

es-module import syntax

i normally use es-module syntax for imports in my usage examples, but i love the idea of writing the example as a separate file that is lintable/testable. since es-module syntax isn't yet part of the language, it makes sense why it wouldn't work out of the box. any chance there is an existing way to apply babel-register or similar to support things like this?

Feature idea: Multiple line comments

Just a thought that randomly came to me... it'd be nice to have multi-line comments in the example files.

So instead of writing this:

console.log('sh', 'yarn global add foo');
console.log('sh', 'foo bar --baz');

You could write this:

console.log('sh', `
yarn global add foo
foo bar --baz
`);

Not sure if that's best approach, something probably better could be thought of.

cc @wooorm

possible build problem with the latest release?

Subject of the issue

when trying to load v7.0.0 into my .remarkrc.js, the same way i did with the previous version, i'm now met with an error:

Error: Cannot parse file `.remarkrc.js`
Cannot parse script `node_modules/remark-usage/index.js`
Error: Cannot find module './lib'

upon inspection of the node_modules/remark-usage/ directory, there is, in fact, no lib/ present

Your environment

$ node -v
v10.16.0
$ npm -v
6.9.0

Steps to reproduce

Tell us how to reproduce this issue. Please provide a working and simplified example.

πŸŽ‰ BONUS POINTS for creating a minimal reproduction and uploading it to GitHub. This will get you the fastest support. πŸŽ‰

  1. clone and checkout branch: https://github.com/travi/javascript-scaffolder/tree/greenkeeper/remark-usage-7.0.0
  2. npm run generate:md

Expected behaviour

What should happen?

remark should rebuild me README.md and include usage details from example.js

Actual behaviour

What happens instead?

error described above is shown

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.