88[ ![ Backers] [ backers-badge ]] [ collective ]
99[ ![ Chat] [ chat-badge ]] [ chat ]
1010
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.
2144
2245## When to use this
2346
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.
2869
2970## Install
3071
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] [ ] :
3574
3675``` sh
3776npm install mdast-util-directive
3877```
3978
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+
4093## Use
4194
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:
43102
44103``` js
104+ import fs from ' node:fs/promises'
45105import {fromMarkdown } from ' mdast-util-from-markdown'
46106import {toMarkdown } from ' mdast-util-to-markdown'
47107import {directive } from ' micromark-extension-directive'
48108import {directiveFromMarkdown , directiveToMarkdown } from ' mdast-util-directive'
49109
50- const doc = ' A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}. '
110+ const doc = await fs . readFile ( ' example.md ' )
51111
52112const tree = fromMarkdown (doc, {
53113 extensions: [directive ()],
@@ -61,7 +121,7 @@ const out = toMarkdown(tree, {extensions: [directiveToMarkdown]})
61121console .log (out)
62122```
63123
64- Now, running ` node example ` yields (positional info removed for brevity):
124+ …now running ` node example.js ` yields (positional info removed for brevity):
65125
66126``` js
67127{
@@ -90,21 +150,21 @@ A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.
90150
91151## API
92152
153+ This package exports the identifiers ` directiveFromMarkdown ` and
154+ ` directiveToMarkdown ` .
155+ There is no default export.
156+
93157### ` directiveFromMarkdown `
94158
159+ Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
160+
95161### ` directiveToMarkdown `
96162
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 ] .
101164
102165There are no options, but passing [ ` options.quote ` ] [ quote ] to
103166` mdast-util-to-markdown ` is honored for attributes.
104167
105- This utility handles parsing and serializing.
106- [ Traverse the tree] [ traversal ] to change them to whatever you please.
107-
108168## Syntax tree
109169
110170The following interfaces are added to ** [ mdast] [ ] ** by this utility.
@@ -256,25 +316,52 @@ or hast).
256316> included.
257317> In JavaScript, both ` null ` and ` undefined ` must be similarly ignored.
258318
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+
259354## Related
260355
261- * [ ` remarkjs/remark ` ] [ remark ]
262- — markdown processor powered by plugins
263- * [ ` remarkjs/remark-directive ` ] [ plugin ]
356+ * [ ` remarkjs/remark-directive ` ] [ remark-directive ]
264357 — remark plugin to support generic directives
265- * [ ` micromark/micromark ` ] [ micromark ]
266- — the smallest commonmark-compliant markdown parser that exists
267358* [ ` micromark/micromark-extension-directive ` ] [ extension ]
268359 — 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
273360
274361## Contribute
275362
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.
278365See [ ` support.md ` ] [ support ] for ways to get help.
279366
280367This project has a [ code of conduct] [ coc ] .
@@ -315,34 +402,40 @@ abide by its terms.
315402
316403[ npm ] : https://docs.npmjs.com/cli/install
317404
405+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
406+
407+ [ esmsh ] : https://esm.sh
408+
409+ [ typescript ] : https://www.typescriptlang.org
410+
318411[ license ] : license
319412
320413[ author ] : https://wooorm.com
321414
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
323418
324- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
419+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
325420
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
327422
328423[ mdast ] : https://github.com/syntax-tree/mdast
329424
330- [ remark ] : https://github.com/remarkjs/remark
425+ [ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
331426
332- [ plugin ] : https://github.com/remarkjs/remark-directive
427+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
333428
334- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from -markdown
429+ [ quote ] : https://github.com/syntax-tree/mdast-util-to -markdown#optionsquote
335430
336- [ to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
431+ [ extension ] : https://github.com/micromark/micromark-extension-directive
337432
338- [ micromark ] : https://github.com/micromark/micromark
433+ [ remark-directive ] : https://github.com/remarkjs/remark-directive
339434
340- [ extension ] : https://github.com/micromark/micromark-extension-directive
435+ [ extending-mmarkdown ] : https://github.com/micromark/micromark#extending-markdown
341436
342437[ prop ] : https://talk.commonmark.org/t/generic-directives-plugins-syntax/444
343438
344- [ quote ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
345-
346439[ traversal ] : https://unifiedjs.com/learn/recipe/tree-traversal/
347440
348441[ dfn-parent ] : https://github.com/syntax-tree/mdast#parent
0 commit comments