unified

Project: rehypejs/rehype-document

Package: rehype-document@6.0.0

  1. Dependents: 26
  2. rehype plugin to wrap a document around a fragment
  1. unified 173
  2. plugin 137
  3. html 122
  4. rehype 87
  5. hast 73
  6. rehype-plugin 61
  7. tree 41
  8. syntax 27
  9. document 11
  10. wrap 4

rehype-document

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to wrap a document around a fragment.

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-document

Use

Say example.md looks as follows:

## Hello world!

This is **my** document.

…and example.js like this:

import {readSync} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeDocument from 'rehype-document'
import rehypeStringify from 'rehype-stringify'

const file = readSync('example.md')

unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeDocument, {title: 'Hi!'})
  .use(rehypeStringify)
  .process(file)
  .then((file) => {
    console.error(reporter(file))
    console.log(String(file))
  })

Now, running node example yields:

example.md: no issues found
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hi!</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Hello world!</h2>
<p>This is <strong>my</strong> document.</p>
</body>
</html>

API

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

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

Wrap a document around a fragment.

options
options.title

Text to use as title (string, default: name of file, if any).

options.language

Natural language of document (string, default: 'en'). should be a BCP 47 language tag.

options.responsive

Whether to insert a meta[viewport] (boolean, default: true).

options.style

CSS to include in head in <style> elements (string or Array<string>, default: []).

options.css

Links to stylesheets to include in head (string or Array<string>, default: []).

options.meta

Metadata to include in head (Object or Array<Object>, default: []). Each object is passed as properties to hastscript with a meta element.

Link tags to include in head (Object or Array<Object>, default: []). Each object is passed as properties to hastscript with a link element.

options.script

Inline scripts to include at end of body (string or Array<string>, default: []).

options.js

External scripts to include at end of body (string or Array<string>, default: []).

Security

Use of rehype-document can open you up to a cross-site scripting (XSS) attack if you pass user provided content in options.

Always be wary of user input and use rehype-sanitize.

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