unified

Project: rehypejs/rehype-retext

Package: rehype-retext@3.0.1

  1. Dependents: 8
  2. rehype plugin to transform to retext
  1. unified 174
  2. html 121
  3. rehype 85
  4. hast 72
  5. rehype-plugin 60
  6. retext 37
  7. retext-plugin 24
  8. nlcst 14
  9. natural 9

rehype-retext

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to bridge or mutate to retext.

Uses hast-util-to-nlcst under the hood. See its documentation to learn how to ignore nodes.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install rehype-retext

Use

Say example.html looks as follows:

<!doctype html>
<meta charset=utf8>
<title>Hello!</title>
<article>
  A implicit sentence.
  <h1>This and and that.</h1>
</article>

…and example.js like this:

import {readSync} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypePresetMinify from 'rehype-preset-minify'
import rehypeStringify from 'rehype-stringify'
import rehypeRetext from 'rehype-retext'
import retextEnglish from 'retext-english'
import retextIndefiniteArticle from 'retext-indefinite-article'
import retextRepeatedWords from 'retext-repeated-words'

const file = readSync('example.html')

unified()
  .use(rehypeParse)
  .use(
    rehypeRetext,
    unified()
      .use(retextEnglish)
      .use(retextIndefiniteArticle)
      .use(retextRepeatedWords)
  )
  .use(rehypePresetMinify)
  .use(rehypeStringify)
  .process(file)
  .then((file) => {
    console.error(reporter(file))
    console.log(String(file))
  })

Now, running node example yields:

example.html
    5:3-5:4  warning  Use `An` before `implicit`, not `A`  retext-indefinite-article  retext-indefinite-article
  6:12-6:19  warning  Expected `and` once, not twice       and                        retext-repeated-words

⚠ 2 warnings
<!doctypehtml><meta charset=utf8><title>Hello!</title><article>A implicit sentence.<h1>This and and that.</h1></article>

API

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

unified().use(rehypeRetext, destination)

rehype (hast) plugin to bridge or mutate to retext (nlcst).

destination

destination is either a parser or a processor.

If a processor (Unified) is given, runs the destination processor with the new nlcst tree, then, after running discards that tree and continues on running the origin processor with the original tree (bridge-mode).

If a parser (such as parse-latin, parse-english, or parse-dutch) is given, passes the tree to further plugins (mutate-mode).

As HTML defines paragraphs, that definition is used for Paragraphs: <p> and <h1-6> are explicitly supported, and implicit paragraphs in flow content are also supported.

Security

rehype-retext does not change the syntax tree so there are no openings for cross-site scripting (XSS) attacks.

Contribute

See contributing.md in rehypejs/.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