rehype-document

Wraps a fragment in a complete HTML document with doctype, head, title, viewport, stylesheets and scripts.

Category
Document & metadata
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-document

What it does

Pipelines that start from Markdown or from an HTML snippet produce a fragment. rehype-document wraps that fragment in <!doctype html>, <html>, <head> and <body>, adds a charset, a viewport meta tag and a title, and can link stylesheets and scripts.

Example

document-wrap.mjs
import {rehype} from 'rehype'
import rehypeDocument from 'rehype-document'
import rehypeFormat from 'rehype-format'

const file = await rehype()
  .data('settings', {fragment: true})
  .use(rehypeDocument, {title: 'Report', css: '/styles.css'})
  .use(rehypeFormat)
  .process('<h1>Monthly report</h1>')

console.log(String(file))
Output node document-wrap.mjs
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Report</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="/styles.css" rel="stylesheet">
  </head>
  <body>
    <h1>Monthly report</h1>
  </body>
</html>

Options

  • title: document title (defaults to the file name when there is one).
  • lang (default 'en') and dir ('auto', 'ltr' or 'rtl').
  • css and js: URLs for <link> and <script src> elements.
  • style and script: inline CSS and JavaScript source.
  • meta and link: extra <meta> and <link> elements.
  • responsive (default true): whether to add the viewport meta tag.

Pair it with rehype-format for readable output, as in the example. The upstream rehype-meta plugin can add Open Graph and other metadata to the head.

Official references

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.