Skip to content

Commit a56e309

Browse files
authored
Update docs for newcomers
Related-to: unifiedjs/unified#168. Closes GH-6. Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Jeff Caldwell <nemo.omen@gmail.com>
1 parent 86376c7 commit a56e309

File tree

1 file changed

+252
-44
lines changed

1 file changed

+252
-44
lines changed

readme.md

Lines changed: 252 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,77 @@
1212
(`:cite[smith04]`, `::youtube[Video of a cat in a box]{v=01ab2cd3efg}`, and
1313
such).
1414

15-
## Important!
16-
17-
This plugin is made for the new parser in remark
18-
([`micromark`](https://github.com/micromark/micromark),
19-
see [`remarkjs/remark#536`](https://github.com/remarkjs/remark/pull/536)).
20-
Use this plugin for remark 13+.
15+
## Contents
16+
17+
* [What is this?](#what-is-this)
18+
* [When should I use this?](#when-should-i-use-this)
19+
* [Install](#install)
20+
* [Use](#use)
21+
* [API](#api)
22+
* [`unified().use(remarkDirective)`](#unifieduseremarkdirective)
23+
* [Examples](#examples)
24+
* [Example: YouTube](#example-youtube)
25+
* [Example: Styled blocks](#example-styled-blocks)
26+
* [Syntax](#syntax)
27+
* [Syntax tree](#syntax-tree)
28+
* [Types](#types)
29+
* [Compatibility](#compatibility)
30+
* [Security](#security)
31+
* [Related](#related)
32+
* [Contribute](#contribute)
33+
* [License](#license)
34+
35+
## What is this?
36+
37+
This package is a [unified][] ([remark][]) plugin to add support for directives:
38+
one syntax for arbitrary extensions in markdown.
39+
You can use this with some more code to match your specific needs, to allow for
40+
anything from callouts, citations, styled blocks, forms, embeds, spoilers, etc.
41+
42+
unified is an AST (abstract syntax tree) based transform project.
43+
**remark** is everything unified that relates to markdown.
44+
The layer under remark is called mdast, which is only concerned with syntax
45+
trees.
46+
Another layer underneath is micromark, which is only concerned with parsing.
47+
This package is a small wrapper to integrate all of these.
48+
49+
## When should I use this?
50+
51+
Directives are one of the four ways to extend markdown: an arbitrary extension
52+
syntax (see [Extending markdown](https://github.com/micromark/micromark#extending-markdown)
53+
in micromark’s docs for the alternatives and more info).
54+
This mechanism works well when you control the content: who authors it, what
55+
tools handle it, and where it’s displayed.
56+
When authors can read a guide on how to embed a tweet but are not expected to
57+
know the ins and outs of HTML or JavaScript.
58+
Directives don’t work well if you don’t know who authors content, what tools
59+
handle it, and where it ends up.
60+
Example use cases are a docs website for a project or product, or blogging tools
61+
and static site generators.
2162

2263
## Install
2364

24-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
25-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
26-
27-
[npm][]:
65+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
66+
In Node.js (12.20+, 14.14+, 16.0+), install with [npm][]:
2867

2968
```sh
3069
npm install remark-directive
3170
```
3271

72+
In Deno with [Skypack][]:
73+
74+
```js
75+
import remarkDirective from 'https://cdn.skypack.dev/remark-directive@2?dts'
76+
```
77+
78+
In browsers with [Skypack][]:
79+
80+
```html
81+
<script type="module">
82+
import remarkDirective from 'https://cdn.skypack.dev/remark-directive@2?min'
83+
</script>
84+
```
85+
3386
## Use
3487

3588
Say we have the following file, `example.md`:
@@ -50,8 +103,7 @@ A :i[lovely] language know as :abbr[HTML]{title="HyperText Markup Language"}.
50103
And our module, `example.js`, looks as follows:
51104

52105
```js
53-
import {readSync} from 'to-vfile'
54-
import {reporter} from 'vfile-reporter'
106+
import {read} from 'to-vfile'
55107
import {unified} from 'unified'
56108
import remarkParse from 'remark-parse'
57109
import remarkDirective from 'remark-directive'
@@ -61,23 +113,26 @@ import rehypeStringify from 'rehype-stringify'
61113
import {visit} from 'unist-util-visit'
62114
import {h} from 'hastscript'
63115

64-
const file = readSync('example.md')
65-
66-
unified()
67-
.use(remarkParse)
68-
.use(remarkDirective)
69-
.use(customPlugin)
70-
.use(remarkRehype)
71-
.use(rehypeFormat)
72-
.use(rehypeStringify)
73-
.process(file)
74-
.then((file) => {
75-
console.error(reporter(file))
76-
console.log(String(file))
77-
})
78-
79-
// This plugin is just an example! You can handle directives however you please!
80-
function customPlugin() {
116+
main()
117+
118+
async function main() {
119+
const file = await unified()
120+
.use(remarkParse)
121+
.use(remarkDirective)
122+
.use(myRemarkPlugin)
123+
.use(remarkRehype)
124+
.use(rehypeFormat)
125+
.use(rehypeStringify)
126+
.process(await read('example.md'))
127+
128+
console.log(String(file))
129+
}
130+
131+
// This plugin is an example to let users write HTML with directives.
132+
// It’s informative but rather useless.
133+
// See below for others examples.
134+
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
135+
function myRemarkPlugin() {
81136
return (tree) => {
82137
visit(tree, (node) => {
83138
if (
@@ -98,12 +153,7 @@ function customPlugin() {
98153

99154
Now, running `node example` yields:
100155

101-
```txt
102-
example.md: no issues found
103-
```
104-
105156
```html
106-
example.md: no issues found
107157
<main id="readme">
108158
<p>Lorem<br>ipsum.</p>
109159
<hr class="red">
@@ -121,8 +171,163 @@ The default export is `remarkDirective`.
121171
Configures remark so that it can parse and serialize directives.
122172
Doesn’t handle the directives: [create your own plugin][create-plugin] to do
123173
that.
124-
See the [micromark extension for the syntax][syntax] and the
125-
[mdast utility for the syntax tree][syntax-tree].
174+
175+
## Examples
176+
177+
### Example: YouTube
178+
179+
This example shows how directives can be used for YouTube embeds.
180+
It’s based on the example in Use above.
181+
If `myRemarkPlugin` was replaced with this function:
182+
183+
```js
184+
// This plugin is an example to turn `::youtube` into iframes.
185+
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
186+
function myRemarkPlugin() {
187+
return (tree, file) => {
188+
visit(tree, (node) => {
189+
if (
190+
node.type === 'textDirective' ||
191+
node.type === 'leafDirective' ||
192+
node.type === 'containerDirective'
193+
) {
194+
if (node.name !== 'youtube') return
195+
196+
const data = node.data || (node.data = {})
197+
const attributes = node.attributes || {}
198+
const id = attributes.id
199+
200+
if (node.type === 'textDirective') file.fail('Text directives for `youtube` not supported', node)
201+
if (!id) file.fail('Missing video id', node)
202+
203+
data.hName = 'iframe'
204+
data.hProperties = {
205+
src: 'https://www.youtube.com/embed/' + id,
206+
width: 200,
207+
height: 200,
208+
frameBorder: 0,
209+
allow: 'picture-in-picture',
210+
allowFullScreen: true
211+
}
212+
}
213+
})
214+
}
215+
}
216+
```
217+
218+
…and `example.md` contains:
219+
220+
```markdown
221+
# Cat videos
222+
223+
::youtube[Video of a cat in a box]{#01ab2cd3efg}
224+
```
225+
226+
…then running `node example` yields:
227+
228+
```html
229+
<h1>Cat videos</h1>
230+
<iframe src="https://www.youtube.com/embed/01ab2cd3efg" width="200" height="200" frameborder="0" allow="picture-in-picture" allowfullscreen>Video of a cat in a box</iframe>
231+
```
232+
233+
### Example: Styled blocks
234+
235+
Note: This is sometimes called admonitions, callouts, etc.
236+
237+
This example shows how directives can be used to style blocks.
238+
It’s based on the example in Use above.
239+
If `myRemarkPlugin` was replaced with this function:
240+
241+
```js
242+
// This plugin is an example to turn `::note` into divs, passing arbitrary
243+
// attributes.
244+
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
245+
function myRemarkPlugin() {
246+
return (tree) => {
247+
visit(tree, (node) => {
248+
if (
249+
node.type === 'textDirective' ||
250+
node.type === 'leafDirective' ||
251+
node.type === 'containerDirective'
252+
) {
253+
if (node.name !== 'note') return
254+
255+
const data = node.data || (node.data = {})
256+
const tagName = node.type === 'textDirective' ? 'span' : 'div'
257+
258+
data.hName = tagName
259+
data.hProperties = h(tagName, node.attributes).properties
260+
}
261+
})
262+
}
263+
}
264+
```
265+
266+
…and `example.md` contains:
267+
268+
```markdown
269+
# How to use xxx
270+
271+
You can use xxx.
272+
273+
:::note{.warning}
274+
if you chose xxx, you should also use yyy somewhere…
275+
:::
276+
```
277+
278+
…then running `node example` yields:
279+
280+
```html
281+
<h1>How to use xxx</h1>
282+
<p>You can use xxx.</p>
283+
<div class="warning">
284+
<p>if you chose xxx, you should also use yyy somewhere…</p>
285+
</div>
286+
```
287+
288+
## Syntax
289+
290+
This plugin applies a micromark extensions to parse the syntax.
291+
See its readme for parse details:
292+
293+
* [`micromark-extension-directive`](https://github.com/micromark/micromark-extension-directive#syntax)
294+
295+
## Syntax tree
296+
297+
This plugin applies one mdast utility to build and serialize the AST.
298+
See its readme for the node types supported in the tree:
299+
300+
* [`mdast-util-directive`](https://github.com/syntax-tree/mdast-util-directive#syntax-tree)
301+
302+
## Types
303+
304+
This package is fully typed with [TypeScript][].
305+
If you’re working with the syntax tree, make sure to import this plugin
306+
somewhere in your types, as that registers the new node types in the tree.
307+
308+
```js
309+
/** @typedef {import('remark-directive')} */
310+
311+
import {visit} from 'unist-util-visit'
312+
313+
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
314+
export default function myRemarkPlugin() => {
315+
return (tree) => {
316+
visit(tree, (node) => {
317+
// `node` can now be one of the nodes for directives.
318+
})
319+
}
320+
}
321+
```
322+
323+
## Compatibility
324+
325+
Projects maintained by the unified collective are compatible with all maintained
326+
versions of Node.js.
327+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
328+
Our projects sometimes work with older versions, but this is not guaranteed.
329+
330+
This plugin works with unified 9+ and remark 14+.
126331

127332
## Security
128333

@@ -133,13 +338,14 @@ scripting (XSS)][xss] attacks.
133338
## Related
134339

135340
* [`remark-gfm`](https://github.com/remarkjs/remark-gfm)
136-
— GFM
341+
support GFM (autolink literals, strikethrough, tables, tasklists)
137342
* [`remark-github`](https://github.com/remarkjs/remark-github)
138-
— Autolink references like in GitHub issues, PRs, and comments
343+
— link references to commits, issues, pull-requests, and users, like on
344+
GitHub
139345
* [`remark-frontmatter`](https://github.com/remarkjs/remark-frontmatter)
140-
Frontmatter (YAML, TOML, and more)
346+
support frontmatter (YAML, TOML, and more)
141347
* [`remark-math`](https://github.com/remarkjs/remark-math)
142-
Math
348+
support math
143349

144350
## Contribute
145351

@@ -185,6 +391,8 @@ abide by its terms.
185391

186392
[npm]: https://docs.npmjs.com/cli/install
187393

394+
[skypack]: https://www.skypack.dev
395+
188396
[health]: https://github.com/remarkjs/.github
189397

190398
[contributing]: https://github.com/remarkjs/.github/blob/HEAD/contributing.md
@@ -197,18 +405,18 @@ abide by its terms.
197405

198406
[author]: https://wooorm.com
199407

408+
[unified]: https://github.com/unifiedjs/unified
409+
200410
[remark]: https://github.com/remarkjs/remark
201411

202412
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
203413

414+
[typescript]: https://www.typescriptlang.org
415+
204416
[rehype]: https://github.com/rehypejs/rehype
205417

206418
[hast]: https://github.com/syntax-tree/hast
207419

208420
[prop]: https://talk.commonmark.org/t/generic-directives-plugins-syntax/444
209421

210422
[create-plugin]: https://unifiedjs.com/learn/guide/create-a-plugin/
211-
212-
[syntax]: https://github.com/micromark/micromark-extension-directive#syntax
213-
214-
[syntax-tree]: https://github.com/syntax-tree/mdast-util-directive#syntax-tree

0 commit comments

Comments
 (0)