8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- Extension for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and/or
12
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] to support the [ generic directives
13
- proposal] [ prop ] (` :cite[smith04] ` , `::youtube[ Video of a cat in a
14
- box] {v=01ab2cd3efg}`, and such) in ** [ mdast] [ ] ** .
15
- When parsing (` from-markdown ` ), must be combined with
16
- [ ` micromark-extension-directive ` ] [ extension ] .
17
-
18
- See [ ` micromark-extension-directive ` ] [ extension ] for how the syntax works.
19
- This utility handles parsing and serializing.
20
- [ Traverse the tree] [ traversal ] to change them to whatever you please.
11
+ [ mdast] [ ] extensions to parse and serialize [ generic directives proposal] [ prop ]
12
+ (` :cite[smith04] ` , ` ::youtube[Video of a cat in a box]{v=01ab2cd3efg} ` , and
13
+ such).
14
+
15
+ ## Contents
16
+
17
+ * [ What is this?] ( #what-is-this )
18
+ * [ When to use this] ( #when-to-use-this )
19
+ * [ Install] ( #install )
20
+ * [ Use] ( #use )
21
+ * [ API] ( #api )
22
+ * [ ` directiveFromMarkdown ` ] ( #directivefrommarkdown )
23
+ * [ ` directiveToMarkdown ` ] ( #directivetomarkdown )
24
+ * [ Syntax tree] ( #syntax-tree )
25
+ * [ Nodes] ( #nodes )
26
+ * [ Mixin] ( #mixin )
27
+ * [ ` Directive ` ] ( #directive )
28
+ * [ Types] ( #types )
29
+ * [ Compatibility] ( #compatibility )
30
+ * [ Related] ( #related )
31
+ * [ Contribute] ( #contribute )
32
+ * [ License] ( #license )
33
+
34
+ ## What is this?
35
+
36
+ This package contains extensions that add support for generic directives to
37
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
38
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
39
+
40
+ This package handles the syntax tree.
41
+ You can use this with some more code to match your specific needs, to allow for
42
+ anything from callouts, citations, styled blocks, forms, embeds, spoilers, etc.
43
+ [ Traverse the tree] [ traversal ] to change directives to whatever you please.
21
44
22
45
## When to use this
23
46
24
- Use this if you’re dealing with the AST manually.
25
- It might be better to use [ ` remark-directive ` ] [ plugin ] with ** [ remark] [ ] ** ,
26
- which includes this but provides a nicer interface and makes it easier to
27
- combine with hundreds of plugins.
47
+ These tools are all rather low-level.
48
+ In most cases, you’d want to use [ ` remark-directive ` ] [ remark-directive ] with
49
+ remark instead.
50
+
51
+ Directives are one of the four ways to extend markdown: an arbitrary extension
52
+ syntax (see [ Extending markdown] [ extending-mmarkdown ] in micromark’s docs for
53
+ 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.
62
+
63
+ When working with ` mdast-util-from-markdown ` , you must combine this package with
64
+ [ ` micromark-extension-directive ` ] [ extension ] .
65
+
66
+ This utility does not handle how directives are turned to HTML.
67
+ You must [ traverse the tree] [ traversal ] to change directives to whatever you
68
+ please.
28
69
29
70
## Install
30
71
31
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
32
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
33
-
34
- [ npm] [ ] :
72
+ This package is [ ESM only] [ esm ] .
73
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
35
74
36
75
``` sh
37
76
npm install mdast-util-directive
38
77
```
39
78
79
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
80
+
81
+ ``` js
82
+ import {directiveFromMarkdown , directiveToMarkdown } from ' https://esm.sh/mdast-util-directive@2'
83
+ ```
84
+
85
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
86
+
87
+ ``` html
88
+ <script type =" module" >
89
+ import {directiveFromMarkdown , directiveToMarkdown } from ' https://esm.sh/mdast-util-directive@2?bundle'
90
+ </script >
91
+ ```
92
+
40
93
## Use
41
94
42
- Say our module, ` example.js ` , looks as follows:
95
+ Say our document ` example.md ` contains:
96
+
97
+ ``` markdown
98
+ A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.
99
+ ```
100
+
101
+ …and our module ` example.js ` looks as follows:
43
102
44
103
``` js
104
+ import fs from ' node:fs/promises'
45
105
import {fromMarkdown } from ' mdast-util-from-markdown'
46
106
import {toMarkdown } from ' mdast-util-to-markdown'
47
107
import {directive } from ' micromark-extension-directive'
48
108
import {directiveFromMarkdown , directiveToMarkdown } from ' mdast-util-directive'
49
109
50
- const doc = ' A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}. '
110
+ const doc = await fs . readFile ( ' example.md ' )
51
111
52
112
const tree = fromMarkdown (doc, {
53
113
extensions: [directive ()],
@@ -61,7 +121,7 @@ const out = toMarkdown(tree, {extensions: [directiveToMarkdown]})
61
121
console .log (out)
62
122
```
63
123
64
- Now, running ` node example ` yields (positional info removed for brevity):
124
+ …now running ` node example.js ` yields (positional info removed for brevity):
65
125
66
126
``` js
67
127
{
@@ -90,21 +150,21 @@ A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.
90
150
91
151
## API
92
152
153
+ This package exports the identifiers ` directiveFromMarkdown ` and
154
+ ` directiveToMarkdown ` .
155
+ There is no default export.
156
+
93
157
### ` directiveFromMarkdown `
94
158
159
+ Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
160
+
95
161
### ` directiveToMarkdown `
96
162
97
- Support the [ generic directives proposal] [ prop ] .
98
- The exports are extensions, respectively
99
- for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and
100
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] .
163
+ Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
101
164
102
165
There are no options, but passing [ ` options.quote ` ] [ quote ] to
103
166
` mdast-util-to-markdown ` is honored for attributes.
104
167
105
- This utility handles parsing and serializing.
106
- [ Traverse the tree] [ traversal ] to change them to whatever you please.
107
-
108
168
## Syntax tree
109
169
110
170
The following interfaces are added to ** [ mdast] [ ] ** by this utility.
@@ -256,25 +316,52 @@ or hast).
256
316
> included.
257
317
> In JavaScript, both ` null ` and ` undefined ` must be similarly ignored.
258
318
319
+ ## Types
320
+
321
+ This package is fully typed with [ TypeScript] [ ] .
322
+ It exports the additional types ` ContainerDirective ` , ` LeafDirective ` ,
323
+ ` TextDirective ` , and ` Directive ` .
324
+
325
+ It also registers the node types with ` @types/mdast ` .
326
+ If you’re working with the syntax tree, make sure to import this utility
327
+ somewhere in your types, as that registers the new node types in the tree.
328
+
329
+ ``` js
330
+ /**
331
+ * @typedef {import('mdast-util-directive')}
332
+ */
333
+
334
+ import {visit } from ' unist-util-visit'
335
+
336
+ /** @type {import('mdast').Root} */
337
+ const tree = getMdastNodeSomeHow ()
338
+
339
+ visit (tree, (node ) => {
340
+ // `node` can now be one of the nodes for directives.
341
+ })
342
+ ```
343
+
344
+ ## Compatibility
345
+
346
+ Projects maintained by the unified collective are compatible with all maintained
347
+ versions of Node.js.
348
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
349
+ Our projects sometimes work with older versions, but this is not guaranteed.
350
+
351
+ This plugin works with ` mdast-util-from-markdown ` version 1+ and
352
+ ` mdast-util-to-markdown ` version 1+.
353
+
259
354
## Related
260
355
261
- * [ ` remarkjs/remark ` ] [ remark ]
262
- — markdown processor powered by plugins
263
- * [ ` remarkjs/remark-directive ` ] [ plugin ]
356
+ * [ ` remarkjs/remark-directive ` ] [ remark-directive ]
264
357
— remark plugin to support generic directives
265
- * [ ` micromark/micromark ` ] [ micromark ]
266
- — the smallest commonmark-compliant markdown parser that exists
267
358
* [ ` micromark/micromark-extension-directive ` ] [ extension ]
268
359
— micromark extension to parse directives
269
- * [ ` syntax-tree/mdast-util-from-markdown ` ] [ from-markdown ]
270
- — mdast parser using ` micromark ` to create mdast from markdown
271
- * [ ` syntax-tree/mdast-util-to-markdown ` ] [ to-markdown ]
272
- — mdast serializer to create markdown from mdast
273
360
274
361
## Contribute
275
362
276
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
277
- started.
363
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
364
+ ways to get started.
278
365
See [ ` support.md ` ] [ support ] for ways to get help.
279
366
280
367
This project has a [ code of conduct] [ coc ] .
@@ -315,34 +402,40 @@ abide by its terms.
315
402
316
403
[ npm ] : https://docs.npmjs.com/cli/install
317
404
405
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
406
+
407
+ [ esmsh ] : https://esm.sh
408
+
409
+ [ typescript ] : https://www.typescriptlang.org
410
+
318
411
[ license ] : license
319
412
320
413
[ author ] : https://wooorm.com
321
414
322
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
415
+ [ health ] : https://github.com/syntax-tree/.github
416
+
417
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
323
418
324
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
419
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
325
420
326
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
421
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
327
422
328
423
[ mdast ] : https://github.com/syntax-tree/mdast
329
424
330
- [ remark ] : https://github.com/remarkjs/remark
425
+ [ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
331
426
332
- [ plugin ] : https://github.com/remarkjs/remark-directive
427
+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
333
428
334
- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from -markdown
429
+ [ quote ] : https://github.com/syntax-tree/mdast-util-to -markdown#optionsquote
335
430
336
- [ to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
431
+ [ extension ] : https://github.com/micromark/micromark-extension-directive
337
432
338
- [ micromark ] : https://github.com/micromark/micromark
433
+ [ remark-directive ] : https://github.com/remarkjs/remark-directive
339
434
340
- [ extension ] : https://github.com/micromark/micromark-extension-directive
435
+ [ extending-mmarkdown ] : https://github.com/micromark/micromark#extending-markdown
341
436
342
437
[ prop ] : https://talk.commonmark.org/t/generic-directives-plugins-syntax/444
343
438
344
- [ quote ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
345
-
346
439
[ traversal ] : https://unifiedjs.com/learn/recipe/tree-traversal/
347
440
348
441
[ dfn-parent ] : https://github.com/syntax-tree/mdast#parent
0 commit comments