Skip to content

Commit e9f3a86

Browse files
committed
Add improved docs
1 parent 383c486 commit e9f3a86

File tree

2 files changed

+152
-32
lines changed

2 files changed

+152
-32
lines changed

lib/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ import {track} from 'mdast-util-to-markdown/lib/util/track.js'
2828

2929
// To do: next major: replace `containerFlow`, `containerPhrasing`, `track`
3030
// with `state` methods.
31+
// To do: next major: expose functions.
3132

3233
const own = {}.hasOwnProperty
3334

3435
const shortcut = /^[^\t\n\r "#'.<=>`}]+$/
3536

3637
handleDirective.peek = peekDirective
3738

38-
/** @type {FromMarkdownExtension} */
39+
/**
40+
* Extension for `mdast-util-from-markdown` to enable directives.
41+
*
42+
* @type {FromMarkdownExtension}
43+
*/
3944
export const directiveFromMarkdown = {
4045
canContainEols: ['textDirective'],
4146
enter: {
@@ -77,7 +82,11 @@ export const directiveFromMarkdown = {
7782
}
7883
}
7984

80-
/** @type {ToMarkdownExtension} */
85+
/**
86+
* Extension for `mdast-util-to-markdown` to enable directives.
87+
*
88+
* @type {ToMarkdownExtension}
89+
*/
8190
export const directiveToMarkdown = {
8291
unsafe: [
8392
{

readme.md

+141-30
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ such).
2121
* [API](#api)
2222
* [`directiveFromMarkdown`](#directivefrommarkdown)
2323
* [`directiveToMarkdown`](#directivetomarkdown)
24+
* [`ContainerDirective`](#containerdirective)
25+
* [`Directive`](#directive)
26+
* [`LeafDirective`](#leafdirective)
27+
* [`TextDirective`](#textdirective)
28+
* [HTML](#html)
29+
* [Syntax](#syntax)
2430
* [Syntax tree](#syntax-tree)
2531
* [Nodes](#nodes)
2632
* [Mixin](#mixin)
27-
* [`Directive`](#directive)
2833
* [Types](#types)
2934
* [Compatibility](#compatibility)
3035
* [Related](#related)
@@ -33,23 +38,18 @@ such).
3338

3439
## What is this?
3540

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.
41+
This package contains two extensions that add support for directive syntax in
42+
markdown to [mdast][].
43+
These extensions plug into
44+
[`mdast-util-from-markdown`][mdast-util-from-markdown] (to support parsing
45+
directives in markdown into a syntax tree) and
46+
[`mdast-util-to-markdown`][mdast-util-to-markdown] (to support serializing
47+
directives in syntax trees to markdown).
4448

4549
## When to use this
4650

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-
5151
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
52+
syntax (see [Extending markdown][extending-markdown] in micromark’s docs for
5353
the alternatives and more info).
5454
This mechanism works well when you control the content: who authors it, what
5555
tools handle it, and where it’s displayed.
@@ -60,17 +60,29 @@ handle it, and where it ends up.
6060
Example use cases are a docs website for a project or product, or blogging tools
6161
and static site generators.
6262

63-
When working with `mdast-util-from-markdown`, you must combine this package with
64-
[`micromark-extension-directive`][extension].
63+
You can use these extensions when you are working with
64+
`mdast-util-from-markdown` and `mdast-util-to-markdown` already.
65+
66+
When working with `mdast-util-from-markdown`, you must combine this package
67+
with [`micromark-extension-directive`][extension].
6568

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.
69+
When you don’t need a syntax tree, you can use [`micromark`][micromark]
70+
directly with `micromark-extension-directive`.
71+
72+
All these packages are used [`remark-directive`][remark-directive], which
73+
focusses on making it easier to transform content by abstracting these
74+
internals away.
75+
76+
This package only handles the syntax tree.
77+
For example, it does not handle how markdown is turned to HTML.
78+
You can use this with some more code to match your specific needs, to allow for
79+
anything from callouts, citations, styled blocks, forms, embeds, spoilers, etc.
80+
[Traverse the tree][traversal] to change directives to whatever you please.
6981

7082
## Install
7183

7284
This package is [ESM only][esm].
73-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
85+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
7486

7587
```sh
7688
npm install mdast-util-directive
@@ -150,21 +162,99 @@ A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.
150162

151163
## API
152164

153-
This package exports the identifiers `directiveFromMarkdown` and
154-
`directiveToMarkdown`.
165+
This package exports the identifiers
166+
[`directiveFromMarkdown`][api-directivefrommarkdown] and
167+
[`directiveToMarkdown`][api-directivetomarkdown].
155168
There is no default export.
156169

157170
### `directiveFromMarkdown`
158171

159-
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
172+
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown] to enable
173+
directives ([`FromMarkdownExtension`][frommarkdownextension]).
160174

161175
### `directiveToMarkdown`
162176

163-
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
177+
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown] to enable
178+
directives ([`ToMarkdownExtension`][tomarkdownextension]).
164179

165180
There are no options, but passing [`options.quote`][quote] to
166181
`mdast-util-to-markdown` is honored for attributes.
167182

183+
### `ContainerDirective`
184+
185+
Directive in flow content (such as in the root document, or block quotes),
186+
which contains further flow content (TypeScript type).
187+
188+
###### Type
189+
190+
```ts
191+
import type {BlockContent, DefinitionContent, Parent} from 'mdast'
192+
193+
interface ContainerDirective extends Parent {
194+
type: 'containerDirective'
195+
name: string
196+
attributes?: Record<string, string | null | undefined> | null | undefined
197+
children: Array<BlockContent | DefinitionContent>
198+
}
199+
```
200+
201+
### `Directive`
202+
203+
The different directive nodes (TypeScript type).
204+
205+
###### Type
206+
207+
```ts
208+
type Directive = ContainerDirective | LeafDirective | TextDirective
209+
```
210+
211+
### `LeafDirective`
212+
213+
Directive in flow content (such as in the root document, or block quotes),
214+
which contains nothing (TypeScript type).
215+
216+
###### Type
217+
218+
```ts
219+
import type {PhrasingContent, Parent} from 'mdast'
220+
221+
interface LeafDirective extends Parent {
222+
type: 'leafDirective'
223+
name: string
224+
attributes?: Record<string, string | null | undefined> | null | undefined
225+
children: Array<PhrasingContent>
226+
}
227+
```
228+
229+
### `TextDirective`
230+
231+
Directive in phrasing content (such as in paragraphs, headings) (TypeScript
232+
type).
233+
234+
###### Type
235+
236+
```ts
237+
import type {PhrasingContent, Parent} from 'mdast'
238+
239+
interface TextDirective extends Parent {
240+
type: 'textDirective'
241+
name: string
242+
attributes?: Record<string, string | null | undefined> | null | undefined
243+
children: Array<PhrasingContent>
244+
}
245+
```
246+
247+
## HTML
248+
249+
This utility does not handle how markdown is turned to HTML.
250+
You can use this with some more code to match your specific needs, to allow for
251+
anything from callouts, citations, styled blocks, forms, embeds, spoilers, etc.
252+
[Traverse the tree][traversal] to change directives to whatever you please.
253+
254+
## Syntax
255+
256+
See [Syntax in `micromark-extension-directive`][syntax].
257+
168258
## Syntax tree
169259

170260
The following interfaces are added to **[mdast][]** by this utility.
@@ -287,7 +377,7 @@ Yields:
287377

288378
### Mixin
289379

290-
### `Directive`
380+
#### `Directive`
291381

292382
```idl
293383
interface mixin Directive {
@@ -319,8 +409,9 @@ or hast).
319409
## Types
320410

321411
This package is fully typed with [TypeScript][].
322-
It exports the additional types `ContainerDirective`, `LeafDirective`,
323-
`TextDirective`, and `Directive`.
412+
It exports the additional types [`ContainerDirective`][api-containerdirective],
413+
[`Directive`][api-directive], [`LeafDirective`][api-leafdirective], and
414+
[`TextDirective`][api-textdirective].
324415

325416
It also registers the node types with `@types/mdast`.
326417
If you’re working with the syntax tree, make sure to import this utility
@@ -345,7 +436,7 @@ visit(tree, (node) => {
345436

346437
Projects maintained by the unified collective are compatible with all maintained
347438
versions of Node.js.
348-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
439+
As of now, that is Node.js 14.14+ and 16.0+.
349440
Our projects sometimes work with older versions, but this is not guaranteed.
350441

351442
This plugin works with `mdast-util-from-markdown` version 1+ and
@@ -428,11 +519,15 @@ abide by its terms.
428519

429520
[quote]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
430521

522+
[micromark]: https://github.com/micromark/micromark
523+
431524
[extension]: https://github.com/micromark/micromark-extension-directive
432525

526+
[syntax]: https://github.com/micromark/micromark-extension-directive#syntax
527+
433528
[remark-directive]: https://github.com/remarkjs/remark-directive
434529

435-
[extending-mmarkdown]: https://github.com/micromark/micromark#extending-markdown
530+
[extending-markdown]: https://github.com/micromark/micromark#extending-markdown
436531

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

@@ -444,4 +539,20 @@ abide by its terms.
444539

445540
[dfn-phrasing-content]: https://github.com/syntax-tree/mdast#phrasingcontent
446541

447-
[dfn-mxn-directive]: #directive
542+
[frommarkdownextension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension
543+
544+
[tomarkdownextension]: https://github.com/syntax-tree/mdast-util-to-markdown#options
545+
546+
[api-directivefrommarkdown]: #directivefrommarkdown
547+
548+
[api-directivetomarkdown]: #directivetomarkdown
549+
550+
[api-containerdirective]: #containerdirective
551+
552+
[api-directive]: #directive
553+
554+
[api-leafdirective]: #leafdirective
555+
556+
[api-textdirective]: #textdirective
557+
558+
[dfn-mxn-directive]: #directive-1

0 commit comments

Comments
 (0)