plugin to wrap a fragment in a document
36
stars
147
commits
JavaScript
primary language
Mar 18, 2025
updated
rehype plugin to wrap a fragment in a document.
This package is a unified (rehype) plugin to wrap a fragment in a document. It’s especially useful when going from a markdown file that represents an article and turning it into a complete HTML document.
unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that wraps a fragment in a document.
This project is useful when you want to turn a fragment
(specifically,
some nodes that can exist in a <body> element)
into a whole document
(a <html>, <head>, and <body>, where the latter will contain the
fragment).
This plugin can make fragments valid whole documents.
It’s not a (social) metadata manager.
That’s done by rehype-meta.
You can use both together.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install rehype-document
In Deno with esm.sh:
import rehypeDocument from 'https://esm.sh/rehype-document@7'
In browsers with esm.sh:
<script type="module">
import rehypeDocument from 'https://esm.sh/rehype-document@7?bundle'
</script>
Say we have the following file example.md:
# Pluto
Pluto is a dwarf planet in the Kuiper belt.
…and a module example.js :
import rehypeDocument from 'rehype-document'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {read} from 'to-vfile'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeDocument, {title: 'Pluto'})
.use(rehypeStringify)
.process(await read('example.md'))
console.log(String(file))
…then running node example.js yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pluto</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<h1>Pluto</h1>
<p>Pluto is a dwarf planet in the Kuiper belt.</p>
</body>
</html>
This package exports no identifiers.
It exports the TypeScript type Options.
The default export is rehypeDocument.
unified().use(rehypeDocument[, options])Wrap a fragment in a document.
options (Options, optional)
— configurationTransform (Transformer).
OptionsConfiguration (TypeScript type).
css
(Array<string> or string, optional)
— URLs to stylesheets to use in <link>sdir
('auto', 'ltr', or 'rtl', optional)
— direction of the documentjs
(Array<string> or string, optional)
— URLs to scripts to use as src on <script>slanguage
(string, default: 'en')
— language of document; should be a BCP 47 language taglink
(Array<Properties> or Properties, optional)
— generate extra <link>s with these properties;
passed as properties to hastscript with 'link'meta
(Array<Properties> or Properties, optional)
— generate extra <meta>s with these properties;
passed as properties to hastscript with 'meta'responsive
(boolean, default: true)
— generate a meta[viewport]script
(Array<string> or string, optional)
— JavaScript source code of <script>s to add at end of bodystyle
(Array<string> or string, optional)
— CSS source code of <style>s to addtitle
(string, optional)
— text to use as title;
defaults to the file name (if any);
can bet set with file.data.matter.title
(vfile-matter)
and file.data.meta.title
(rehype-infer-title-meta),
which are preferredThis example shows how to set a language:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {title: 'פּלוטאָ', language: 'yi', dir: 'rtl'})
.use(rehypeStringify)
.process('<h1>העלא, פּלוטאָ!</h1>')
console.log(String(file))
Yields:
<!doctype html>
<html dir="rtl" lang="yi">
<head>
<meta charset="utf-8">
<title>פּלוטאָ</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<h1>העלא, פּלוטאָ!</h1>
</body>
</html>
This example shows how to reference CSS files and include stylesheets:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
css: 'https://example.com/index.css',
style: 'body { color: red }'
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<style>body { color: red }</style>
<link href="https://example.com/index.css" rel="stylesheet">
</head>
<body>
</body>
</html>
This example shows how to reference JS files and include scripts:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
js: 'https://example.com/index.js',
script: 'console.log(1)'
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<script>console.log(1)</script>
<script src="https://example.com/index.js"></script>
</body>
</html>
This example shows how to define metadata and include links (other than styles):
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
link: [
{href: '/favicon.ico', rel: 'icon', sizes: 'any'},
{href: '/icon.svg', rel: 'icon', type: 'image/svg+xml'}
],
meta: [{content: 'rehype-document', name: 'generator'}]
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="rehype-document" name="generator">
<link href="/favicon.ico" rel="icon" sizes="any">
<link href="/icon.svg" rel="icon" type="image/svg+xml">
</head>
<body>
</body>
</html>
💡 Tip:
rehype-metais a (social) metadata manager.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release,
we drop support for unmaintained versions of Node.
This means we try to keep the current release line,
rehype-document@7,
compatible with Node.js 16.
This plugin works with rehype-parse version 3+,
rehype-stringify version 3+,
rehype version 5+,
and unified version 6+.
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.
rehype-meta
— add metadata to the head of a documentrehype-format
— format HTMLrehype-minify
— minify HTMLSee 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.
JavaScript
100.0%
plugin to wrap a fragment in a document
36
stars
147
commits
JavaScript
primary language
Mar 18, 2025
updated
rehype plugin to wrap a fragment in a document.
This package is a unified (rehype) plugin to wrap a fragment in a document. It’s especially useful when going from a markdown file that represents an article and turning it into a complete HTML document.
unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that wraps a fragment in a document.
This project is useful when you want to turn a fragment
(specifically,
some nodes that can exist in a <body> element)
into a whole document
(a <html>, <head>, and <body>, where the latter will contain the
fragment).
This plugin can make fragments valid whole documents.
It’s not a (social) metadata manager.
That’s done by rehype-meta.
You can use both together.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install rehype-document
In Deno with esm.sh:
import rehypeDocument from 'https://esm.sh/rehype-document@7'
In browsers with esm.sh:
<script type="module">
import rehypeDocument from 'https://esm.sh/rehype-document@7?bundle'
</script>
Say we have the following file example.md:
# Pluto
Pluto is a dwarf planet in the Kuiper belt.
…and a module example.js :
import rehypeDocument from 'rehype-document'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {read} from 'to-vfile'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeDocument, {title: 'Pluto'})
.use(rehypeStringify)
.process(await read('example.md'))
console.log(String(file))
…then running node example.js yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pluto</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<h1>Pluto</h1>
<p>Pluto is a dwarf planet in the Kuiper belt.</p>
</body>
</html>
This package exports no identifiers.
It exports the TypeScript type Options.
The default export is rehypeDocument.
unified().use(rehypeDocument[, options])Wrap a fragment in a document.
options (Options, optional)
— configurationTransform (Transformer).
OptionsConfiguration (TypeScript type).
css
(Array<string> or string, optional)
— URLs to stylesheets to use in <link>sdir
('auto', 'ltr', or 'rtl', optional)
— direction of the documentjs
(Array<string> or string, optional)
— URLs to scripts to use as src on <script>slanguage
(string, default: 'en')
— language of document; should be a BCP 47 language taglink
(Array<Properties> or Properties, optional)
— generate extra <link>s with these properties;
passed as properties to hastscript with 'link'meta
(Array<Properties> or Properties, optional)
— generate extra <meta>s with these properties;
passed as properties to hastscript with 'meta'responsive
(boolean, default: true)
— generate a meta[viewport]script
(Array<string> or string, optional)
— JavaScript source code of <script>s to add at end of bodystyle
(Array<string> or string, optional)
— CSS source code of <style>s to addtitle
(string, optional)
— text to use as title;
defaults to the file name (if any);
can bet set with file.data.matter.title
(vfile-matter)
and file.data.meta.title
(rehype-infer-title-meta),
which are preferredThis example shows how to set a language:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {title: 'פּלוטאָ', language: 'yi', dir: 'rtl'})
.use(rehypeStringify)
.process('<h1>העלא, פּלוטאָ!</h1>')
console.log(String(file))
Yields:
<!doctype html>
<html dir="rtl" lang="yi">
<head>
<meta charset="utf-8">
<title>פּלוטאָ</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<h1>העלא, פּלוטאָ!</h1>
</body>
</html>
This example shows how to reference CSS files and include stylesheets:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
css: 'https://example.com/index.css',
style: 'body { color: red }'
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<style>body { color: red }</style>
<link href="https://example.com/index.css" rel="stylesheet">
</head>
<body>
</body>
</html>
This example shows how to reference JS files and include scripts:
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
js: 'https://example.com/index.js',
script: 'console.log(1)'
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<script>console.log(1)</script>
<script src="https://example.com/index.js"></script>
</body>
</html>
This example shows how to define metadata and include links (other than styles):
import rehypeDocument from 'rehype-document'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeDocument, {
link: [
{href: '/favicon.ico', rel: 'icon', sizes: 'any'},
{href: '/icon.svg', rel: 'icon', type: 'image/svg+xml'}
],
meta: [{content: 'rehype-document', name: 'generator'}]
})
.use(rehypeStringify)
.process('')
console.log(String(file))
Yields:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="rehype-document" name="generator">
<link href="/favicon.ico" rel="icon" sizes="any">
<link href="/icon.svg" rel="icon" type="image/svg+xml">
</head>
<body>
</body>
</html>
💡 Tip:
rehype-metais a (social) metadata manager.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release,
we drop support for unmaintained versions of Node.
This means we try to keep the current release line,
rehype-document@7,
compatible with Node.js 16.
This plugin works with rehype-parse version 3+,
rehype-stringify version 3+,
rehype version 5+,
and unified version 6+.
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.
rehype-meta
— add metadata to the head of a documentrehype-format
— format HTMLrehype-minify
— minify HTMLSee 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.
JavaScript
100.0%