Skip to content

Commit ab28821

Browse files
authored
Add improved docs
Closes GH-38. Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 7538365 commit ab28821

File tree

2 files changed

+145
-72
lines changed

2 files changed

+145
-72
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
22
* @typedef {import('mdast').Root} Root
33
* @typedef {import('hast-util-sanitize').Schema} Schema
4-
* @typedef {import('mdast-util-to-hast').Handlers} Handlers
54
*
6-
* @typedef Options
7-
* Configuration.
5+
* @typedef ExtraOptionsFields
6+
* Configuration (optional).
87
* @property {boolean|Schema|null} [sanitize]
98
* How to sanitize the output.
10-
* @property {Handlers} [handlers={}]
9+
* @property {import('mdast-util-to-hast').Handlers} [handlers={}]
1110
* Object mapping mdast nodes to functions handling them.
11+
*
12+
* @typedef {import('hast-util-to-html').Options & ExtraOptionsFields} Options
1213
*/
1314

1415
import {toHtml} from 'hast-util-to-html'

readme.md

Lines changed: 140 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,93 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**remark**][remark] plugin to serialize Markdown as HTML.
12-
13-
> ⚠️ This package essentially packs [`remark-rehype`][remark-rehype] and
14-
> [`rehype-stringify`][rehype-stringify], and although it does support some
15-
> customisation, it isn’t very pluggable.
16-
> It’s probably smarter to use `remark-rehype` directly and benefit from the
17-
> [**rehype**][rehype] ecosystem.
11+
**[remark][]** plugin to add support for serializing HTML.
12+
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`unified().use(remarkHtml[, options])`](#unifieduseremarkhtml-options)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
27+
28+
## What is this?
29+
30+
This package is a [unified][] ([remark][]) plugin that compiles markdown to
31+
HTML.
32+
33+
**unified** is a project that transforms content with abstract syntax trees
34+
(ASTs).
35+
**remark** adds support for markdown to unified.
36+
**rehype** adds support for HTML to unified.
37+
**mdast** is the markdown AST that remark uses.
38+
**hast** is the HTML AST that rehype uses.
39+
This is a remark plugin that adds a compiler to compile mdast to hast and then
40+
to a string.
41+
42+
## When should I use this?
43+
44+
This plugin is useful when you want to turn markdown into HTML.
45+
It’s a shortcut for `.use(remarkRehype).use(rehypeStringify)`.
46+
47+
The reason that there are different ecosystems for markdown and HTML is that
48+
turning markdown into HTML is, while frequently needed, not the only purpose of
49+
markdown.
50+
Checking (linting) and formatting markdown are also common use cases for
51+
remark and markdown.
52+
There are several aspects of markdown that do not translate 1-to-1 to HTML.
53+
In some cases markdown contains more information than HTML: for example, there
54+
are several ways to add a link in markdown (as in, autolinks: `<https://url>`,
55+
resource links: `[label](url)`, and reference links with definitions:
56+
`[label][id]` and `[id]: url`).
57+
In other cases HTML contains more information than markdown: there are many
58+
tags, which add new meaning (semantics), available in HTML that aren’t available
59+
in markdown.
60+
If there was just one AST, it would be quite hard to perform the tasks that
61+
several remark and rehype plugins currently do.
62+
63+
This plugin is useful when you want to quickly turn markdown into HTML.
64+
In most cases though, it’s recommended to use [`remark-rehype`][remark-rehype]
65+
instead and finally use [`rehype-stringify`][rehype-stringify] to serialize
66+
HTML.
67+
The reason using both ecosystems instead of this plugin is recommended, is that
68+
there are many useful rehype plugins that you can then use.
69+
For example, you can [minify HTML][rehype-minify], [format HTML][rehype-format],
70+
[highlight code][rehype-highlight], [add metadata][rehype-meta], and a lot more.
1871

1972
## Install
2073

21-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
22-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
23-
24-
[npm][]:
74+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
75+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2576

2677
```sh
2778
npm install remark-html
2879
```
2980

81+
In Deno with [Skypack][]:
82+
83+
```js
84+
import remarkHtml from 'https://cdn.skypack.dev/remark-html@15?dts'
85+
```
86+
87+
In browsers with [Skypack][]:
88+
89+
```html
90+
<script type="module">
91+
import remarkHtml from 'https://cdn.skypack.dev/remark-html@15?min'
92+
</script>
93+
```
94+
3095
## Use
3196

32-
Say we have the following file, `example.md`:
97+
Say we have the following file `example.md`:
3398

3499
```markdown
35100
# Hello & World
@@ -39,26 +104,27 @@ Say we have the following file, `example.md`:
39104
* Some _emphasis_, **importance**, and `code`.
40105
```
41106

42-
And our module, `example.js`, looks as follows:
107+
And our module `example.js` looks as follows:
43108

44109
```js
45-
import fs from 'node:fs'
110+
import {read} from 'to-vfile'
46111
import {unified} from 'unified'
47112
import remarkParse from 'remark-parse'
48113
import remarkHtml from 'remark-html'
49114

50-
const buf = fs.readFileSync('example.md')
115+
main()
51116

52-
unified()
53-
.use(remarkParse)
54-
.use(remarkHtml)
55-
.process(buf)
56-
.then((file) => {
57-
console.log(String(file))
58-
})
117+
async function main() {
118+
const file = await unified()
119+
.use(remarkParse)
120+
.use(remarkHtml)
121+
.process(await read('example.md'))
122+
123+
console.log(String(file))
124+
}
59125
```
60126

61-
Now, running `node example` yields:
127+
Now running `node example.js` yields:
62128

63129
```html
64130
<h1>Hello &#x26; World</h1>
@@ -77,61 +143,61 @@ The default export is `remarkHtml`.
77143

78144
### `unified().use(remarkHtml[, options])`
79145

80-
Serialize Markdown as HTML.
146+
Add support for serializing HTML.
81147

82148
##### `options`
83149

84-
All options except for `sanitize` and `handlers` are passed to
85-
[`hast-util-to-html`][to-html].
86-
87-
The underlying tools allow much more customisation.
88-
It is recommended to replace this project with [`remark-rehype`][remark-rehype]
89-
and [`rehype-stringify`][rehype-stringify] ;
150+
Configuration (optional).
151+
All options other than `sanitize` and `handlers` are passed to
152+
[`hast-util-to-html`][hast-util-to-html].
90153

91154
###### `options.handlers`
92155

93-
Object mapping [mdast][] [nodes][mdast-node] to functions handling them.
94-
This option is passed to [`mdast-util-to-hast`][to-hast-handlers].
156+
This option is a bit advanced as it requires knowledge of ASTs, so we defer
157+
to the documentation available in
158+
[`mdast-util-to-hast`][mdast-util-to-hast].
95159

96160
###### `options.sanitize`
97161

98162
How to sanitize the output (`Object` or `boolean`, default: `true`):
99163

100164
* `false`
101-
HTML is not sanitized, dangerous HTML persists
165+
output is not sanitized, dangerous raw HTML persists
102166
* `true`
103-
HTML is sanitized according to [GitHub’s sanitation rules][github],
104-
dangerous HTML is dropped
167+
output is sanitized according to [GitHub’s sanitation rules][github],
168+
dangerous raw HTML is dropped
105169
* `Object`
106-
the object is treated as a `schema` for how to sanitize with
107-
[`hast-util-sanitize`][sanitize], dangerous HTML is dropped
170+
`schema` that defines how to sanitize output with
171+
[`hast-util-sanitize`][sanitize], dangerous raw HTML is dropped
108172

109-
> Note that raw HTML in Markdown cannot be sanitized, so it’s removed.
110-
> A schema can still be used to allow certain values from other plugins
111-
> though.
112-
> To support HTML in Markdown, use [`rehype-raw`][raw].
173+
## Types
113174

114-
For example, to add strict sanitation but allowing `className`s, use something
115-
like:
175+
This package is fully typed with [TypeScript][].
176+
It exports an `Options` type, which specifies the interface of the accepted
177+
options.
116178

117-
```js
118-
// ...
119-
var merge = require('deepmerge')
120-
var github = require('hast-util-sanitize/lib/github')
179+
## Compatibility
121180

122-
var schema = merge(github, {attributes: {'*': ['className']}})
181+
Projects maintained by the unified collective are compatible with all maintained
182+
versions of Node.js.
183+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
184+
Our projects sometimes work with older versions, but this is not guaranteed.
123185

124-
remark()
125-
.use(html, {sanitize: schema})
126-
.processSync(/**/)
127-
```
186+
This plugin works with `unified` version 6+ and `remark` version 7+.
128187

129188
## Security
130189

131-
Use of `remark-html` is *unsafe* by default and opens you up to a
132-
[cross-site scripting (XSS)][xss] attack.
190+
Use of `remark-html` is **unsafe** by default and opens you up to
191+
[cross-site scripting (XSS)][xss] attacks.
133192
Pass `sanitize: true` to prevent attacks.
134-
Settings `sanitize` to anything else may be unsafe.
193+
Setting `sanitize` to anything else can be unsafe.
194+
195+
## Related
196+
197+
* [`remark-rehype`](https://github.com/remarkjs/remark-rehype)
198+
— turn markdown into HTML to support rehype
199+
* [`rehype-sanitize`](https://github.com/rehypejs/rehype-sanitize)
200+
— sanitize HTML
135201

136202
## Contribute
137203

@@ -177,38 +243,44 @@ abide by its terms.
177243

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

246+
[skypack]: https://www.skypack.dev
247+
180248
[health]: https://github.com/remarkjs/.github
181249

182-
[contributing]: https://github.com/remarkjs/.github/blob/HEAD/contributing.md
250+
[contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
183251

184-
[support]: https://github.com/remarkjs/.github/blob/HEAD/support.md
252+
[support]: https://github.com/remarkjs/.github/blob/main/support.md
185253

186-
[coc]: https://github.com/remarkjs/.github/blob/HEAD/code-of-conduct.md
254+
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
187255

188256
[license]: license
189257

190258
[author]: https://wooorm.com
191259

260+
[unified]: https://github.com/unifiedjs/unified
261+
192262
[remark]: https://github.com/remarkjs/remark
193263

194-
[remark-rehype]: https://github.com/remarkjs/remark-rehype
264+
[github]: https://github.com/syntax-tree/hast-util-sanitize#schema
265+
266+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
195267

196-
[rehype]: https://github.com/rehypejs/rehype
268+
[typescript]: https://www.typescriptlang.org
197269

198-
[rehype-stringify]: https://github.com/rehypejs/rehype/tree/HEAD/packages/rehype-stringify
270+
[remark-rehype]: https://github.com/remarkjs/remark-rehype
199271

200-
[raw]: https://github.com/rehypejs/rehype-raw
272+
[rehype-minify]: https://github.com/rehypejs/rehype-minify
201273

202-
[mdast]: https://github.com/syntax-tree/mdast
274+
[rehype-format]: https://github.com/rehypejs/rehype-format
203275

204-
[mdast-node]: https://github.com/syntax-tree/mdast#nodes
276+
[rehype-highlight]: https://github.com/rehypejs/rehype-highlight
205277

206-
[to-html]: https://github.com/syntax-tree/hast-util-to-html
278+
[rehype-meta]: https://github.com/rehypejs/rehype-meta
207279

208-
[to-hast-handlers]: https://github.com/syntax-tree/mdast-util-to-hast#optionshandlers
280+
[rehype-stringify]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-stringify
209281

210282
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
211283

212-
[github]: https://github.com/syntax-tree/hast-util-sanitize#schema
284+
[hast-util-to-html]: https://github.com/syntax-tree/hast-util-to-html
213285

214-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
286+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast

0 commit comments

Comments
 (0)