Skip to content

Commit

Permalink
Add improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 21, 2022
1 parent b03ce26 commit 353622c
Showing 1 changed file with 91 additions and 32 deletions.
123 changes: 91 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,103 @@
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

**[mdast][]** utility to get the plain text content of a node.
[mdast][] utility to get the text content of a node.

## Install
## Contents

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`toString(node[, options])`](#tostringnode-options)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This package is a tiny utility that gets the textual content of a node.

## When should I use this?

This utility is useful when you have a node, say a heading, and want to get the
text inside it.

This package does not serialize markdown, that’s what
[`mdast-util-to-markdown`][mdast-util-to-markdown] does.

Similar packages, [`hast-util-to-string`][hast-util-to-string] and
[`hast-util-to-text`][hast-util-to-text], do the same but on [hast][].

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
## Install

[npm][]:
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:

```sh
npm install mdast-util-to-string
```

In Deno with [`esm.sh`][esmsh]:

```js
import {toString} from 'https://esm.sh/mdast-util-to-string@3'
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
import {toString} from 'https://esm.sh/mdast-util-to-string@3?bundle'
</script>
```

## Use

```js
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toString} from 'mdast-util-to-string'

const tree = unified()
.use(remarkParse)
.parse('Some _emphasis_, **importance**, and `code`.')
const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')

console.log(toString(tree)) // => 'Some emphasis, importance, and code.'
```

## API

This package exports the following identifiers: `toString`.
This package exports the identifier `toString`.
There is no default export.

### `toString(node[, options])`

Get the text content of a [node][] or list of nodes.
Prefers the node’s plain-text fields, otherwise serializes its children, and if
the given value is an array, serialize the nodes in it.

The algorithm checks `value` of `node` and then `alt`.
If no value is found, the algorithm checks the children of `node` and joins them
(without spaces or newlines).
##### `options`

> This is not a markdown to plain-text library.
> Use [`strip-markdown`][strip-markdown] for that.
Configuration (optional).

###### `options.includeImageAlt`

Whether to use `alt` (`boolean`, default: `true`)
Whether to use `alt` (`boolean`, default: `true`).

## Types

This package is fully typed with [TypeScript][].
It exports the type `Options`.

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

## Security

Expand All @@ -63,19 +114,15 @@ attacks.

## Related

* [`nlcst-to-string`](https://github.com/syntax-tree/nlcst-to-string)
— Get text content in nlcst
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-to-string)
— Get text content in hast
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/main/packages/hast-util-to-string)
— get text content in hast
* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text)
— Get text content in hast according to the `innerText` algorithm
* [`hast-util-from-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-from-string)
— Set text content in hast
— get text content in hast according to the `innerText` algorithm

## Contribute

See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.

This project has a [code of conduct][coc].
Expand Down Expand Up @@ -116,22 +163,34 @@ abide by its terms.

[npm]: https://docs.npmjs.com/cli/install

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[license]: license

[author]: https://wooorm.com

[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
[health]: https://github.com/syntax-tree/.github

[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md

[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md

[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md

[mdast]: https://github.com/syntax-tree/mdast

[node]: https://github.com/syntax-tree/mdast#nodes
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown

[strip-markdown]: https://github.com/remarkjs/strip-markdown
[hast]: https://github.com/syntax-tree/hast

[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[hast-util-to-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string

[hast]: https://github.com/syntax-tree/hast
[hast-util-to-text]: https://github.com/syntax-tree/hast-util-to-text

[node]: https://github.com/syntax-tree/mdast#nodes

[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

0 comments on commit 353622c

Please sign in to comment.