README
remark-usage
remark plugin to add a usage example to a readme.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package is a unified (remark) plugin to add a Usage section to markdown.
unified is an AST (abstract syntax tree) based transform project. remark is everything unified that relates to markdown. The layer under remark is called mdast, which is only concerned with syntax trees. Another layer underneath is micromark, which is only concerned with parsing. This package is a small wrapper to integrate all of these.
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 (12.20+, 14.14+, 16.0+), 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 {readSync} from 'to-vfile'
import {remark} from 'remark'
import remarkUsage from 'remark-usage'
const file = readSync({path: 'readme.md', cwd: 'example'})
const result = await remark().use(remarkUsage).process(file)
Now, printing result
(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 example.js
to the Usage
section in a readme.
Replaces the current content between the heading containing the text “usage” (configurable) and the next heading of the same (or higher) rank with the example.
The example is run in Node.js.
Make sure no side effects occur when running example.js
.
Line comments are parsed as markdown.
Calls to console.log()
are exposed as code blocks, containing the logged
values (optionally with a language flag).
It may help to compare example.js
with the above use
section.
You can ignore lines like so:
// 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.
options
options.heading
Heading to look for (string?
, default: 'usage'
).
Wrapped in new RegExp('^(' + value + ')